Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log exception using autoretry in Celery tasks

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.

like image 330
coolusername Avatar asked Jan 31 '26 17:01

coolusername


1 Answers

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):
    ...
like image 184
Malik Faiq Avatar answered Feb 02 '26 06:02

Malik Faiq