-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
42 lines (31 loc) · 977 Bytes
/
tasks.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
41
42
from celery import Celery
from flask import Flask
from flask_mail import Message
from exts import mail, alidayu
import config
app = Flask(__name__)
app.config.from_object(config)
mail.init_app(app)
# 运行本文件:
# celery -A tasks.celery worker --loglevel=info
def make_celery(app):
celery = Celery(
app.import_name,
backend=app.config['CELERY_RESULT_BACKEND'],
broker=app.config['CELERY_BROKER_URL']
)
celery.conf.update(app.config)
class ContextTask(celery.Task):
def __call__(self, *args, **kwargs):
with app.app_context():
return self.run(*args, **kwargs)
celery.Task = ContextTask
return celery
celery = make_celery(app)
@celery.task
def send_mail(subject, recipients, body):
message = Message(subject=subject, recipients=recipients, body=body)
mail.send(message)
@celery.task
def send_sms_captcha(telephone,captcha):
alidayu.send_sms(telephone, code=captcha)