-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblog_celery.py
More file actions
40 lines (29 loc) · 996 Bytes
/
blog_celery.py
File metadata and controls
40 lines (29 loc) · 996 Bytes
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
# @Time: 2022/3/3 4:29 下午
# @Author: Bruce
# @Description : celery的工具模型
from flask_mail import Message
from exts import mail
from celery import Celery
# 定义任务函数
def send_mail(recipient, subject, body):
message = Message(subject=subject, recipients=[recipient], body=body)
try:
mail.send(message)
return {"status": "success"}
except Exception as e:
return {"status": "fail"}
# 创建celery对象
def make_celery(app):
celery = Celery(app.import_name, backend=app.config['CELERY_RESULT_BACKEND'],
broker=app.config['CELERY_BROKER_URL'])
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
app.celery = celery
# 添加任务
celery.task(name='send_mail')(send_mail)
return celery