芹菜任务不重试



我有一个简单的test_celery.py文件,其中包含以下内容,不会重新运行失败的任务:

from celery import Celery
import time
app = Celery(
    'test_celery',
    broker='amqp://',
    backend='amqp'
)
@app.task(retries=4)
def my_fail():
    try:
        raise Exception()
    except Exception as e:
      print('Tring: {0}/{1}'.format(my_fail.request.retries, my_fail.max_retries))
      # Print log message with current retry
      my_fail.retry(exc=e, max_retries=4, countdown=2)
if __name__ == '__main__':
    fail()
    print('All done!')

不幸的是,当我运行任务时,它只运行一次:

$ python test_celery.py
Tring: 0/3
Traceback (most recent call last):
  File "test_celery.py", line 20, in <module>
    my_fail()
  File "/Users/alexgray/.virtualenvs/clearcare/lib/python3.4/site-packages/celery/local.py", line 191, in __call__
    return self._get_current_object()(*a, **kw)
  File "/Users/alexgray/.virtualenvs/clearcare/lib/python3.4/site-packages/celery/app/task.py", line 380, in __call__
    return self.run(*args, **kwargs)
  File "test_celery.py", line 17, in my_fail
    my_fail.retry(exc=e, max_retries=4, countdown=2)
  File "/Users/alexgray/.virtualenvs/clearcare/lib/python3.4/site-packages/celery/app/task.py", line 653, in retry
    raise_with_context(exc or Retry('Task can be retried', None))
  File "test_celery.py", line 13, in my_fail
    raise Exception()
Exception

我确定我错过了一些明显的东西,但我不知道我做错了什么。

根据芹菜文档,我认为您想要的是实际提出重试调用。

def my_fail():
    try:
        raise Exception()
    except Exception as e:
      print('Tring: {0}/{1}'.format(my_fail.request.retries, my_fail.max_retries))
      # Print log message with current retry
      raise my_fail.retry(exc=e, max_retries=4, countdown=2)

请注意,我们引发了错误,而不仅仅是调用任务。

最新更新