/
addreward.py
100 lines (84 loc) · 2.97 KB
/
addreward.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
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
from celery import Celery,platforms
from kombu import Exchange, Queue
from celery import task
from config import *
import json
import uuid
import datetime
import time
ONCE_CAPACITY = 1
app = Celery("srjob.reward", broker=amqp_url)
platforms.C_FORCE_ROOT = True
app.conf.update(
CELERY_TRACK_STARTED=True,
CELERY_TASK_SERIALIZER='json',
CELERY_ACCEPT_CONTENT=['json'], # Ignore other content
CELERY_RESULT_SERIALIZER='json',
CELERY_IMPORTS = ("addsendapp","preparesendapp","dosendapp","addJob","addsendmail","addsendmsg","addsendsms","addtask","custinfosync","custsync","doreward","dosendmail","dosendmsg","dosendsms","findJob","preparereward","preparesendmail","preparesendmsg","preparesendsms","sessionclose","tagsync","tasks","usercheck",)
)
diff_time = time.timezone
def utc_now():
return datetime.datetime.now() + datetime.timedelta(seconds=diff_time)
@app.task(name="srjob.reward.addreward")
def addreward(reward):
redisdb = ensure_redis()
task = json.loads(reward)
if "esttime" in task.keys():
esttime = task["esttime"]
if "pointnum" in task.keys():
pointnum = task["pointnum"]
if "sendtype" in task.keys():
sendtype = task["sendtype"]
if "couponid" in task.keys():
couponid = task["couponid"]
if "pointexdate" in task.keys():
pointexdate = task["pointexdate"]
if "taskid" in task.keys():
task_id = task["taskid"]
if "campaignid" in task.keys():
campaignid = task["campaignid"]
redisdb.hmset("reward:"+str(task_id),{
"_id": task_id,
"task": "reward",
"arguments": _decode_dict(task),
"campaignid":campaignid,
"esttime":esttime,
"creat_time": utc_now(),
"isenable":1,#1可用 0不可用
"prepare":0,#1数据已准备 0数据未准备
"status": "STARTED"
})
redisdb.rpush("reward","reward:"+str(task_id))
def _decode_dict(data):
rv = {}
for key, value in data.iteritems():
if isinstance(key, unicode):
key = key.encode('utf-8')
if isinstance(value, unicode):
value = value.encode('utf-8')
elif isinstance(value, list):
value = _decode_list(value)
elif isinstance(value, dict):
value = _decode_dict(value)
rv[key] = value
return rv
def _decode_list(data):
rv = []
for item in data:
if isinstance(item, unicode):
item = item.encode('utf-8')
elif isinstance(item, list):
item = _decode_list(item)
elif isinstance(item, dict):
item = _decode_dict(item)
rv.append(item)
return rv
if __name__ == "__main__":
# 使用sys.argv参数运行
# app.worker_main()
# 使用自定义参数运行
# --beat同时开启beat模式,即运行按计划发送task的实例
# 应确保全局只有一份同样的beat
app.worker_main(["worker","--loglevel=debug","-n","addreward.%h"])