mirror of
https://github.com/meeb/tubesync.git
synced 2025-06-24 14:06:36 +00:00
Add a exponential_backoff
decorator to mimic the background
delays
This commit is contained in:
parent
897aa9ce7a
commit
3b754622f5
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user