为此,您可以使用抽象类来实现on_failure
处理程序。
from celery import Task
class DebugTask(Task):
abstract = True
def on_failure(self, exc, task_id, args, kwargs, einfo):
logger.exception("Something happened when trying"
" to resolve %s" % args[0])
@tasks_app.task(base=DebugTask)
def resolve_hostname(hostname):
return (hostname, {hst.address for hst in dns.resolver.query(hostname)})
从文档:
on_failure(self, exc, task_id, args, kwargs, einfo)
Parameters:
exc – The exception raised by the task.
task_id – Unique id of the Failed task.
args – Original arguments for the task that Failed.
kwargs – Original keyword arguments for the task that Failed.
einfo – ExceptionInfo instance, containing the traceback.