-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrockettm.py
79 lines (67 loc) · 2.05 KB
/
rockettm.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
import logging
import uuid
import traceback
import time
from redisqueue import RedisQueue
from redis import Redis
class tasks(object):
subs = {}
ip = "localhost"
conn = False
producer = False
serializer = "json"
# deprecated
@staticmethod
def connect(ip='localhost'):
if ip != 'localhost':
tasks.ip = ip
tasks.conn = RedisQueue(Redis(host=tasks.ip),
serializer=tasks.serializer)
logging.warning('connected redis')
@staticmethod
def add_task(event, func, max_time=-1):
logging.info("add task %s" % event)
if event not in tasks.subs:
tasks.subs[event] = []
tasks.subs[event].append((func, max_time))
@staticmethod
def task(event, max_time=-1):
def wrap_function(func):
tasks.add_task(event, func, max_time)
return func
return wrap_function
@staticmethod
def send_task(queue_name, event, *args, **kwargs):
if 'rocket_id' in kwargs:
_id = kwargs.pop('rocket_id')
else:
_id = str(uuid.uuid4())
args = list((_id,) + args)
logging.info("send task to queue %s, event %s" % (queue_name, event))
if not tasks.conn:
tasks.connect()
send_ok = False
for retry in range(10):
try:
tasks.conn.put({'event': event,
'args': args,
'kwargs': kwargs},
queue_name)
send_ok = True
break
except:
logging.error(traceback.format_exc())
time.sleep(retry * 1.34)
tasks.connect()
print(traceback.format_exc())
if send_ok:
logging.info("send ok!")
return _id
else:
logging.error("send Failed")
return False
# avoids having to import tasks
connect = tasks.connect
send_task = tasks.send_task
add_task = tasks.add_task
task = tasks.task