-
Notifications
You must be signed in to change notification settings - Fork 2
/
TaskServer.py
115 lines (103 loc) · 4.41 KB
/
TaskServer.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# -*- coding:utf-8 -*-
import traceback
import os
import tempfile
import simplejson
import subprocess, socket
import threading
import redis
import uuid
from functools import wraps
class TaskExecutor(object):
def __init__(self, task_name , *args, **kwargs):
self.queue = redis.StrictRedis()#host='localhost', port=6378, db=0, password='xxx_tasks')
self.task_name = task_name
self.stdout_file = tempfile.mktemp()
self.stderr_file = tempfile.mktemp()
def _publish_task(self, task_id , func, *args, **kwargs):
self.queue.lpush(self.task_name,
# simplejson.dumps({'id':task_id, 'func':func, 'args':args, 'kwargs':kwargs})
simplejson.dumps({'instance_id':uuid.uuid4().hex, 'task_id':task_id, 'execute_date':'201810', 'kwargs':kwargs})
)
def _publish_cmdtask(self, task_id , *args, **kwargs):
print(args[0])
self.queue.lpush(self.task_name,
simplejson.dumps({'instance_id':6, 'task_id':'task_demo', 'execute_date':'201810', 'kwargs':kwargs})
)
def task(self, func):#decorator
setattr(func,'delay',lambda *args, **kwargs:self._publish_task(uuid.uuid4().hex, func.__name__, *args, **kwargs))
@wraps(func)
def _w(*args, **kwargs):
return func(*args, **kwargs)
return _w
def cmd(self,func):
setattr(func,'delay',lambda *args, **kwargs:self._publish_cmdtask(uuid.uuid4().hex, *args, **kwargs))
@wraps(func)
def _w(*args, **kwargs):
if len(args):
cmd=args[0]
print(cmd)
return func(*args, **kwargs)
return _w
def _read_task_log(self, stream):
while True:
line = stream.readline()
if len(line) == 0:
break
print(str(line))
def run_command(self):
full_cmd = self.command
print('Running Command: [{}]'.format(full_cmd))
proc = subprocess.Popen(
full_cmd,
stdout=self.stdout_file,
stderr=self.stderr_file,
shell=True
)
# Start daemon thread to read subprocess log.loggerging output
# logger_reader = threading.Thread(
# target=self._read_task_log,
# args=(proc.stdout,),
# )
# logger_reader.daemon = True
# logger_reader.start()
return proc
def run(self):
print ('waiting for tasks...')
while True:
if self.queue.llen(self.task_name):
msg_data = simplejson.loads( self.queue.rpop(self.task_name))#这里可以用StrictRedis实例的brpop改善,去掉llen轮询。
print('msg_data:{0}'.format(msg_data))
print ('handling task(id:{0})...'.format(msg_data['id']))
try:
if msg_data.get('func',None):
func = eval(msg_data.get('func'))
if callable(func):
#print msg_data['args'], msg_data['kwargs']
ret = func(*msg_data['args'], **msg_data['kwargs'])
msg_data.update({'result':ret})
self.queue.lpush(self.task_name+'.response.success', simplejson.dumps(msg_data) )
elif msg_data.get('cmd',None):
self.command=msg_data.get('cmd',None)
print('run cmd:{0}'.format(self.command))
self.run_command()
msg_data.update({'result':"success"})
self.queue.lpush(self.task_name+'.response.success', simplejson.dumps(msg_data) )
except:
msg_data.update({'failed_times':msg_data.get('failed_times',0)+1, 'failed_reason':traceback.format_exc()})
if msg_data.get('failed_times',0)<10:#最多失败10次,避免死循环
self.queue.rpush(self.task_name,simplejson.dumps(msg_data))
else:
self.queue.lpush(self.task_name+'.response.failure', simplejson.dumps(msg_data) )
print (traceback.format_exc())
PingTask = TaskExecutor('bi_scheduler_dispatch')
@PingTask.task
def ping_url(url):
import os
os.system('ping -c 2 '+url)
@PingTask.cmd
def cmd_demo(cmd):
print('提交:{0}'.format(cmd))
if __name__=='__main__':
# PingTask.run()
cmd_demo.delay('ping')