2017-10-10 95 views
0

我有沒有被重新運行失敗的任務包含以下內容的簡單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

根據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) 

請注意,我們提出的錯誤,而不是僅僅調用任務。

+0

你說得對。我也必須在main中調用它,像my_fail.delay(),而不是my_fail() – grayaii