Can I add an on_failure callback to a celery task created using a task decorator?

I am sure that this can only be done if I create my own class of tasks, but I would like to know if anyone else has found a way to do this.

+8
celery
source share
2 answers
+4
source share

To expand on the accepted answer, here is a complete solution (works for Celery 4+ ):

 import celery from celery.task import task class MyBaseClassForTask(celery.Task): def on_failure(self, exc, task_id, args, kwargs, einfo): # exc (Exception) - The exception raised by the task. # args (Tuple) - Original arguments for the task that failed. # kwargs (Dict) - Original keyword arguments for the task that failed. print('{0!r} failed: {1!r}'.format(task_id, exc)) @task(name="foo:my_task", base=MyBaseClassForTask) def add(x, y): raise KeyError() 

Resources

+8
source share

All Articles