-
Notifications
You must be signed in to change notification settings - Fork 0
/
celery_worker.py
41 lines (28 loc) · 982 Bytes
/
celery_worker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Celery quiz
from abc import ABC
from celery import current_app
from celery.schedules import crontab
from celery.app.task import Task as BaseTask
from quiz.app import create_app
from quiz.task import task_conf, celery
from quiz.task.persist import persist_answer_votes
def make_celery(celery_app, flask_app):
"""Start the celery with the flask context"""
celery_app.config_from_object(task_conf)
class AppContextTask(BaseTask, ABC):
def __call__(self, *args, **kwargs):
"""Execute task"""
with flask_app.app_context():
return self.run(*args, **kwargs)
current_app.Task = AppContextTask
return celery_app
app = create_app()
make_celery(celery, app)
@celery.on_after_configure.connect
def setup_periodic_tasks(sender, **kwargs):
sender.add_periodic_task(
crontab(minute=0,
hour='*/3'),
persist_answer_votes.s(),
name='persist answer votes per 3 hours'
)