芹菜任务没有重试

时间:2017-10-10 15:29:20

标签: python celery

我有一个简单的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

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

1 个答案:

答案 0 :(得分:1)

根据celery docs,我认为您想要的是实际提高重试次数。

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)

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