Increase celery retry time each retry cycle

二次信任 提交于 2019-11-29 01:04:03

问题


I do retries with celery like in the Docs-Example:

@task()
def add(x, y):
    try:
        ...
    except Exception, exc:
        add.retry(exc=exc, countdown=60)  # override the default and
                                          # retry in 1 minute

How can I increase the retry-countdown everytime the retry occurs for this job - e.g. 60 seconds, 2 minutes, 4 minutes and so on until the MaxRetriesExceeded is raised?


回答1:


Since version 4.2 you can use options autoretry_for and retry_backoff for this purposes, for example:

@task(max_retries=10, autoretry_for=(Exception,), retry_backoff=60)
def add(x, y):
    pass



回答2:


Here is a simple way to create bigger delay each time the task is evaluated. This value is updated by celery itself so you don't need to manage anything yourself.

@task()
def add(x, y):
    try:
        ...
    except Exception as exc:
        raise add.retry(exc=exc, countdown=60 * add.request.retries) 

Note: First task is repeated with countdown of 0. Because number of retries is 0 for the first run.




回答3:


Keep a variable with your last retry time in it, and multiply it by 2 each time until it exceeds whatever level you want (or, keep a count if you prefer a certain number of times...)



来源:https://stackoverflow.com/questions/9470089/increase-celery-retry-time-each-retry-cycle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!