-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPRM.py
executable file
·423 lines (376 loc) · 15.9 KB
/
PRM.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import multiprocessing
import time
import sproxy
import sys
import prt_utils
import Queue
import queue_device
import zmq
import os
import logging
import threading
import mplog
import proxy_worker
import redis
import requests
import json
import traceback
# TODO - scenario1: Proxy is down at the beginning of the release process (before check_dump_age)
### Proxies For Test Purposes Only ###
###SITES = ["ny_an", "ny_lb", "ams_an", "ams_lb", "lax_an", "lax_lb", "sg"]
SITES = ["OPS_PROXY", "OPS_PROXY_2"]
"""
ny_an = ["nyproxy25", 'nyproxy26', 'nyproxy27', 'nyproxy28', 'nyproxy29', 'nyproxy30', 'nyproxy31']
ny_lb = ["ny4aproxy10", 'ny4aproxy11', 'ny4aproxy12','ny4aproxy13', 'ny4aproxy14', 'ny4aproxy15', 'ny4aproxy16']
ams_an =["ams2proxy25", 'ams2proxy26', 'ams2proxy27', 'ams2proxy28', 'ams2proxy29', 'ams2proxy30']
ams_lb = ["ams2proxy05", 'ams2proxy06', 'ams2proxy07', 'ams2proxy08', 'ams2proxy09']
lax_an = ["laxproxy25", 'laxproxy26', 'laxproxy27', 'laxproxy28', 'laxproxy29']
lax_lb = ["laxproxy15", 'laxproxy16', 'laxproxy17']
sg = ["sgproxy12", 'sgproxy13', 'sgproxy14', 'sgproxy15']
"""
basedir = os.getcwd()
workers_conf_file = os.path.join(basedir, 'workers_for_release-dev.conf')
#######################
def process_validator(func):
def validator(*args, **kwargs):
try:
processes = kwargs['processes']
site = kwargs['site']
pid = kwargs['pid']
proc = processes[site][pid]['proc']
if prt_utils.check_process(proc):
return func(*args, **kwargs)
else:
#print 'This is not a new server!!'
#logger.error('Requested process is dead!!') # TODO - implement logger
raise Exception # TODO - Create custom exception for this screnario
except Exception as e:
print "Process is dead!"
del processes[site][pid] # This should only happen if the custom exception was raised
raise
return validator
def test():
return "This Is Test Func"
def active_proxy_workers(**kwargs):
#sites_dict = kwargs['sites_dict']
processes = kwargs['processes']
active_count = {}
for site in processes.keys():
active_count[site] = {}
active_count[site]['workers'] = {}
active_count[site]['active_workers'] = len(processes[site].keys())
active_count[site]['proxies'] = globals()['comp_servers'][site] # Test purposes only
for proc in processes[site].keys():
active_count[site]['workers'][proc] = {}
active_count[site]['workers'][proc]['status'] = processes[site][proc]['status']
active_count[site]['workers'][proc]['working_on'] = processes[site][proc]['working_on']
active_count[site]['workers'][proc]['step'] = processes[site][proc]['step']
return active_count
def create_process(**kwargs):
try:
processes = kwargs['processes']
queues = kwargs['queues']
site = kwargs['site']
r = kwargs['r']
r13 = kwargs['r13']
processes_lock = kwargs['processes_lock']
prm_conn, proc_conn = multiprocessing.Pipe()
worker_num = len(processes[site]) + 1
worker_type = r13.hget('workers_config:%s' % site, 'type')
if worker_type == 'custom':
custom_command = r13.hget('workers_config:%s' % site, 'command')
else:
custom_command = ''
proc = multiprocessing.Process(target=proxy_worker.proxy_worker, args=(queues[site], proc_conn, site,
worker_num, worker_type, custom_command))
proc.daemon = True
proc.start()
pid = str(proc.pid)
r.sadd(site, pid)
processes_lock.acquire()
processes[site][pid] = {}
processes[site][pid]['conn'] = prm_conn
processes[site][pid]['proc'] = proc
processes_lock.release()
r.hmset(pid, {'status': 'Idle', 'working_on': None, 'step': None, 'step_start_time': None, 'type': worker_type,
'custom_command': custom_command})
return pid
except Exception:
raise
def create_sites_queues(sites_dict):
for site in sites_dict.keys():
q = multiprocessing.Queue()
sites_dict[site]['site_q'] = q
def add_to_q(**kwargs):
#sites_dict = kwargs['sites_dict']
##pre_queues = kwargs['pre_queues']
queues = kwargs['queues']
site = kwargs['site']
proxies = kwargs['proxies']
#site_q = sites_dict[site]['site_q']
#site_q.put(proxy_name)
wasAdded = []
q = queues[site]
q_items = q.get_items()
for proxy in proxies:
if not proxy in q_items:
q.put(proxy)
wasAdded.append(proxy)
return "%s was added to queue!" % wasAdded
def init_dictionaries(r):
processes = {}
queues = {}
pre_queues = {}
for site in SITES:
processes[site] = {}
#queues[site] = multiprocessing.Queue() # Replaced by redisQueue
queues[site] = prt_utils.RedisQueue(site, host='localhost', port=6379, db=11)
pre_queues[site] = []
r.sadd('processes', site)
return processes, queues, pre_queues
"""
def pre_q_to_q(processes, pre_queues, queues, SITES):
for site in SITES:
for pid in processes[site]:
if len(pre_queues[site]) > 0 and processes[site][pid]['status'] == "Idle":
queues[site].put(pre_queues[site].pop(0))
"""
#TODO - Create a decorator that will validate that the process still exists (that there's someone on the other side
#of the pipe
@process_validator
def pause_or_resume_worker(**kwargs):
try:
processes = kwargs['processes']
site = kwargs['site']
pid = kwargs['pid']
action = kwargs['action']
conn = processes[site][pid]['conn']
conn.send(action)
while not conn.poll(0.1):
pass
conn.recv()
if action == 'stop':
while processes[site].has_key(pid):
time.sleep(0.1)
#for item in message:
#processes[site][pid][item[0]] = item[1]
#conn.send("OK")
return "Process %s is now %sd" % (pid, action)
except Exception as e:
print "Error!"
print str(e)
raise
def get_Qs_status(**kwargs):
queues = kwargs['queues']
queues_status = {}
for site in SITES:
queues_status[site] = queues[site].get_items()
return queues_status
def get_default_num_workers(**kwargs):
conf = prt_utils.get_conf_from_file(workers_conf_file) # TODO - Conf file should only be read once at the start.
num_workers_for_release = {}
for site in conf:
num_workers_for_release[site] = conf[site]['num_of_workers']
return num_workers_for_release
def start_workers_for_release(**kwargs):
numOfWorkers = kwargs['numOfWorkers']
processes = kwargs['processes']
queues = kwargs['queues']
r = kwargs['r']
processes_lock = kwargs['processes_lock']
r13 = kwargs['r13']
response = {}
for item in numOfWorkers:
site = item[0]
numWorkersInSite = item[1]
response[site] = []
for i in range(int(numWorkersInSite)):
newKwargs = {}
newKwargs['processes'] = processes
newKwargs['queues'] = queues
newKwargs['site'] = site
newKwargs['r'] = r
newKwargs['processes_lock'] = processes_lock
newKwargs['r13'] = r13
pid = create_process(**newKwargs)
response[site].append(pid)
return response
def get_config(**kwargs):
r13 = kwargs['r13']
config = r13.hgetall('config')
steps = r13.smembers('steps')
sites = r13.smembers('sites')
sites.add('main')
workers_config = {}
for site in iter(sites):
workers_config[site] = r13.hgetall('workers_config:%s' % site)
time_values = {}
for step in iter(steps):
time_values[step] = {}
step_values = r13.hgetall('time_values:%s' % step)
for key in step_values:
time_values[step][key] = step_values[key]
return [config, time_values, workers_config]
def update_config(**kwargs):
r13 = kwargs['r13']
configs = kwargs['configs']
processes = kwargs['processes']
print configs
response = []
#current_config = r13.hgetall('config')
workers_type_changed = False
new_workers_config = {}
for item in configs:
if item[0] == 'workers_config:main' and item[1] == 'use_main':
if item[2]:
item[2] = 'True'
else:
item[2] = 'False'
new_use_main = item[2]
elif item[0] == 'config' and item[1] == 'show_all_proxies':
if item[2]:
item[2] = 'True'
else:
item[2] = 'False'
elif item[0].startswith('workers_config') and item[1] == 'type':
site = item[0].split(':')[1]
new_workers_config[site] = {}
new_workers_config[site]['type'] = item[2]
for site in new_workers_config:
if site == 'main':
for s in processes:
if len(processes[s].keys()) > 0:
raise RuntimeError("Can't change configs for %s's workers while there are active workers" % site)
else:
if len(processes[site].keys()) > 0:
raise RuntimeError("Can't change configs for %s's workers while there are active workers" % site)
current_use_main = r13.hget('workers_config:main', 'use_main')
if current_use_main != new_use_main:
workers_config = {}
for item in r13.keys('workers_config:*'):
site = item.split(':')[1]
workers_config[site] = r13.hgetall(item)
for site in new_workers_config:
workers_config[site] = new_workers_config[site]
main_type = workers_config['main']['type']
try:
main_command = workers_config['main']['command']
except KeyError:
main_command = ''
for site in processes:
if len(processes[site].keys()) > 0:
site_type = workers_config[site]['type']
try:
site_command = workers_config[site]['command']
except KeyError:
site_command = ''
if main_type != site_type:
raise RuntimeError("Can't change configs for %s's workers while there are active workers" % site)
if main_type == site_type and main_type == 'custom' and main_command != site_command:
raise RuntimeError("Can't change configs for %s's workers while there are active workers" % site)
for item in configs:
conf = item[0]
key = item[1]
value = item[2]
current_config = r13.hget(conf, key) # TODO - find a way that only values that are being changed will arrive here
if current_config != value:
r13.hmset(conf, {key:value})
response.append('%s - %s' % (conf, key))
return response
def get_workers_status(processes, pre_queues, queues, SITES, lock):
while True:
for site in SITES:
for pid in processes[site]:
if len(pre_queues[site]) > 0 and processes[site][pid]['status'] == "Idle":
lock.acquire() # TODO - make the lock useful...
queues[site].put(pre_queues[site].pop(0))
lock.release()
time.sleep(2)
def processes_gc(processes, r, processes_lock):
while True:
for site in processes.keys():
for pid in processes[site].keys():
if not processes[site][pid]['proc'].is_alive():
r.srem(site, pid)
r.delete(pid)
processes_lock.acquire()
processes[site].pop(pid)
processes_lock.release()
time.sleep(3)
#TODO - There should be a regular process_checker, in case for some reason a process dies
def start_prm(main_conn):
try:
r = redis.StrictRedis(host='localhost', port=6379, db=1)
r13 = redis.StrictRedis(host='localhost', port=6379, db=13)
except Exception: # TODO - add redis exception
print "Cant connect to Redis!"
sys.exit(1)
r.flushdb() ### Clean all process data before starting
formatter = logging.Formatter('%(asctime)s %(worker)s: %(levelname)-8s %(message)s')
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
this_module = sys.modules[__name__]
mpHandler = mplog.MultiProcessingLog(name="/tmp/prm.log", mode='a', maxsize=1024, rotate=0)
mpHandler.setFormatter(formatter)
#logger.addHandler(mplog.MultiProcessingLog(name="/tmp/testmplog.txt", mode='a', maxsize=1024, rotate=0))
logger.root.addHandler(mpHandler)
me = {'worker': 'PRM'}
logger.info("============================================================================", extra=me)
logger.info("================================ PRM Has Started! =======================", extra=me)
logger.info("============================================================================", extra=me)
processes, queues, pre_queues = init_dictionaries(r)
processes_lock = threading.Lock()
prmDict = {'processes': processes, 'queues': queues, 'pre_queues': pre_queues, 'r': r, 'r13': r13,
'processes_lock': processes_lock} # TODO - There should be an init func that returns prmDict with all its keys
toExit = False
prmDict['sites_dict'] = {}
for site in SITES:
prmDict['sites_dict'][site] = {}
prmDict['sites_dict'][site]['procs'] = {}
###create_sites_queues(prmDict['sites_dict'])
###prmDict['processes'] = []
lock = threading.Lock()
socket = prt_utils.create_zmq_connection("127.0.0.1", "5556", zmq.REP, "bind")
##msg_checker = threading.Thread(target=get_workers_status, args=(processes, pre_queues, queues, SITES, lock))
processesGC = threading.Thread(target=processes_gc, args=(processes, r, processes_lock))
##msg_checker.daemon = True
processesGC.daemon = True
##msg_checker.start()
processesGC.start()
while True:
while socket.poll(timeout = 10) == 0:
time.sleep(0.1)
multiprocessing.active_children()
#pre_q_to_q(processes, pre_queues, queues, SITES)
###msg_checker.join(0.1) # TODO - moved to redis
pass
request = socket.recv_json()
try:
method = getattr(this_module, request[0])
if len(request) > 1:
kwargs = {}
for arg in request[1]:
kwargs[arg] = prmDict[arg]
for arg in request[2].keys():
kwargs[arg] = request[2][arg]
response = method(**kwargs)
else:
response = method()
except Exception as e:
print "PRM exception handler"
time.sleep(2)
response = str(e) # TODO - respone should contain a "success/fail" field
exc_type, exc_value, exc_traceback = sys.exc_info()
#print "%s" % e # TODO - Make sure the full traceback is printed. right now only e.message is printed.
print "*** print_exception:"
traceback.print_exception(exc_type, exc_value, exc_traceback,
limit=2, file=sys.stdout)
finally:
try:
socket.send_json(response)
except Exception as e: # TODO - this should only refer to socket exceptions
print "Something went REALLY wrong, unable to send response to UI. Exiting..."
print e
sys.exit(1)
if __name__ == '__main__':
start_prm(conn)