]> git.lizzy.rs Git - cheatdb.git/blob - app/tasks/__init__.py
Replace "Content DB" with "ContentDB"
[cheatdb.git] / app / tasks / __init__.py
1 # ContentDB
2 # Copyright (C) 2018  rubenwardy
3 #
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.
8 #
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.
13 #
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/>.
16
17
18 import flask
19 from flask_sqlalchemy import SQLAlchemy
20 from celery import Celery
21 from celery.schedules import crontab
22 from app import app
23 from app.models import *
24
25 class TaskError(Exception):
26         def __init__(self, value):
27                 self.value = value
28         def __str__(self):
29                 return repr("TaskError: " + self.value)
30
31 class FlaskCelery(Celery):
32         def __init__(self, *args, **kwargs):
33                 super(FlaskCelery, self).__init__(*args, **kwargs)
34                 self.patch_task()
35
36                 if 'app' in kwargs:
37                         self.init_app(kwargs['app'])
38
39         def patch_task(self):
40                 TaskBase = self.Task
41                 _celery = self
42
43                 class ContextTask(TaskBase):
44                         abstract = True
45
46                         def __call__(self, *args, **kwargs):
47                                 if flask.has_app_context():
48                                         return TaskBase.__call__(self, *args, **kwargs)
49                                 else:
50                                         with _celery.app.app_context():
51                                                 return TaskBase.__call__(self, *args, **kwargs)
52
53                 self.Task = ContextTask
54
55         def init_app(self, app):
56                 self.app = app
57                 self.config_from_object(app.config)
58
59 def make_celery(app):
60         celery = FlaskCelery(app.import_name, backend=app.config['CELERY_RESULT_BACKEND'],
61                                         broker=app.config['CELERY_BROKER_URL'])
62
63         celery.init_app(app)
64         return celery
65
66 celery = make_celery(app)
67
68 CELERYBEAT_SCHEDULE = {
69         'topic_list_import': {
70                 'task': 'app.tasks.forumtasks.importTopicList',
71                 'schedule': crontab(minute=1, hour=1),
72         },
73         'package_score_update': {
74                 'task': 'app.tasks.pkgtasks.updatePackageScores',
75                 'schedule': crontab(minute=10, hour=1),
76         }
77 }
78 celery.conf.beat_schedule = CELERYBEAT_SCHEDULE
79
80 from . import importtasks, forumtasks, emails, pkgtasks