2 # Copyright (C) 2018 rubenwardy
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
19 from flask_sqlalchemy import SQLAlchemy
20 from celery import Celery
21 from celery.schedules import crontab
23 from app.models import *
25 class TaskError(Exception):
26 def __init__(self, value):
29 return repr("TaskError: " + self.value)
31 class FlaskCelery(Celery):
32 def __init__(self, *args, **kwargs):
33 super(FlaskCelery, self).__init__(*args, **kwargs)
37 self.init_app(kwargs['app'])
43 class ContextTask(TaskBase):
46 def __call__(self, *args, **kwargs):
47 if flask.has_app_context():
48 return TaskBase.__call__(self, *args, **kwargs)
50 with _celery.app.app_context():
51 return TaskBase.__call__(self, *args, **kwargs)
53 self.Task = ContextTask
55 def init_app(self, app):
57 self.config_from_object(app.config)
60 celery = FlaskCelery(app.import_name, backend=app.config['CELERY_RESULT_BACKEND'],
61 broker=app.config['CELERY_BROKER_URL'])
66 celery = make_celery(app)
68 CELERYBEAT_SCHEDULE = {
69 'topic_list_import': {
70 'task': 'app.tasks.forumtasks.importTopicList',
71 'schedule': crontab(minute=1, hour=1),
73 'package_score_update': {
74 'task': 'app.tasks.pkgtasks.updatePackageScores',
75 'schedule': crontab(minute=10, hour=1),
78 celery.conf.beat_schedule = CELERYBEAT_SCHEDULE
80 from . import importtasks, forumtasks, emails, pkgtasks