2020-07-12 17:34:25 +02:00
|
|
|
# ContentDB
|
2018-05-17 16:18:20 +02:00
|
|
|
# Copyright (C) 2018 rubenwardy
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# 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
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# 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
|
|
|
|
2018-05-11 13:57:16 +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
|
|
|
|
|
2018-05-11 13:57:16 +02:00
|
|
|
|
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):
|
2020-12-07 19:06:34 +01:00
|
|
|
BaseTask : celery.Task = self.Task
|
2018-05-11 13:57:16 +02:00
|
|
|
_celery = self
|
|
|
|
|
2020-12-07 19:06:34 +01:00
|
|
|
class ContextTask(BaseTask):
|
2018-05-11 13:57:16 +02:00
|
|
|
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)
|
2018-05-11 13:57:16 +02:00
|
|
|
else:
|
|
|
|
with _celery.app.app_context():
|
2020-12-07 19:06:34 +01:00
|
|
|
return super(BaseTask, self).__call__(*args, **kwargs)
|
2018-05-11 13:57:16 +02:00
|
|
|
|
|
|
|
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-10-09 22:49:26 +02:00
|
|
|
CELERYBEAT_SCHEDULE = {
|
|
|
|
'topic_list_import': {
|
|
|
|
'task': 'app.tasks.forumtasks.importTopicList',
|
|
|
|
'schedule': crontab(minute=1, hour=1),
|
2019-11-21 23:16:35 +01:00
|
|
|
},
|
|
|
|
'package_score_update': {
|
|
|
|
'task': 'app.tasks.pkgtasks.updatePackageScores',
|
|
|
|
'schedule': crontab(minute=10, hour=1),
|
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',
|
2020-12-05 20:15:33 +01:00
|
|
|
'schedule': crontab(minute='*/5'),
|
2020-12-06 16:02:02 +01:00
|
|
|
},
|
|
|
|
'send_notification_digests': {
|
|
|
|
'task': 'app.tasks.emails.send_pending_digests',
|
|
|
|
'schedule': crontab(minute=0, hour=14),
|
2018-10-09 22:49:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
celery.conf.beat_schedule = CELERYBEAT_SCHEDULE
|
|
|
|
|
2020-12-07 19:06:34 +01:00
|
|
|
from . import importtasks, forumtasks, emails, pkgtasks, celery
|
|
|
|
|
|
|
|
|
|
|
|
# noinspection PyUnusedLocal
|
|
|
|
@signals.after_setup_logger.connect
|
|
|
|
def on_after_setup_logger(**kwargs):
|
|
|
|
from app.maillogger import build_handler
|
|
|
|
|
|
|
|
class ExceptionFilter(Filter):
|
|
|
|
def filter(self, record):
|
|
|
|
if record.exc_info:
|
|
|
|
exc, _, _ = record.exc_info
|
|
|
|
if exc == TaskError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
logger = celery.log.get_default_logger()
|
|
|
|
handler = build_handler(app)
|
|
|
|
handler.addFilter(ExceptionFilter())
|
|
|
|
logger.addHandler(handler)
|