2

I am using celery on_failure handler to logging all failed tasks for debugging and analysis. And I want to know the task name(function name) of the failed task, how can I get that?

from celery import Task

class DebugTask(Task):
    abstract = True

    def after_return(self, *args, **kwargs):
        print('Task returned: {0!r}'.format(self.request))

    def on_failure(self, exc, task_id, args, kwargs, einfo):
        func_name = get_func_name_by_task_id(task_id) # how do I do this?
        print "{} failed".format(func_name)           # expected out: add failed.

@app.task(base=DebugTask)
def add(x, y):
    return x + y

PS: I know there is task_id, but query function name by task_id every time is not fun,

cizixs
  • 12,931
  • 6
  • 48
  • 60

1 Answers1

4

Quick look at the documentation shows Task.name.

tuomur
  • 6,888
  • 34
  • 37