Repeat Celery Exponential Jobs

For such a task:

from celery.decorators import task @task() def add(x, y): if not x or not y: raise Exception("test error") return self.wait_until_server_responds( 

if it throws an exception, and I want to repeat it from the side of the daemon, how to apply the exponential indent algorithm, i.e. after 2^2, 2^3,2^4 , etc. seconds?

It also retries from the server, so if the worker is killed, then the next worker that appears will perform the retry task?

+48
python celery django-celery
Mar 16 '12 at 3:28
source share
2 answers

The task.request.retries attribute contains the number of attempts so far, so you can use this to implement exponential deviation:

 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 the Thundering Herd problem, you might consider adding random jitter to the exponential bias:

 import random self.retry(exc=exc, countdown=int(random.uniform(2, 4) ** self.request.retries)) 
+89
Mar 17 2018-12-18T00:
source share

As with Celery 4.2 (not yet released), you can configure your tasks to automatically use the exponential response: 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): ... 

Do not be fooled by "New in version 4.1." ad in documents, it has not yet been issued, see merge request

+6
Sep 28 '17 at 11:19 on
source share



All Articles