From 3b754622f5f4fd63bef678878f775d19cc514926 Mon Sep 17 00:00:00 2001 From: tcely Date: Sat, 14 Jun 2025 13:56:15 -0400 Subject: [PATCH] Add a `exponential_backoff` decorator to mimic the `background` delays --- tubesync/common/huey.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tubesync/common/huey.py b/tubesync/common/huey.py index fa36c619..3ea849df 100644 --- a/tubesync/common/huey.py +++ b/tubesync/common/huey.py @@ -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 +