diff options
author | rubenwardy <rw@rubenwardy.com> | 2018-05-11 12:57:16 +0100 |
---|---|---|
committer | rubenwardy <rw@rubenwardy.com> | 2018-05-11 12:57:16 +0100 |
commit | 5e44f3d64c2be1b890e393832efd9fb100adf2c0 (patch) | |
tree | ea4a1fd0ce7fe8ed9c97687f6afd8e18b0a393e4 /app/tasks/__init__.py | |
parent | a55b4f84ff8fa9774b1aa3a0363d848b9a9bd252 (diff) | |
download | cheatdb-5e44f3d64c2be1b890e393832efd9fb100adf2c0.tar.xz |
Move Github import to backend
Fixes #41
Diffstat (limited to 'app/tasks/__init__.py')
-rw-r--r-- | app/tasks/__init__.py | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/app/tasks/__init__.py b/app/tasks/__init__.py new file mode 100644 index 0000000..c431fae --- /dev/null +++ b/app/tasks/__init__.py @@ -0,0 +1,44 @@ +import flask +from flask.ext.sqlalchemy import SQLAlchemy +from celery import Celery +from app import app +from app.models import * + +class FlaskCelery(Celery): + def __init__(self, *args, **kwargs): + super(FlaskCelery, self).__init__(*args, **kwargs) + self.patch_task() + + if 'app' in kwargs: + self.init_app(kwargs['app']) + + def patch_task(self): + TaskBase = self.Task + _celery = self + + class ContextTask(TaskBase): + abstract = True + + def __call__(self, *args, **kwargs): + if flask.has_app_context(): + return TaskBase.__call__(self, *args, **kwargs) + else: + with _celery.app.app_context(): + return TaskBase.__call__(self, *args, **kwargs) + + self.Task = ContextTask + + def init_app(self, app): + self.app = app + self.config_from_object(app.config) + +def make_celery(app): + celery = FlaskCelery(app.import_name, backend=app.config['CELERY_RESULT_BACKEND'], + broker=app.config['CELERY_BROKER_URL']) + + celery.init_app(app) + return celery + +celery = make_celery(app) + +from . import importtasks |