Retry Celery tasks with exponential back off

后端 未结 2 1768
隐瞒了意图╮
隐瞒了意图╮ 2020-12-07 11:58

For a task like this:

from celery.decorators import task

@task()
def add(x, y):
    if not x or not y:
        raise Exception(\"test error\")
    return se         


        
相关标签:
2条回答
  • 2020-12-07 12:38

    The task.request.retries attribute contains the number of tries so far, so you can use this to implement exponential back-off:

    from celery.task import task
    
    @task(bind=True, max_retries=3)
    def update_status(self, auth, status):
        try:
            Twitter(auth).update_status(status)
        except Twitter.WhaleFail as exc:
            self.retry(exc=exc, countdown=2 ** self.request.retries)
    

    To prevent a Thundering Herd Problem, you may consider adding a random jitter to your exponential backoff:

    import random
    self.retry(exc=exc, countdown=int(random.uniform(2, 4) ** self.request.retries))
    
    0 讨论(0)
  • 2020-12-07 12:46

    As of Celery 4.2 you can configure your tasks to use an exponential backoff automatically: http://docs.celeryproject.org/en/master/userguide/tasks.html#automatic-retry-for-known-exceptions

    @app.task(autoretry_for=(Exception,), retry_backoff=2)
    def add(x, y):
        ...
    

    (This was already in the docs for Celery 4.1 but actually wasn't released then, see merge request)

    0 讨论(0)
提交回复
热议问题