Add a exponential_backoff decorator to mimic the background delays

This commit is contained in:
tcely 2025-06-14 13:56:15 -04:00 committed by GitHub
parent 897aa9ce7a
commit 3b754622f5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,3 +1,4 @@
from functools import wraps
def delay_to_eta(delay, /):
@ -59,3 +60,29 @@ def sqlite_tasks(key, /, prefix=None):
),
)
def exponential_backoff(task_func=None, /, *args, *, **kwargs):
if task_func is None:
from django_huey import task as huey_task
task_func = huey_task
def backoff(attempt, /):
return (5+(attempt**4))
def deco(fn):
@wraps(fn)
def inner(*a, **kwa):
task = kwa.pop('task')
try:
return fn(*a, **kwa)
except Exception as exc:
attempt = 1
while task.retry_delay <= backoff(attempt):
attempt += 1
task.retry_delay = backoff(attempt)
raise exc
kwargs.update(dict(
context=True,
retry_delay=backoff(1),
))
return task_func(*args, **kwargs)(inner)
return deco