-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtasks.py
More file actions
71 lines (57 loc) · 1.55 KB
/
tasks.py
File metadata and controls
71 lines (57 loc) · 1.55 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import time
from celery import Celery
# from app import configured_app
from marrow.mailer import Mailer
import secret
from config import admin_mail
celery = Celery('tasks', backend='redis://localhost', broker='redis://localhost')
# app = configured_app()
def configured_mailer():
config = {
# 'manager.use': 'futures',
'transport.debug': True,
'transport.timeout': 1,
'transport.use': 'smtp',
'transport.host': 'smtp.exmail.qq.com',
'transport.port': 465,
'transport.tls': 'ssl',
'transport.username': admin_mail,
'transport.password': secret.mail_password,
}
m = Mailer(config)
m.start()
return m
mailer = configured_mailer()
@celery.task
def add(x, y):
import time
time.sleep(30)
return x + y
@celery.task
def send_async_simple(subject, author, to, plain):
m = mailer.new(
subject=subject,
author=author,
to=to,
)
m.plain = plain
mailer.send(m)
time.sleep(10)
@celery.task(bind=True)
def send_async(self, subject, author, to, plain):
# 有了 bind 才能去拿到 self 参数
# 这样才能去通过 self 调用当前 task 的一些功能
# 比如重试
try:
m = mailer.new(
subject=subject,
author=author,
to=to,
)
m.plain = plain
mailer.send(m)
time.sleep(10)
raise ValueError('tetest')
except Exception as exc:
# 3秒重试一次 最多重试5次
raise self.retry(exc=exc, countdown=3, max_retries=5)