-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathtaskWorkerHTTP.py
More file actions
executable file
·522 lines (413 loc) · 17.9 KB
/
Copy pathtaskWorkerHTTP.py
File metadata and controls
executable file
·522 lines (413 loc) · 17.9 KB
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#!/usr/bin/python
##################
# taskWorkerM.py
#
# Copyright David Baddeley, 2009
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##################
#!/usr/bin/python
#import Pyro.core
#import Pyro.naming
import os
import platform
import time
import matplotlib
matplotlib.use('SVG')
import queue as Queue
import threading
#import PYME.misc.pyme_zeroconf as pzc
from PYME import config
from PYME.misc.computerName import GetComputerName
compName = GetComputerName()
import logging
import logging.handlers
dataserver_root = config.get('dataserver-root')
if dataserver_root:
log_dir = '%s/LOGS/%s/taskWorkerHTTP' % (dataserver_root, compName)
if not os.path.exists(log_dir):
try:
os.makedirs(log_dir)
except:
#as we are launching multiple processes at once, there is a race condition here and we might
#have already created the directory between our test and the makedirs call
pass
#fh = logging.FileHandler('%s/%d.log' % (log_dir, os.getpid()), 'w')
#fh.setLevel(logging.DEBUG)
#logger.addHandler(fh)
#logging.basicConfig(filename ='%s/%d.log' % (log_dir, os.getpid()), level=logging.DEBUG)
logger = logging.getLogger('')
logger.setLevel(logging.DEBUG)
fh = logging.handlers.RotatingFileHandler(filename ='%s/%d.log' % (log_dir, os.getpid()), mode='w', maxBytes=1e6)
logger.addHandler(fh)
else:
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger('')
import requests
import sys
import signal
import yaml
def str_presenter(dumper, data):
if len(data.splitlines()) > 1: # check for multiline string
return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='|')
return dumper.represent_scalar('tag:yaml.org,2002:str', data)
yaml.add_representer(str, str_presenter)
#import socket
from PYME.localization import remFitBuf
from PYME.cluster import distribution
#import here to pre-populate the zeroconf nameserver
from PYME.IO import clusterIO
from PYME.IO import unifiedIO
from PYME.IO import clusterResults
#time.sleep(3)
LOCAL = False
if 'PYME_LOCAL_ONLY' in os.environ.keys():
LOCAL = os.environ['PYME_LOCAL_ONLY'] == '1'
class TaskError(object):
template = '''===========================================================================
Error in rule: {rule_id} running task: {task_id} on {comp_name}:{pid}
taskDescr:
----------
{taskDescr}
Traceback:
----------
{traceback}
'''
html_error_template = '''
<h5>Error in rule: {rule_id} running task: {task_id} on {comp_name}:{pid}</h5>
<h6>taskDescr:</h6>
<pre style="text-size:8pt;">{taskDescr}</pre>
<h6>Traceback:</h6>
<pre style="text-size:8pt;">{traceback}</pre>
<hrule>
'''
def __init__(self, taskDescr, traceback, exception=None):
self.taskDescr = taskDescr
self.traceback = traceback
self.exception = exception
@property
def log_url(self):
rule_id = self.taskDescr['id'].split('~')[0]
return 'PYME-CLUSTER://%s/__aggregate_txt/LOGS/rules/%s.log' % (clusterIO.local_serverfilter, rule_id)
@property
def html_log_url(self):
rule_id = self.taskDescr['id'].split('~')[0]
return 'PYME-CLUSTER://%s/__aggregate_txt/LOGS/rules/%s.html' % (clusterIO.local_serverfilter, rule_id)
def format_taskDesc(self):
return yaml.dump(self.taskDescr)
def extra_template_kwargs(self, mode='txt'):
""" Over-ride in derived classes to provide extra info to templates"""
return {}
def to_string(self, mode='txt'):
rule_id, task_id = self.taskDescr['id'].split('~')
if mode == 'html':
template = self.html_error_template
else:
template = self.template
return template.format(rule_id = rule_id, task_id = task_id, comp_name = compName, pid=os.getpid(),
taskDescr= self.format_taskDesc(), traceback = self.traceback, **self.extra_template_kwargs(mode=mode))
class RecipeTaskError(TaskError):
template = '''===========================================================================
Error in rule: {rule_id} running task: {task_id} on {comp_name}:{pid}
taskDescr:
----------
{taskDescr}
Recipe:
-------
{recipe}
Traceback:
----------
{traceback}
'''
html_error_template = '''
<h5>Error in rule: {rule_id} running task: {task_id} on {comp_name}:{pid}</h5>
<h6>taskDescr:</h6>
<pre style="font-size: 8pt;">{taskDescr}</pre>
<h6>Recipe:</h6>
{recipe}
<h6>Traceback:</h6>
<pre style="font-size: 8pt;color: darkred;">{traceback}</pre>
<hrule>
'''
def format_taskDesc(self):
import copy
# copy description, as we are going to override the recipe entry
desc = copy.deepcopy(self.taskDescr)
if 'recipe' in desc['taskdef']:
desc['taskdef']['recipe'] = 'see below ...'
return yaml.dump(desc)
def extra_template_kwargs(self, mode='text'):
from PYME.recipes.recipe import RecipeExecutionError
if (mode == 'html'):
if isinstance(self.exception, RecipeExecutionError) and self.exception.recipe:
return {'recipe' : self.exception.recipe.to_html()}
else:
return {'recipe': '<pre style="font-size: 8pt;">%s</pre>' % self.taskDescr['taskdef'].get('recipe', 'see taskdef.recipeURI')}
return {'recipe': self.taskDescr['taskdef'].get('recipe', 'see taskdef.recipeURI')}
class taskWorker(object):
def __init__(self, nodeserver_port):
self.inputQueue = Queue.Queue()
self.resultsQueue = Queue.Queue()
self.procName = '%s_%d' % (compName, os.getpid())
self._nodeserver_port = nodeserver_port
self._local_queue_url = 'http://127.0.0.1:%d/' % self._nodeserver_port
self._loop_alive = True
def loop_forever(self):
self.tCompute = threading.Thread(target=self.computeLoop)
self.tCompute.daemon = True
self.tCompute.start()
self.tI = threading.Thread(target=self.tasksLoop)
self.tI.daemon = True
self.tI.start()
self.tO = threading.Thread(target=self.returnLoop)
self.tO.daemon = True
self.tO.start()
try:
while True:
time.sleep(1)
finally:
self._loop_alive = False
def _return_task_results(self):
"""
File all results that this worker has completed
Returns
-------
"""
while True: # loop over results queue until it's empty
# print 'getting results'
try:
queueURL, taskDescr, res = self.resultsQueue.get_nowait()
outputs = taskDescr.get('outputs', {})
except Queue.Empty:
# queue is empty
return
if isinstance(res, TaskError):
# failure
clusterResults.fileResults(res.log_url, res.to_string().encode())
clusterResults.fileResults(res.html_log_url, res.to_string(mode='html').encode())
s = clusterIO._getSession(queueURL)
r = s.post(queueURL + 'node/handin?taskID=%s&status=failure' % taskDescr['id'])
if not r.status_code == 200:
logger.error('Returning task failed with error: %s' % r.status_code)
elif res is None:
# failure
s = clusterIO._getSession(queueURL)
r = s.post(queueURL + 'node/handin?taskID=%s&status=failure' % taskDescr['id'])
if not r.status_code == 200:
logger.error('Returning task failed with error: %s' % r.status_code)
elif res == True: # isinstance(res, ModuleCollection): #recipe output
# res.save(outputs) #abuse outputs dictionary as context
s = clusterIO._getSession(queueURL)
r = s.post(queueURL + 'node/handin?taskID=%s&status=success' % taskDescr['id'])
if not r.status_code == 200:
logger.error('Returning task failed with error: %s' % r.status_code)
else:
# success
try:
if 'results' in outputs.keys():
# old style pickled results
clusterResults.fileResults(outputs['results'], res)
else:
if len(res.results) > 0:
clusterResults.fileResults(outputs['fitResults'], res.results)
if len(res.driftResults) > 0:
clusterResults.fileResults(outputs['driftResults'], res.driftResults)
except requests.Timeout:
logger.exception('Filing results failed on timeout.')
s = clusterIO._getSession(queueURL)
r = s.post(queueURL + 'node/handin?taskID=%s&status=failure' % taskDescr['id'])
if not r.status_code == 200:
logger.error('Returning task failed with error: %s' % r.status_code)
else:
s = clusterIO._getSession(queueURL)
r = s.post(queueURL + 'node/handin?taskID=%s&status=success' % taskDescr['id'])
if not r.status_code == 200:
logger.error('Returning task failed with error: %s' % r.status_code)
def _get_tasks(self):
"""
Query nodeserver for tasks and place them in the queue for this worker,
if available
Returns
-------
new_tasks : bool
flag to report whether _get_tasks added new tasks to the taskWorker queue
"""
tasks = []
queueURL = self._local_queue_url
try:
# ask the queue for tasks
s = clusterIO._getSession(queueURL)
r = s.get(queueURL + 'node/tasks?workerID=%s&numWant=50' % self.procName)
if r.status_code == 200:
resp = r.json()
if resp['ok']:
res = resp['result']
if isinstance(res, list):
tasks += [(queueURL, t) for t in res]
else:
tasks.append((queueURL, res))
except requests.Timeout:
logger.info('Read timout requesting tasks from %s' % queueURL)
except Exception:
import traceback
logger.exception(traceback.format_exc())
if len(tasks) != 0:
#logger.debug('Got %d tasks' % len(tasks))
for t in tasks:
self.inputQueue.put(t)
return True
else:
# flag that there were no new tasks
return False
def tasksLoop(self):
"""
Loop forever asking for tasks to queue up for this worker
Returns
-------
"""
while True:
if not self._loop_alive:
break
# if our queue for computing is empty, try to get more tasks
if self.inputQueue.empty():
# if we don't have any new tasks, sleep to avoid constant polling
#logger.debug('tasksLoop-_get_tasks()')
if not self._get_tasks():
# no queues had tasks
time.sleep(0.1)
else:
time.sleep(0.1)
def returnLoop(self):
"""
Loop forever returning task results
Returns
-------
"""
while True:
# turn in completed tasks
try:
#logger.debug('returnLoop - _return')
self._return_task_results()
except:
import traceback
logger.exception(traceback.format_exc())
if not self._loop_alive:
break
time.sleep(1)
def computeLoop(self):
while self._loop_alive:
#loop over tasks - we pop each task and then delete it after processing
#to keep memory usage down
#logger.debug('computeLoop-inputQueue.get()')
queueURL, taskDescr = self.inputQueue.get()
#logger.debug('computeLoop-%s' %taskDescr)
if taskDescr['type'] == 'localization':
try:
task = remFitBuf.createFitTaskFromTaskDef(taskDescr)
res = task()
self.resultsQueue.put((queueURL, taskDescr, res))
except:
import traceback
traceback.print_exc()
tb = traceback.format_exc()
logger.exception(tb)
self.resultsQueue.put((queueURL, taskDescr, TaskError(taskDescr, tb)))
#self.resultsQueue.put((queueURL, taskDescr, None))
elif taskDescr['type'] == 'recipe':
from PYME.recipes import Recipe
from PYME.recipes import modules
try:
taskdefRef = taskDescr.get('taskdefRef', None)
if taskdefRef: #recipe is defined in a file - go find it
recipe_yaml = unifiedIO.read(taskdefRef)
else: #recipe is defined in the task
recipe_yaml = taskDescr['taskdef']['recipe']
recipe = Recipe.fromYAML(recipe_yaml)
#initial context
context = {'data_root' : clusterIO.local_dataroot,
'task_id' : taskDescr['id'].split('~')[0]}
#load recipe inputs
logging.debug(taskDescr)
recipe.load_inputs(taskDescr['inputs'])
for key, url in taskDescr['inputs'].items():
if key == '__sim':
# special case for no-input simulation recipes
# for now, essentially ignore `__sim` inputs, but propagate into context just in case
# TODO?? find a way of encoding simulation parameters?
context['sim_tag'] = url
# else:
# logging.debug('RECIPE: loading %s as %s' % (url, key))
# recipe.loadInput(url, key)
#print recipe.namespace
recipe.execute()
#update context with file stub and input directory
try:
principle_input = taskDescr['inputs']['input'] #default input
context['file_stub'] = os.path.splitext(os.path.basename(principle_input))[0]
context['input_dir'] = unifiedIO.dirname(principle_input)
except KeyError:
pass
try:
od = taskDescr['output_dir']
# make sure we have a trailing slash
# TODO - this should be fine for most windows use cases, as you should generally
# use POSIX urls for the cluster/cluster of one, but might need checking
if not od.endswith('/'):
od = od + '/'
context['output_dir'] = unifiedIO.dirname(od)
except KeyError:
pass
#print taskDescr['inputs']
#print context
#abuse outputs as context
outputs = taskDescr.get('outputs', None)
if not outputs is None:
context.update(outputs)
#print context, context['input_dir']
recipe.save(context)
self.resultsQueue.put((queueURL, taskDescr, True))
except Exception as e:
import traceback
traceback.print_exc()
tb = traceback.format_exc()
logger.exception(tb)
self.resultsQueue.put((queueURL, taskDescr, RecipeTaskError(taskDescr, tb, e)))
def on_SIGHUP(signum, frame):
raise RuntimeError('Recieved SIGHUP')
from argparse import ArgumentParser
if __name__ == '__main__':
op = ArgumentParser(description="PYME rule server for task distribution. This should run once per cluster.")
op.add_argument('-p', dest='profile', default=False, action='store_true',
help="enable profiling")
op.add_argument('--profile-dir', dest='profile_dir')
op.add_argument('-s', '--server-port', dest='server_port', type=int, default=config.get('nodeserver-port', 15347),
help='Optionally restrict advertisements to local machine')
args = op.parse_args()
if args.profile:
from PYME.util import mProfile
mProfile.profileOn(['taskWorkerHTTP.py', 'remFitBuf.py'])
if not platform.platform().startswith('Windows'):
signal.signal(signal.SIGHUP, on_SIGHUP)
try:
#main()
tW = taskWorker(nodeserver_port=args.server_port)
tW.loop_forever()
except KeyboardInterrupt:
#supress error message here - we only want to know if something bad happened
pass
finally:
if args.profile:
mProfile.report(display=False, profiledir=args.profile_dir)