Skip to content

Commit 1cb5932

Browse files
author
Julep Developers
committed
fix(agents-api): Fix cozo query retry logic
Signed-off-by: Julep Developers <[email protected]>
1 parent 8aa82e5 commit 1cb5932

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

agents-api/agents_api/models/utils.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77

88
import pandas as pd
99
from fastapi import HTTPException
10+
from httpcore import NetworkError, TimeoutException
11+
from httpx import RequestError
1012
from pydantic import BaseModel
13+
from requests.exceptions import ConnectionError, Timeout
1114

1215
from ..common.utils.cozo import uuid_int_list_to_uuid4
1316
from ..env import do_verify_developer, do_verify_developer_owns_resource
@@ -254,7 +257,21 @@ def wrapper(*args: P.args, client=None, **kwargs: P.kwargs) -> pd.DataFrame:
254257

255258
debug and print(repr(e))
256259

257-
if "busy" in (str(e) + str(getattr(e, "resp", e))).lower():
260+
pretty_error = repr(e).lower()
261+
cozo_busy = ("busy" in pretty_error) or (
262+
"when executing against relation '_" in pretty_error
263+
)
264+
connection_error = isinstance(
265+
e,
266+
(
267+
ConnectionError,
268+
Timeout,
269+
TimeoutException,
270+
NetworkError,
271+
RequestError,
272+
),
273+
)
274+
if cozo_busy or connection_error:
258275
raise HTTPException(
259276
status_code=429, detail="Resource busy. Please try again later."
260277
) from e
@@ -321,9 +338,10 @@ def is_resource_busy(e: Exception) -> bool:
321338
return isinstance(e, HTTPException) and e.status_code == 429
322339

323340
@retry(
324-
stop=stop_after_attempt(4),
325-
wait=wait_exponential(multiplier=1, min=4, max=10),
341+
stop=stop_after_attempt(6),
342+
wait=wait_exponential(multiplier=1.2, min=3, max=10),
326343
retry=retry_if_exception(is_resource_busy),
344+
reraise=True,
327345
)
328346
@wraps(func)
329347
async def wrapper(
@@ -367,7 +385,21 @@ async def wrapper(
367385

368386
debug and print(repr(e))
369387

370-
if "busy" in (str(e) + str(getattr(e, "resp", e))).lower():
388+
pretty_error = repr(e).lower()
389+
cozo_busy = ("busy" in pretty_error) or (
390+
"when executing against relation '_" in pretty_error
391+
)
392+
connection_error = isinstance(
393+
e,
394+
(
395+
ConnectionError,
396+
Timeout,
397+
TimeoutException,
398+
NetworkError,
399+
RequestError,
400+
),
401+
)
402+
if cozo_busy or connection_error:
371403
raise HTTPException(
372404
status_code=429, detail="Resource busy. Please try again later."
373405
) from e

agents-api/gunicorn_conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
debug = os.getenv("AGENTS_API_DEBUG", "false").lower() == "true"
55

66
# Gunicorn config variables
7-
workers = (multiprocessing.cpu_count() - 1) if not debug else 1
7+
workers = (multiprocessing.cpu_count() // 2) if not debug else 1
88
worker_class = "uvicorn.workers.UvicornWorker"
99
bind = "0.0.0.0:8080"
1010
keepalive = 120

0 commit comments

Comments
 (0)