more readable scheduler, thanks Niphlod

This commit is contained in:
mdipierro
2012-08-19 10:03:28 -05:00
parent b2294a8a56
commit c9360194c0
2 changed files with 53 additions and 47 deletions
+1 -1
View File
@@ -1 +1 @@
Version 2.00.0 (2012-08-19 10:02:26) dev Version 2.00.0 (2012-08-19 10:03:24) dev
+52 -46
View File
@@ -181,16 +181,13 @@ def executor(queue,task, out):
self.out_queue = out_queue self.out_queue = out_queue
self.stdout = sys.stdout self.stdout = sys.stdout
sys.stdout = self sys.stdout = self
self.istr = ""
def __del__(self): def __del__(self):
sys.stdout = self.stdout sys.stdout = self.stdout
def flush(self):
pass
def write(self,data): def write(self,data):
self.out_queue.put(data) self.out_queue.put(data)
self.istr += data
def getvalue(self):
return self.istr
#stdout, sys.stdout = sys.stdout, cStringIO.StringIO()
stdout = LogOutput(out) stdout = LogOutput(out)
try: try:
if task.app: if task.app:
@@ -224,13 +221,11 @@ def executor(queue,task, out):
result = eval(task.function)( result = eval(task.function)(
*loads(task.args, object_hook=_decode_dict), *loads(task.args, object_hook=_decode_dict),
**loads(task.vars, object_hook=_decode_dict)) **loads(task.vars, object_hook=_decode_dict))
#stdout, sys.stdout = sys.stdout, stdout queue.put(TaskReport(COMPLETED, result=result))
sys.stdout = stdout.stdout
queue.put(TaskReport(COMPLETED, result,stdout.getvalue()))
except BaseException,e: except BaseException,e:
sys.stdout = stdout.stdout
tb = traceback.format_exc() tb = traceback.format_exc()
queue.put(TaskReport(FAILED,tb=tb, output=stdout.getvalue())) queue.put(TaskReport(FAILED,tb=tb))
del stdout
class MetaScheduler(threading.Thread): class MetaScheduler(threading.Thread):
def __init__(self): def __init__(self):
@@ -255,47 +250,62 @@ class MetaScheduler(threading.Thread):
self.process = p self.process = p
logging.debug(' task starting') logging.debug(' task starting')
p.start() p.start()
task_output = ""
tout = ""
try: try:
if task.sync_output > 0: if task.sync_output > 0:
task_output = "" run_timeout = task.sync_output
start = time.time() else:
while p.is_alive() and (time.time()-start < task.timeout): run_timeout = task.timeout
p.join(timeout=task.sync_output)
tout = "" start = time.time()
while not out.empty():
tout += out.get() while p.is_alive() and (time.time()-start < task.timeout):
if tout: if tout:
logging.debug(' partial output: "%s"' % str(tout)) try:
if CLEAROUT in tout: logging.debug("aaaaaaaaaaaaaa partial output saved")
task_output = tout[tout.rfind(CLEAROUT)+len(CLEAROUT):]
else:
task_output += tout
db(sr.id==task.run_id).update(output = task_output) db(sr.id==task.run_id).update(output = task_output)
db.commit() db.commit()
else: except:
p.join(task.timeout) pass
p.join(timeout=run_timeout)
tout = ""
while not out.empty():
tout += out.get()
if tout:
logging.debug(' partial output: "%s"' % str(tout))
if CLEAROUT in tout:
task_output = tout[tout.rfind(CLEAROUT)+len(CLEAROUT):]
else:
task_output += tout
except: except:
p.terminate() p.terminate()
p.join() p.join()
self.have_heartbeat = False self.have_heartbeat = False
logging.debug(' task stopped by general exception') logging.debug(' task stopped by general exception')
return TaskReport(STOPPED) tr = TaskReport(STOPPED)
if p.is_alive():
p.terminate()
logging.debug(' task timeout')
try:
tr = queue.get(timeout=2)
tr.status = TIMEOUT
except Queue.Empty:
tr = TaskReport(TIMEOUT)
return tr
elif queue.empty():
self.have_heartbeat = False
logging.debug(' task stopped')
return TaskReport(STOPPED)
else: else:
logging.debug(' task completed or failed') if p.is_alive():
return queue.get() p.terminate()
logging.debug(' task timeout')
try:
# we try to get a traceback here
tr = queue.get(timeout=2)
tr.status = TIMEOUT
tr.output = task_output
except Queue.Empty:
tr = TaskReport(TIMEOUT)
elif queue.empty():
self.have_heartbeat = False
logging.debug(' task stopped')
tr = TaskReport(STOPPED)
else:
logging.debug(' task completed or failed')
tr = queue.get()
tr.output = task_output
return tr
def die(self): def die(self):
logging.info('die!') logging.info('die!')
@@ -588,15 +598,11 @@ class Scheduler(MetaScheduler):
#if it's stopped it's None as NoneType, so we record #if it's stopped it's None as NoneType, so we record
#the STOPPED "run" anyway #the STOPPED "run" anyway
logging.debug(' recording task report in db (%s)' % task_report.status) logging.debug(' recording task report in db (%s)' % task_report.status)
#CLEAROUT clears the output
tout = task_report.output
if tout and CLEAROUT in tout:
tout = tout[tout.rfind(CLEAROUT)+len(CLEAROUT):]
db(db.scheduler_run.id==task.run_id).update( db(db.scheduler_run.id==task.run_id).update(
status = task_report.status, status = task_report.status,
stop_time = now, stop_time = now,
result = task_report.result, result = task_report.result,
output = tout, output = task_report.output,
traceback = task_report.tb) traceback = task_report.tb)
else: else:
logging.debug(' deleting task report in db because of no result') logging.debug(' deleting task report in db because of no result')