Allow custom back off functions

This commit is contained in:
tcely authored and GitHub committed 2025-06-15 06:22:18 -04:00
1 parent f73ac4e59a
commit 94b71cbfb9
1 file changed
+17 -3
+17 -3
View File
@@ -65,15 +65,29 @@ 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, /):
backoff_func = kwargs.pop('backoff_func', None)
def default_backoff(attempt, /):
return (5+(attempt**4))
if backoff_func is None or not callable(backoff_func):
backoff_func = default_backoff
def deco(fn):
@wraps(fn)
def inner(*a, **kwa):
task = kwa.pop('task')
backoff = backoff_func
# the scoping becomes complicated when reusing functions
try:
_task = kwa.pop('task')
except KeyError:
pass
else:
task = _task
try:
return fn(*a, **kwa)
except Exception as exc:
try:
task is not None
except NameError:
raise exc
for attempt in range(1, 240):
if backoff(attempt) > task.retry_delay:
task.retry_delay = backoff(attempt)
@@ -84,7 +98,7 @@ def exponential_backoff(task_func=None, /, *args, **kwargs):
raise exc
kwargs.update(dict(
context=True,
retry_delay=backoff(1),
retry_delay=backoff_func(1),
))
return task_func(*args, **kwargs)(inner)
return deco