From 2622866622cb331f79e208dcee389bca480012f1 Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Thu, 31 Aug 2017 12:52:06 +0100 Subject: [PATCH] log unhandled celery exceptions they were always caught locally by celery's base handler, however, we weren't logging them ourselves, which meant it wouldn't be put into the json logs that are sent to cloudwatch. --- app/celery/celery.py | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/app/celery/celery.py b/app/celery/celery.py index 183e50bd6..59d240d07 100644 --- a/app/celery/celery.py +++ b/app/celery/celery.py @@ -1,17 +1,28 @@ -from celery import Celery +from flask import current_app +from celery import Celery, Task + + +class NotifyTask(Task): + abstract = True + + def on_failure(self, exc, task_id, args, kwargs, einfo): + # ensure task will log exceptions to correct handlers + current_app.logger.exception('Celery task failed') + super().on_failure(exc, task_id, args, kwargs, einfo) + + def __call__(self, *args, **kwargs): + # ensure task has flask context to access config, logger, etc + with current_app.app_context(): + return super().__call__(*args, **kwargs) class NotifyCelery(Celery): def init_app(self, app): - super().__init__(app.import_name, broker=app.config['BROKER_URL']) + super().__init__( + app.import_name, + broker=app.config['BROKER_URL'], + task_cls=NotifyTask, + ) + self.conf.update(app.config) - TaskBase = self.Task - - class ContextTask(TaskBase): - abstract = True - - def __call__(self, *args, **kwargs): - with app.app_context(): - return TaskBase.__call__(self, *args, **kwargs) - self.Task = ContextTask