]> git.lizzy.rs Git - cheatdb.git/blob - app/tasks/__init__.py
c431fae718a716e5ef62f3486811b4ec60dc6564
[cheatdb.git] / app / tasks / __init__.py
1 import flask
2 from flask.ext.sqlalchemy import SQLAlchemy
3 from celery import Celery
4 from app import app
5 from app.models import *
6
7 class FlaskCelery(Celery):
8         def __init__(self, *args, **kwargs):
9                 super(FlaskCelery, self).__init__(*args, **kwargs)
10                 self.patch_task()
11
12                 if 'app' in kwargs:
13                         self.init_app(kwargs['app'])
14
15         def patch_task(self):
16                 TaskBase = self.Task
17                 _celery = self
18
19                 class ContextTask(TaskBase):
20                         abstract = True
21
22                         def __call__(self, *args, **kwargs):
23                                 if flask.has_app_context():
24                                         return TaskBase.__call__(self, *args, **kwargs)
25                                 else:
26                                         with _celery.app.app_context():
27                                                 return TaskBase.__call__(self, *args, **kwargs)
28
29                 self.Task = ContextTask
30
31         def init_app(self, app):
32                 self.app = app
33                 self.config_from_object(app.config)
34
35 def make_celery(app):
36         celery = FlaskCelery(app.import_name, backend=app.config['CELERY_RESULT_BACKEND'],
37                                         broker=app.config['CELERY_BROKER_URL'])
38
39         celery.init_app(app)
40         return celery
41
42 celery = make_celery(app)
43
44 from . import importtasks