2018-05-11 13:57:16 +02:00
|
|
|
import flask
|
|
|
|
from flask.ext.sqlalchemy import SQLAlchemy
|
|
|
|
from celery import Celery
|
|
|
|
from app import app
|
|
|
|
from app.models import *
|
|
|
|
|
2018-05-14 02:20:02 +02:00
|
|
|
class TaskError(Exception):
|
|
|
|
def __init__(self, value):
|
|
|
|
self.value = value
|
|
|
|
def __str__(self):
|
|
|
|
return repr("TaskError: " + self.value)
|
|
|
|
|
2018-05-11 13:57:16 +02:00
|
|
|
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)
|
|
|
|
|
2018-05-14 01:40:34 +02:00
|
|
|
from . import importtasks, forumtasks, emails
|