Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Middleware for celery tasks

Tags:

django

celery

Currently we have a large number of celery tasks we use in our application, we have an exception which might raise on a large number of tasks.

We could create a try and catch block in each task and handle this specific exception accordingly, yet am searching for a way to catch any exception from any task (like a middleware line) for ease of maintenance.

Can any one advise?

We are using following versions:

celery==3.1.18
django-celery==3.1.16
Django==1.6.5
like image 343
Mo J. Mughrabi Avatar asked Jun 04 '26 15:06

Mo J. Mughrabi


1 Answers

This can be done using a base abstract task handler. For exceptions in particular there is a on_failure handler.

from celery import Task

class MyBaseTask(Task):
    abstract = True

    def on_failure(self, exc, task_id, args, kwargs, einfo):
        # Task failed. What do you want to do?
        print('Task raised an exception: {}'.format(exc)

@app.task(base=MyBaseTask)
def my_task():
    # Your task code

See the Celery docs for more info on the available handlers and the arguments which are passed to each one: http://docs.celeryproject.org/en/latest/userguide/tasks.html#abstract-classes

like image 118
Mark Lavin Avatar answered Jun 07 '26 14:06

Mark Lavin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!