-
Notifications
You must be signed in to change notification settings - Fork 32
/
control.py
executable file
·276 lines (222 loc) · 8.63 KB
/
control.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#coding=UTF-8
import redis
import zerorpc
from config import *
from sutils import queues
#节点信息
class NodeInfo():
def __init__(self):
self.pool = redis.ConnectionPool(host=RTASK_REDIS_HOST, port=RTASK_REDIS_POST, db=RTASK_REDIS_DB, password=RTASK_REDIS_PWD, encoding='utf-8', decode_responses=True)
self.client = redis.StrictRedis(connection_pool=self.pool)
def node_list(self):
nodes = self.client.hgetall(TASK_NAME+':task_nodes')
node_lists = list()
for k, v in nodes.items():
#macid = k.decode('UTF-8')
data = eval(v)
rpcip = None
ips = data['ips']
for ip in ips:
if self.rpcconn_check(ip):
rpcip = ip
break
data['rpcip'] = "tcp://{ip}:{port}".format(ip=rpcip, port=RPC_PORT)
node_lists.append(data)
return node_lists
def node_search(self, macid):
node = self.client.hget(TASK_NAME+':task_nodes', macid)
node = eval(node)
rpcip = None
ips = node['ips']
for ip in ips:
if self.rpcconn_check(ip):
rpcip = ip
break
node['rpcip'] = "tcp://{ip}:{port}".format(ip=rpcip, port=RPC_PORT)
return node
def rpcconn_check(self, ip):
try:
rpc = zerorpc.Client(timeout=3, heartbeat=3)
rpc.connect("tcp://{ip}:{port}".format(ip=ip, port=RPC_PORT))
ping = rpc.ping()
rpc.close()
return ping
except Exception as e:
return False
def task_list(self):
tasks = self.client.hgetall(TASK_NAME+':task_status')
node_lists = self.node_list()
macid_rpcip = dict()
for node in node_lists:
macid_rpcip[node['macid']] = node['rpcip']
task_lists = list()
for k, v in tasks.items():
data = eval(v)
data['rpcip'] = macid_rpcip[data['macid']]
task_lists.append(data)
task_lists.sort(key=lambda x:x['macid'])
return task_lists
def node_tasks(self, macid):
nodes = self.client.hget(TASK_NAME+':task_nodes', macid)
node_dict = eval(nodes)
tasks = node_dict['tasks']
task_lists = list()
for t in tasks:
task = self.client.hget(TASK_NAME+':task_status', t)
task = eval(task)
task_lists.append(task)
return task_lists
def start_alltasks(self):
alltasks = self.client.hgetall(TASK_NAME+':task_status')
for task_uuid, task_data in alltasks.items():
task_uuid = task_uuid
task_data = eval(task_data)
status = task_data['status']
if status == 'stop':
task_data['status'] = 'run'
self.client.hset(TASK_NAME+':task_status', task_uuid, task_data)
def stop_alltasks(self):
alltasks = self.client.hgetall(TASK_NAME + ':task_status')
for task_uuid, task_data in alltasks.items():
task_uuid = task_uuid
task_data = eval(task_data)
status = task_data['status']
if status == 'run':
task_data['status'] = 'stop'
self.client.hset(TASK_NAME + ':task_status', task_uuid, task_data)
def clear_data(self):
self.client.delete(TASK_NAME+':task_nodes')
self.client.delete(TASK_NAME+':task_status')
#服务端worker控制
class WorkerControl():
def __init__(self, ip):
self.rpc = zerorpc.Client(timeout=3, heartbeat=3)
self.rpc.connect("tcp://{ip}:{port}".format(ip=ip, port=RPC_PORT))
def ping(self):
try:
return self.rpc.ping()
except Exception as e:
return False
def start_worker(self, worker_nums=10):
result = self.rpc.start_worker(worker_nums, RPC_PWD)
return result
def kill_worker(self):
result = self.rpc.kill_worker(RPC_PWD)
return result
def worker_status(self):
status = self.rpc.worker_status(RPC_PWD)
return status
def stop_task(self, task_uuid):
result = self.rpc.stop_task(task_uuid, RPC_PWD)
return result
def restart_task(self, task_uuid):
result = self.rpc.restart_task(task_uuid, RPC_PWD)
return result
def stop_alltasks(self):
result = self.rpc.stop_alltasks(RPC_PWD)
return result
def restart_alltasks(self):
result = self.rpc.restart_alltasks(RPC_PWD)
return result
def exit_rpc(self):
try:
self.rpc.exit_rpc(RPC_PWD)
return self.rpc.ping()
except Exception as e:
return True
def close(self):
self.rpc.close()
#任务队列信息
class QueueInfo():
def __init__(self):
self.client = queues.get_redis_client(QUEUE_REDIS_TYPE, QUEUE_REDIS_HOST, QUEUE_REDIS_PORT, QUEUE_REDIS_DB, QUEUE_REDIS_PWD, QUEUE_REDIS_NODES)
self.queues = queues.RedisQueues(conn=self.client)
def taskid_counts(self):
return self.queues.len(TASK_NAME+':task_ids')
def taskfail_counts(self):
return self.queues.len(TASK_NAME+':task_fails')
def taskid_range(self, start, end):
return self.queues.range(TASK_NAME+':task_ids', start, end)
def taskfail_range(self, start, end):
fail_datas = self.queues.range(TASK_NAME+':task_fails', start, end)
if fail_datas:
return [eval(fail) for fail in fail_datas]
else:
return list()
def task_index(self, key, index):
return self.queues.index(key, index)
def taskid_add(self, task_id):
return self.queues.push(TASK_NAME+':task_ids', task_id)
def task_delete(self, key, value):
count = self.queues.delete_value(key, value)
return count
def taskid_first(self, task_id):
return self.queues.first(TASK_NAME+':task_ids', task_id)
def taskid_last(self, task_id):
return self.queues.last(TASK_NAME+':task_ids', task_id)
def taskfail_rpush(self, value):
taskid = value['taskid']
self.task_delete(TASK_NAME+':task_fails', value)
return self.queues.push(TASK_NAME+':task_ids', taskid)
def taskfail_all_rpush(self):
fail_counts = self.taskfail_counts()
fails = self.taskfail_range(0, fail_counts)
fail_ids = [fail['taskid'] for fail in fails]
self.queues.delete_key(TASK_NAME+':task_fails')
for task_id in fail_ids:
self.queues.push(TASK_NAME+':task_ids', task_id)
return 'OK'
def taskid_empty(self):
return self.queues.delete_key(TASK_NAME+':task_ids')
def taskfail_empty(self):
return self.queues.delete_key(TASK_NAME+':task_fails')
#Redis信息
class RedisInfo():
def __init__(self, conn=None):
self.redis = conn
def info(self):
return self.redis.info()
def config_get(self):
return self.redis.config_get('*')
def client_list(self):
clients = self.redis.client_list()
if isinstance(clients, list):
client_lists = list()
for client in clients:
temps = list()
for k, v in client.items():
temps.append(k+'='+v)
client_lists.append(' '.join(temps))
return client_lists
else:
client_dicts = dict()
for ip, clients in clients.items():
client_lists = list()
for client in clients:
temps = list()
for k, v in client.items():
temps.append(k + '=' + v)
client_lists.append(' '.join(temps))
client_dicts[ip] = client_lists
return client_dicts
def save(self, is_bgsave=False):
if is_bgsave:
return self.redis.bgsave()
else:
return self.redis.save()
#节点服务器系统信息
class ServerInfo():
def __init__(self, ip):
self.rpc = zerorpc.Client(timeout=3, heartbeat=3)
self.rpc.connect("tcp://{ip}:{port}".format(ip=ip, port=RPC_PORT))
def get_sysinfo(self):
sysinfo = self.rpc.sysinfo(RPC_PWD)
return sysinfo
# client = queues.get_redis_client(QUEUE_REDIS_TYPE, QUEUE_REDIS_HOST, QUEUE_REDIS_PORT, QUEUE_REDIS_DB, QUEUE_REDIS_PWD, QUEUE_REDIS_NODES)
# queues = queues.RedisQueues(conn=client)
# pool = redis.ConnectionPool(host=QUEUE_REDIS_HOST, port=QUEUE_REDIS_PORT, db=QUEUE_REDIS_DB, password=QUEUE_REDIS_PWD)
# client = redis.StrictRedis(connection_pool=pool)
# qi = QueueInfo()
# qi.taskfail_rpush({'taskid': '27050696', 'error': "module 'tasks.spider' has no attribute 'save'"})
# ni = NodeInfo()
# ni.task_list()