Fixing new session error when running multiple hendrik
This commit is contained in:
parent
dd4c6c674d
commit
d54fe171fc
@ -30,7 +30,7 @@ class NeoSessionManager:
|
|||||||
self._db = TinyDB(db_path)
|
self._db = TinyDB(db_path)
|
||||||
self._table = self._db.table("sessions")
|
self._table = self._db.table("sessions")
|
||||||
|
|
||||||
def create(self, name: str, model_info: dict) -> NeoSession:
|
def create(self, name: str, model_info: dict, max_retries: int = 5) -> NeoSession:
|
||||||
now = datetime.now(timezone.utc).isoformat()
|
now = datetime.now(timezone.utc).isoformat()
|
||||||
doc = {
|
doc = {
|
||||||
"session_id": str(uuid.uuid4()),
|
"session_id": str(uuid.uuid4()),
|
||||||
@ -40,8 +40,24 @@ class NeoSessionManager:
|
|||||||
"model_info": model_info,
|
"model_info": model_info,
|
||||||
"messages": [],
|
"messages": [],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
import time
|
||||||
|
import random
|
||||||
|
|
||||||
|
for attempt in range(max_retries):
|
||||||
|
try:
|
||||||
doc_id = self._table.insert(doc)
|
doc_id = self._table.insert(doc)
|
||||||
return self._doc_to_session(self._table.get(doc_id=doc_id))
|
return self._doc_to_session(self._table.get(doc_id=doc_id))
|
||||||
|
except ValueError:
|
||||||
|
if attempt < max_retries - 1:
|
||||||
|
# Exponential backoff dengan jitter
|
||||||
|
wait_time = (0.1 * (2 ** attempt)) + random.uniform(0, 0.05)
|
||||||
|
time.sleep(wait_time)
|
||||||
|
# Reload DB untuk sinkronisasi
|
||||||
|
self._db = TinyDB(self._db._root._path)
|
||||||
|
self._table = self._db.table("sessions")
|
||||||
|
else:
|
||||||
|
raise
|
||||||
|
|
||||||
def list(self) -> list[dict]:
|
def list(self) -> list[dict]:
|
||||||
results = []
|
results = []
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user