-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcelery_worker.py
40 lines (33 loc) · 1.21 KB
/
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
from celery import Celery
# from celery.schedules import crontab
from apps import create_app
# from example.tasks import log, reverse_messages
def create_celery(app):
celery = Celery(app.import_name,
backend=app.config['CELERY_RESULT_BACKEND'],
broker=app.config['BROKER_URL'])
celery.conf.update(app.config)
TaskBase = celery.Task
class ContextTask(TaskBase):
abstract = True
def __call__(self, *args, **kwargs):
with app.app_context():
return TaskBase.__call__(self, *args, **kwargs)
celery.Task = ContextTask
return celery
flask_app = create_app()
celery = create_celery(flask_app)
#
# @celery.on_after_configure.connect
# def setup_periodic_tasks(sender, **kwargs):
# # Calls reverse_messages every 10 seconds.
# sender.add_periodic_task(10.0, reverse_messages, name='reverse every 10')
#
# # Calls log('Logging Stuff') every 30 seconds
# sender.add_periodic_task(30.0, log.s(('Logging Stuff')), name='Log every 30')
#
# # Executes every Monday morning at 7:30 a.m.
# sender.add_periodic_task(
# crontab(hour=7, minute=30, day_of_week=1),
# log.s('Monday morning log!'),
# )