2017-08-31 12:52:06 +01:00
|
|
|
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)
|
2017-06-01 14:32:19 +01:00
|
|
|
|
2016-02-09 13:31:45 +00:00
|
|
|
|
|
|
|
|
class NotifyCelery(Celery):
|
2017-06-09 16:20:02 +01:00
|
|
|
|
2017-07-19 13:50:29 +01:00
|
|
|
def init_app(self, app):
|
2017-08-31 12:52:06 +01:00
|
|
|
super().__init__(
|
|
|
|
|
app.import_name,
|
|
|
|
|
broker=app.config['BROKER_URL'],
|
|
|
|
|
task_cls=NotifyTask,
|
|
|
|
|
)
|
2016-02-09 13:31:45 +00:00
|
|
|
|
2017-08-31 12:52:06 +01:00
|
|
|
self.conf.update(app.config)
|