I'm pretty sure this can only be done if I create my own task class, but I'd like to know if anyone else has found a way to do this.
You can provide the function directly to the decorator:
def fun(self, exc, task_id, args, kwargs, einfo):
print('Failed!')
@task(name="foo:my_task", on_failure=fun)
def add(x, y):
raise KeyError()
Here is a full 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:
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