本文整理汇总了Python中celery.worker.job.TaskWrapper.on_failure方法的典型用法代码示例。如果您正苦于以下问题:Python TaskWrapper.on_failure方法的具体用法?Python TaskWrapper.on_failure怎么用?Python TaskWrapper.on_failure使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类celery.worker.job.TaskWrapper
的用法示例。
在下文中一共展示了TaskWrapper.on_failure方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_send_email
# 需要导入模块: from celery.worker.job import TaskWrapper [as 别名]
# 或者: from celery.worker.job.TaskWrapper import on_failure [as 别名]
def test_send_email(self):
from celery import conf
from celery.worker import job
old_mail_admins = job.mail_admins
old_enable_mails = conf.CELERY_SEND_TASK_ERROR_EMAILS
mail_sent = [False]
def mock_mail_admins(*args, **kwargs):
mail_sent[0] = True
job.mail_admins = mock_mail_admins
conf.CELERY_SEND_TASK_ERROR_EMAILS = True
try:
tw = TaskWrapper(mytask.name, gen_unique_id(), [1], {"f": "x"})
try:
raise KeyError("foo")
except KeyError:
einfo = ExceptionInfo(sys.exc_info())
tw.on_failure(einfo)
self.assertTrue(mail_sent[0])
mail_sent[0] = False
conf.CELERY_SEND_TASK_ERROR_EMAILS = False
tw.on_failure(einfo)
self.assertFalse(mail_sent[0])
finally:
job.mail_admins = old_mail_admins
conf.CELERY_SEND_TASK_ERROR_EMAILS = old_enable_mails
示例2: _test_on_failure
# 需要导入模块: from celery.worker.job import TaskWrapper [as 别名]
# 或者: from celery.worker.job.TaskWrapper import on_failure [as 别名]
def _test_on_failure(self, exception):
tid = gen_unique_id()
tw = TaskWrapper(mytask.name, tid, [4], {"f": "x"})
try:
raise exception
except Exception:
exc_info = ExceptionInfo(sys.exc_info())
logfh = StringIO()
tw.logger.handlers = []
tw.logger = setup_logger(logfile=logfh, loglevel=logging.INFO)
from celery import conf
conf.CELERY_SEND_TASK_ERROR_EMAILS = True
tw.on_failure(exc_info)
logvalue = logfh.getvalue()
self.assertIn(mytask.name, logvalue)
self.assertIn(tid, logvalue)
self.assertIn("ERROR", logvalue)
conf.CELERY_SEND_TASK_ERROR_EMAILS = False
示例3: test_on_failure
# 需要导入模块: from celery.worker.job import TaskWrapper [as 别名]
# 或者: from celery.worker.job.TaskWrapper import on_failure [as 别名]
def test_on_failure(self):
tid = gen_unique_id()
tw = TaskWrapper("cu.mytask", tid, mytask, [4], {"f": "x"})
try:
raise Exception("Inside unit tests")
except Exception:
exc_info = ExceptionInfo(sys.exc_info())
logfh = StringIO()
tw.logger.handlers = []
tw.logger = setup_logger(logfile=logfh, loglevel=logging.INFO)
from celery import conf
conf.SEND_CELERY_TASK_ERROR_EMAILS = True
tw.on_failure(exc_info, {"task_id": tid, "task_name": "cu.mytask"})
logvalue = logfh.getvalue()
self.assertTrue("cu.mytask" in logvalue)
self.assertTrue(tid in logvalue)
self.assertTrue("ERROR" in logvalue)
conf.SEND_CELERY_TASK_ERROR_EMAILS = False