mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-05 02:41:14 -05:00
Merge pull request #1224 from alphagov/celery_logging
log unhandled celery exceptions
This commit is contained in:
@@ -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):
|
class NotifyCelery(Celery):
|
||||||
|
|
||||||
def init_app(self, app):
|
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)
|
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
|
|
||||||
|
|||||||
Reference in New Issue
Block a user