From celery documentation,
If you want to automatically retry on any error, simply use:
@app.task(autoretry_for=(Exception,))
def x():
...
How do we log the exception that it retried for? It would easy to add a log if we have a try-except, for example
@app.task(bind=True, default_retry_delay=30 * 60) # retry in 30 minutes.
def add(self, x, y):
try:
something_raising()
except Exception as exc:
logger.info('Retry for exception %s', exc)
# overrides the default delay to retry after 1 minute
raise self.retry(exc=exc, countdown=60)
but I want to use autoretry so I can make use of celery's retry backoff that comes with autoretry and not implement my own countdown with try-except. Please help.
Add throws argument while declaring task, this will pop the exception on the screen and it can be changed to any expected exception you want to throw. For example RequestException, TimeoutException
@app.task(bind=True, throws=(Exception,), default_retry_delay=30 * 60) # retry in 30 minutes.
def add(self, x, y):
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With