Skip to content

Commit

Permalink
fix(media): retry requests upload errors (#1100)
Browse files Browse the repository at this point in the history
  • Loading branch information
hassiebp authored Feb 3, 2025
1 parent d951399 commit 57c86f7
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions langfuse/_task_manager/media_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,22 +270,29 @@ def _process_upload_media_job(
def _request_with_backoff(
self, func: Callable[P, T], *args: P.args, **kwargs: P.kwargs
) -> T:
@backoff.on_exception(
backoff.expo, Exception, max_tries=self._max_retries, logger=None
)
def execute_task_with_backoff() -> T:
try:
return func(*args, **kwargs)
except ApiError as e:
if (
def _should_give_up(e: Exception) -> bool:
if isinstance(e, ApiError):
return (
e.status_code is not None
and 400 <= e.status_code < 500
and (e.status_code) != 429
):
raise e
except Exception as e:
raise e
and e.status_code != 429
)
if isinstance(e, requests.exceptions.RequestException):
return (
e.response is not None
and e.response.status_code < 500
and e.response.status_code != 429
)
return False

raise Exception("Failed to execute task")
@backoff.on_exception(
backoff.expo,
Exception,
max_tries=self._max_retries,
giveup=_should_give_up,
logger=None,
)
def execute_task_with_backoff() -> T:
return func(*args, **kwargs)

return execute_task_with_backoff()

0 comments on commit 57c86f7

Please sign in to comment.