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 = TaskRequest(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
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 = TaskRequest(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
def test_send_email(self): from celery.loaders import current_loader loader = current_loader() old_mail_admins = loader.mail_admins old_enable_mails = mytask.send_error_emails mail_sent = [False] def mock_mail_admins(*args, **kwargs): mail_sent[0] = True loader.mail_admins = mock_mail_admins mytask.send_error_emails = True try: tw = TaskRequest(mytask.name, gen_unique_id(), [1], {"f": "x"}) try: raise KeyError("moofoobar") except: einfo = ExceptionInfo(sys.exc_info()) tw.on_failure(einfo) self.assertTrue(mail_sent[0]) mail_sent[0] = False mytask.send_error_emails = False tw.on_failure(einfo) self.assertFalse(mail_sent[0]) finally: loader.mail_admins = old_mail_admins mytask.send_error_emails = old_enable_mails
def test_send_email(self): app = app_or_default() old_mail_admins = app.mail_admins old_enable_mails = mytask.send_error_emails mail_sent = [False] def mock_mail_admins(*args, **kwargs): mail_sent[0] = True app.mail_admins = mock_mail_admins mytask.send_error_emails = True try: tw = TaskRequest(mytask.name, gen_unique_id(), [1], {"f": "x"}) try: raise KeyError("moofoobar") except: einfo = ExceptionInfo(sys.exc_info()) tw.on_failure(einfo) self.assertTrue(mail_sent[0]) mail_sent[0] = False mytask.send_error_emails = False tw.on_failure(einfo) self.assertFalse(mail_sent[0]) finally: app.mail_admins = old_mail_admins mytask.send_error_emails = old_enable_mails
def test_send_email(self): from celery.loaders import current_loader loader = current_loader() old_mail_admins = loader.mail_admins old_enable_mails = mytask.send_error_emails mail_sent = [False] def mock_mail_admins(*args, **kwargs): mail_sent[0] = True loader.mail_admins = mock_mail_admins mytask.send_error_emails = True try: tw = TaskRequest(mytask.name, gen_unique_id(), [1], {"f": "x"}) try: raise KeyError("moofoobar") except: einfo = ExceptionInfo(sys.exc_info()) tw.on_failure(einfo) self.assertTrue(mail_sent[0]) mail_sent[0] = False mytask.send_error_emails = False tw.on_failure(einfo) self.assertFalse(mail_sent[0]) finally: loader.mail_admins = old_mail_admins mytask.send_error_emails = old_enable_mails
def test_on_failure_WorkerLostError(self): def get_ei(): try: raise WorkerLostError('do re mi') except WorkerLostError: return ExceptionInfo() job = TaskRequest( self.mytask.name, uuid(), [1], {'f': 'x'}, app=self.app, ) exc_info = get_ei() job.on_failure(exc_info) self.assertEqual( self.mytask.backend.get_status(job.id), states.FAILURE, ) self.mytask.ignore_result = True exc_info = get_ei() job = TaskRequest( self.mytask.name, uuid(), [1], {'f': 'x'}, app=self.app, ) job.on_failure(exc_info) self.assertEqual( self.mytask.backend.get_status(job.id), states.PENDING, )
def test_on_retry(self): tw = TaskRequest(mytask.name, uuid(), [1], {"f": "x"}) tw.eventer = MockEventDispatcher() try: raise RetryTaskError("foo", KeyError("moofoobar")) except: einfo = ExceptionInfo(sys.exc_info()) tw.on_failure(einfo) self.assertIn("task-retried", tw.eventer.sent)
def test_on_retry(self): tw = TaskRequest(mytask.name, uuid(), [1], {"f": "x"}) tw.eventer = MockEventDispatcher() try: raise RetryTaskError("foo", KeyError("moofoobar")) except: einfo = ExceptionInfo(sys.exc_info()) tw.on_failure(einfo) self.assertIn("task-retried", tw.eventer.sent)
def test_on_failure_acks_late(self): job = TaskRequest(self.mytask.name, uuid(), [1], {"f": "x"}, app=self.app) job.time_start = 1 self.mytask.acks_late = True try: raise KeyError("foo") except KeyError: exc_info = ExceptionInfo() job.on_failure(exc_info) self.assertTrue(job.acknowledged)
def test_on_failure_acks_late(self): tw = TaskRequest(mytask.name, uuid(), [1], {"f": "x"}) tw.time_start = 1 mytask.acks_late = True try: try: raise KeyError("foo") except KeyError: exc_info = ExceptionInfo(sys.exc_info()) tw.on_failure(exc_info) self.assertTrue(tw.acknowledged) finally: mytask.acks_late = False
def test_on_failure_acks_late(self): tw = TaskRequest(mytask.name, uuid(), [1], {'f': 'x'}, app=self.app) tw.time_start = 1 mytask.acks_late = True try: try: raise KeyError('foo') except KeyError: exc_info = ExceptionInfo() tw.on_failure(exc_info) self.assertTrue(tw.acknowledged) finally: mytask.acks_late = False
def test_on_failure_acks_late(self): tw = TaskRequest(mytask.name, uuid(), [1], {"f": "x"}) tw.time_start = 1 mytask.acks_late = True try: try: raise KeyError("foo") except KeyError: exc_info = ExceptionInfo(sys.exc_info()) tw.on_failure(exc_info) self.assertTrue(tw.acknowledged) finally: mytask.acks_late = False
def test_on_failure_acks_late(self): tw = TaskRequest(mytask.name, uuid(), [1], {'f': 'x'}, app=self.app) tw.time_start = 1 mytask.acks_late = True try: try: raise KeyError('foo') except KeyError: exc_info = ExceptionInfo() tw.on_failure(exc_info) self.assertTrue(tw.acknowledged) finally: mytask.acks_late = False
def _test_on_failure(self, exception, logger): app = self.app tid = uuid() job = TaskRequest(self.mytask.name, tid, [4], {'f': 'x'}, app=self.app) try: raise exception except Exception: exc_info = ExceptionInfo() app.conf.CELERY_SEND_TASK_ERROR_EMAILS = True job.on_failure(exc_info) self.assertTrue(logger.log.called) context = logger.log.call_args[0][2] self.assertEqual(self.mytask.name, context['name']) self.assertIn(tid, context['id'])
def test_on_failure_WorkerLostError(self): tw = TaskRequest(mytask.name, uuid(), [1], {"f": "x"}) try: raise WorkerLostError("do re mi") except WorkerLostError: exc_info = ExceptionInfo(sys.exc_info()) tw.on_failure(exc_info) self.assertEqual(mytask.backend.get_status(tw.task_id), states.FAILURE) mytask.ignore_result = True try: tw = TaskRequest(mytask.name, uuid(), [1], {"f": "x"}) tw.on_failure(exc_info) self.assertEqual(mytask.backend.get_status(tw.task_id), states.PENDING) finally: mytask.ignore_result = False
def _test_on_failure(self, exception, logger): app = self.app tid = uuid() tw = TaskRequest(mytask.name, tid, [4], {'f': 'x'}, app=self.app) try: raise exception except Exception: exc_info = ExceptionInfo() app.conf.CELERY_SEND_TASK_ERROR_EMAILS = True try: tw.on_failure(exc_info) self.assertTrue(logger.log.called) context = logger.log.call_args[0][2] self.assertEqual(mytask.name, context['name']) self.assertIn(tid, context['id']) finally: app.conf.CELERY_SEND_TASK_ERROR_EMAILS = False
def _test_on_failure(self, exception, logger): app = app_or_default() tid = uuid() tw = TaskRequest(mytask.name, tid, [4], {"f": "x"}) try: raise exception except Exception: exc_info = ExceptionInfo() app.conf.CELERY_SEND_TASK_ERROR_EMAILS = True try: tw.on_failure(exc_info) self.assertTrue(logger.log.called) context = logger.log.call_args[0][2] self.assertEqual(mytask.name, context["name"]) self.assertIn(tid, context["id"]) finally: app.conf.CELERY_SEND_TASK_ERROR_EMAILS = False
def _test_on_failure(self, exception, logger): app = app_or_default() tid = uuid() tw = TaskRequest(mytask.name, tid, [4], {"f": "x"}) try: raise exception except Exception: exc_info = ExceptionInfo() app.conf.CELERY_SEND_TASK_ERROR_EMAILS = True try: tw.on_failure(exc_info) self.assertTrue(logger.log.called) context = logger.log.call_args[0][2] self.assertEqual(mytask.name, context["name"]) self.assertIn(tid, context["id"]) finally: app.conf.CELERY_SEND_TASK_ERROR_EMAILS = False
def test_on_failure_WorkerLostError(self): tw = TaskRequest(mytask.name, gen_unique_id(), [1], {"f": "x"}) try: raise WorkerLostError("do re mi") except WorkerLostError: exc_info = ExceptionInfo(sys.exc_info()) tw.on_failure(exc_info) self.assertEqual(mytask.backend.get_status(tw.task_id), states.FAILURE) mytask.ignore_result = True try: tw = TaskRequest(mytask.name, gen_unique_id(), [1], {"f": "x"}) tw.on_failure(exc_info) self.assertEqual(mytask.backend.get_status(tw.task_id), states.PENDING) finally: mytask.ignore_result = False
def test_on_success_when_failure(self): tw = TaskRequest(mytask.name, uuid(), [1], {"f": "x"}) tw.time_start = 1 tw.on_failure = Mock() try: raise KeyError("foo") except Exception: tw.on_success(ExceptionInfo(sys.exc_info())) self.assertTrue(tw.on_failure.called)
def test_on_success_when_failure(self): job = TaskRequest(self.mytask.name, uuid(), [1], {"f": "x"}, app=self.app) job.time_start = 1 job.on_failure = Mock() try: raise KeyError("foo") except Exception: job.on_success(ExceptionInfo()) self.assertTrue(job.on_failure.called)
def test_on_success_when_failure(self): tw = TaskRequest(mytask.name, uuid(), [1], {"f": "x"}) tw.time_start = 1 tw.on_failure = Mock() try: raise KeyError("foo") except Exception: tw.on_success(ExceptionInfo()) self.assertTrue(tw.on_failure.called)
def test_on_success_when_failure(self): tw = TaskRequest(mytask.name, uuid(), [1], {'f': 'x'}, app=self.app) tw.time_start = 1 tw.on_failure = Mock() try: raise KeyError('foo') except Exception: tw.on_success(ExceptionInfo()) self.assertTrue(tw.on_failure.called)
def test_on_failure_WorkerLostError(self): def get_ei(): try: raise WorkerLostError("do re mi") except WorkerLostError: return ExceptionInfo() tw = TaskRequest(mytask.name, uuid(), [1], {"f": "x"}) exc_info = get_ei() tw.on_failure(exc_info) self.assertEqual(mytask.backend.get_status(tw.id), states.FAILURE) mytask.ignore_result = True try: exc_info = get_ei() tw = TaskRequest(mytask.name, uuid(), [1], {"f": "x"}) tw.on_failure(exc_info) self.assertEqual(mytask.backend.get_status(tw.id), states.PENDING) finally: mytask.ignore_result = False
def test_send_email(self): app = self.app mail_sent = [False] def mock_mail_admins(*args, **kwargs): mail_sent[0] = True def get_ei(): try: raise KeyError('moofoobar') except: return ExceptionInfo() app.mail_admins = mock_mail_admins self.mytask.send_error_emails = True job = TaskRequest( self.mytask.name, uuid(), [1], {'f': 'x'}, app=self.app, ) einfo = get_ei() job.on_failure(einfo) self.assertTrue(mail_sent[0]) einfo = get_ei() mail_sent[0] = False self.mytask.send_error_emails = False job.on_failure(einfo) self.assertFalse(mail_sent[0]) einfo = get_ei() mail_sent[0] = False self.mytask.send_error_emails = True job.on_failure(einfo) self.assertTrue(mail_sent[0])
def _test_on_failure(self, exception): tid = gen_unique_id() tw = TaskRequest(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
def _test_on_failure(self, exception): tid = gen_unique_id() tw = TaskRequest(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
def _test_on_failure(self, exception): app = app_or_default() tid = uuid() tw = TaskRequest(mytask.name, tid, [4], {"f": "x"}) try: raise exception except Exception: exc_info = ExceptionInfo(sys.exc_info()) logfh = WhateverIO() tw.logger.handlers = [] tw.logger = app.log.setup_logger("INFO", logfh, root=False) app.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) app.conf.CELERY_SEND_TASK_ERROR_EMAILS = False
def test_send_email(self): app = app_or_default() old_mail_admins = app.mail_admins old_enable_mails = mytask.send_error_emails mail_sent = [False] def mock_mail_admins(*args, **kwargs): mail_sent[0] = True def get_ei(): try: raise KeyError("moofoobar") except: return ExceptionInfo(sys.exc_info()) app.mail_admins = mock_mail_admins mytask.send_error_emails = True try: tw = TaskRequest(mytask.name, uuid(), [1], {"f": "x"}) einfo = get_ei() tw.on_failure(einfo) self.assertTrue(mail_sent[0]) einfo = get_ei() mail_sent[0] = False mytask.send_error_emails = False tw.on_failure(einfo) self.assertFalse(mail_sent[0]) einfo = get_ei() mail_sent[0] = False mytask.send_error_emails = True mytask.error_whitelist = [KeyError] tw.on_failure(einfo) self.assertTrue(mail_sent[0]) einfo = get_ei() mail_sent[0] = False mytask.send_error_emails = True mytask.error_whitelist = [SyntaxError] tw.on_failure(einfo) self.assertFalse(mail_sent[0]) finally: app.mail_admins = old_mail_admins mytask.send_error_emails = old_enable_mails mytask.error_whitelist = ()
def test_send_email(self): app = app_or_default() old_mail_admins = app.mail_admins old_enable_mails = mytask.send_error_emails mail_sent = [False] def mock_mail_admins(*args, **kwargs): mail_sent[0] = True def get_ei(): try: raise KeyError("moofoobar") except: return ExceptionInfo(sys.exc_info()) app.mail_admins = mock_mail_admins mytask.send_error_emails = True try: tw = TaskRequest(mytask.name, uuid(), [1], {"f": "x"}) einfo = get_ei() tw.on_failure(einfo) self.assertTrue(mail_sent[0]) einfo = get_ei() mail_sent[0] = False mytask.send_error_emails = False tw.on_failure(einfo) self.assertFalse(mail_sent[0]) einfo = get_ei() mail_sent[0] = False mytask.send_error_emails = True mytask.error_whitelist = [KeyError] tw.on_failure(einfo) self.assertTrue(mail_sent[0]) einfo = get_ei() mail_sent[0] = False mytask.send_error_emails = True mytask.error_whitelist = [SyntaxError] tw.on_failure(einfo) self.assertFalse(mail_sent[0]) finally: app.mail_admins = old_mail_admins mytask.send_error_emails = old_enable_mails mytask.error_whitelist = ()
def _test_on_failure(self, exception): app = app_or_default() tid = uuid() tw = TaskRequest(mytask.name, tid, [4], {"f": "x"}) try: raise exception except Exception: exc_info = ExceptionInfo(sys.exc_info()) logfh = WhateverIO() tw.logger.handlers = [] tw.logger = setup_logger(logfile=logfh, loglevel=logging.INFO, root=False) app.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) app.conf.CELERY_SEND_TASK_ERROR_EMAILS = False
def test_on_failure_WorkerLostError(self): def get_ei(): try: raise WorkerLostError('do re mi') except WorkerLostError: return ExceptionInfo() tw = TaskRequest(mytask.name, uuid(), [1], {'f': 'x'}) exc_info = get_ei() tw.on_failure(exc_info) self.assertEqual(mytask.backend.get_status(tw.id), states.FAILURE) mytask.ignore_result = True try: exc_info = get_ei() tw = TaskRequest(mytask.name, uuid(), [1], {'f': 'x'}) tw.on_failure(exc_info) self.assertEqual(mytask.backend.get_status(tw.id), states.PENDING) finally: mytask.ignore_result = False
def test_on_retry(self): job = TaskRequest(self.mytask.name, uuid(), [1], {"f": "x"}, app=self.app) job.eventer = MockEventDispatcher() try: raise Retry("foo", KeyError("moofoobar")) except: einfo = ExceptionInfo() job.on_failure(einfo) self.assertIn("task-retried", job.eventer.sent) prev, module._does_info = module._does_info, False try: job.on_failure(einfo) finally: module._does_info = prev einfo.internal = True job.on_failure(einfo)
def test_on_retry(self): tw = TaskRequest(mytask.name, uuid(), [1], {'f': 'x'}) tw.eventer = MockEventDispatcher() try: raise RetryTaskError('foo', KeyError('moofoobar')) except: einfo = ExceptionInfo() tw.on_failure(einfo) self.assertIn('task-retried', tw.eventer.sent) prev, module._does_info = module._does_info, False try: tw.on_failure(einfo) finally: module._does_info = prev einfo.internal = True tw.on_failure(einfo)
def test_on_retry(self): tw = TaskRequest(mytask.name, uuid(), [1], {"f": "x"}) tw.eventer = MockEventDispatcher() try: raise RetryTaskError("foo", KeyError("moofoobar")) except: einfo = ExceptionInfo() tw.on_failure(einfo) self.assertIn("task-retried", tw.eventer.sent) prev, module._does_info = module._does_info, False try: tw.on_failure(einfo) finally: module._does_info = prev einfo.internal = True tw.on_failure(einfo)
def test_on_retry(self): tw = TaskRequest(mytask.name, uuid(), [1], {'f': 'x'}, app=self.app) tw.eventer = MockEventDispatcher() try: raise RetryTaskError('foo', KeyError('moofoobar')) except: einfo = ExceptionInfo() tw.on_failure(einfo) self.assertIn('task-retried', tw.eventer.sent) prev, module._does_info = module._does_info, False try: tw.on_failure(einfo) finally: module._does_info = prev einfo.internal = True tw.on_failure(einfo)
def test_send_email(self): app = self.app old_mail_admins = app.mail_admins old_enable_mails = mytask.send_error_emails mail_sent = [False] def mock_mail_admins(*args, **kwargs): mail_sent[0] = True def get_ei(): try: raise KeyError('moofoobar') except: return ExceptionInfo() app.mail_admins = mock_mail_admins mytask.send_error_emails = True try: tw = TaskRequest(mytask.name, uuid(), [1], {'f': 'x'}, app=self.app) einfo = get_ei() tw.on_failure(einfo) self.assertTrue(mail_sent[0]) einfo = get_ei() mail_sent[0] = False mytask.send_error_emails = False tw.on_failure(einfo) self.assertFalse(mail_sent[0]) einfo = get_ei() mail_sent[0] = False mytask.send_error_emails = True tw.on_failure(einfo) self.assertTrue(mail_sent[0]) finally: app.mail_admins = old_mail_admins mytask.send_error_emails = old_enable_mails
def test_send_email(self): app = self.app old_mail_admins = app.mail_admins old_enable_mails = mytask.send_error_emails mail_sent = [False] def mock_mail_admins(*args, **kwargs): mail_sent[0] = True def get_ei(): try: raise KeyError('moofoobar') except: return ExceptionInfo() app.mail_admins = mock_mail_admins mytask.send_error_emails = True try: tw = TaskRequest(mytask.name, uuid(), [1], {'f': 'x'}, app=self.app) einfo = get_ei() tw.on_failure(einfo) self.assertTrue(mail_sent[0]) einfo = get_ei() mail_sent[0] = False mytask.send_error_emails = False tw.on_failure(einfo) self.assertFalse(mail_sent[0]) einfo = get_ei() mail_sent[0] = False mytask.send_error_emails = True tw.on_failure(einfo) self.assertTrue(mail_sent[0]) finally: app.mail_admins = old_mail_admins mytask.send_error_emails = old_enable_mails