contentdb/app/tasks/__init__.py

110 lines
2.9 KiB
Python
Raw Normal View History

2020-07-12 17:34:25 +02:00
# ContentDB
2021-01-30 17:59:42 +01:00
# Copyright (C) 2018-21 rubenwardy
2018-05-17 16:18:20 +02:00
#
# This program is free software: you can redistribute it and/or modify
2021-01-30 17:59:42 +01:00
# it under the terms of the GNU Affero General Public License as published by
2018-05-17 16:18:20 +02:00
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2021-01-30 17:59:42 +01:00
# GNU Affero General Public License for more details.
2018-05-17 16:18:20 +02:00
#
2021-01-30 17:59:42 +01:00
# You should have received a copy of the GNU Affero General Public License
2018-05-17 16:18:20 +02:00
# along with this program. If not, see <https://www.gnu.org/licenses/>.
2020-12-07 19:06:34 +01:00
from logging import Filter
2018-05-17 16:18:20 +02:00
import flask
2020-12-07 19:06:34 +01:00
from celery import Celery, signals
2018-10-09 22:49:26 +02:00
from celery.schedules import crontab
2020-12-07 19:06:34 +01:00
from app import app
class TaskError(Exception):
def __init__(self, value):
self.value = value
2023-06-19 20:32:36 +02:00
def __str__(self):
return repr("TaskError: " + self.value)
2023-06-18 23:21:37 +02:00
class FlaskCelery(Celery):
2023-06-18 23:21:37 +02:00
app: flask.app
def __init__(self, *args, **kwargs):
super(FlaskCelery, self).__init__(*args, **kwargs)
2023-06-18 23:21:37 +02:00
self.app = None
self.patch_task()
if 'app' in kwargs:
self.init_app(kwargs['app'])
def patch_task(self):
2020-12-07 19:06:34 +01:00
BaseTask : celery.Task = self.Task
_celery = self
2020-12-07 19:06:34 +01:00
class ContextTask(BaseTask):
abstract = True
def __call__(self, *args, **kwargs):
if flask.has_app_context():
2020-12-07 19:06:34 +01:00
return super(BaseTask, self).__call__(*args, **kwargs)
else:
with _celery.app.app_context():
2020-12-07 19:06:34 +01:00
return super(BaseTask, self).__call__(*args, **kwargs)
self.Task = ContextTask
def init_app(self, app):
self.app = app
self.config_from_object(app.config)
2023-06-18 23:21:37 +02:00
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
2023-06-18 23:21:37 +02:00
celery = make_celery(app)
2023-06-18 23:21:37 +02:00
2018-10-09 22:49:26 +02:00
CELERYBEAT_SCHEDULE = {
'topic_list_import': {
2023-06-19 22:27:49 +02:00
'task': 'app.tasks.forumtasks.import_topic_list',
'schedule': crontab(minute=1, hour=1), # 0101
2019-11-21 23:16:35 +01:00
},
'package_score_update': {
2023-06-19 22:27:49 +02:00
'task': 'app.tasks.pkgtasks.update_package_scores',
'schedule': crontab(minute=10, hour=1), # 0110
2020-12-05 20:15:33 +01:00
},
2021-01-30 00:35:23 +01:00
'check_for_updates': {
'task': 'app.tasks.importtasks.check_for_updates',
2022-08-23 03:24:12 +02:00
'schedule': crontab(minute=10, hour=2), # 0210
},
2020-12-05 20:15:33 +01:00
'send_pending_notifications': {
2020-12-06 16:02:02 +01:00
'task': 'app.tasks.emails.send_pending_notifications',
'schedule': crontab(minute='*/5'), # every 5 minutes
2020-12-06 16:02:02 +01:00
},
'send_notification_digests': {
'task': 'app.tasks.emails.send_pending_digests',
'schedule': crontab(minute=0, hour=14), # 1400
},
'delete_inactive_users': {
'task': 'app.tasks.usertasks.delete_inactive_users',
'schedule': crontab(minute=15), # every hour at quarter past
},
2022-08-23 03:24:12 +02:00
'upgrade_new_members': {
'task': 'app.tasks.usertasks.upgrade_new_members',
'schedule': crontab(minute=10, hour=3), # 0310
},
2018-10-09 22:49:26 +02:00
}
celery.conf.beat_schedule = CELERYBEAT_SCHEDULE
2023-06-18 23:21:37 +02:00
from . import importtasks, forumtasks, emails, pkgtasks, usertasks