### takes in argument a list of tasks that are instances
### of a CompleterTask subclass
class Completer:
- def __init__ (self, tasks, verbose=True):
+ def __init__ (self, tasks, verbose=True, message=None):
self.tasks=tasks
self.verbose=verbose
- def run (self, timeout_timedelta, silent_timedelta, period=None):
- timeout = datetime.now()+timeout_timedelta
- timeout_minutes = timeout_timedelta.total_seconds()/60
+ self.message="({})".format(message) if message else ""
+ def run (self, timeout_timedelta, silent_timedelta, period):
+ begin = datetime.now()
+ timeout = begin+timeout_timedelta
+ timeout_seconds = timeout_timedelta.total_seconds()
+ timeout_minutes = timeout_seconds/60
graceout = datetime.now()+silent_timedelta
- silent_minutes = silent_timedelta.total_seconds()/60
+ silent_seconds = silent_timedelta.total_seconds()
+ silent_minutes = silent_seconds/60
period_seconds=int(period.total_seconds())
if self.verbose:
- utils.header("max timeout is %d minutes, silent for %d minutes (period is %s s)"%\
+ if timeout_seconds >= 120:
+ utils.header("max timeout is %d minutes, silent for %d minutes (period is %s s)"%\
(timeout_minutes,silent_minutes,period_seconds))
+ else:
+ utils.header("max timeout is %d seconds, silent for %d seconds (period is %s s)"%\
+ (timeout_seconds,silent_seconds,period_seconds))
tasks=self.tasks
while tasks:
fine=[]
success=task.run (silent=datetime.now() <= graceout)
if success: fine.append(task)
for task in fine: tasks.remove(task)
- if not tasks: return True
+ if not tasks:
+ if self.verbose:
+ duration = datetime.now()-begin
+ print "total completer {} {}s".format(self.message,
+ int(duration.total_seconds()))
+ return True
if datetime.now() > timeout:
for task in tasks:
- print task.failure_message()
task.failure_epilogue()
return False
if self.verbose:
print self.message(),"->","OK" if result else "KO"
return result
def message (self): return "you-need-to-redefine-message"
- def failure_message (self): return "you-need-to-redefine-failure_message"
- def failure_epilogue (self): pass
+ def failure_epilogue (self): print "you-need-to-redefine-failure_epilogue"
# random result
class TaskTest (CompleterTask):
def message (self):
return "Task %d - delay was %d s"%(self.counter,self.delay)
- def failure_message (self): return "BOTTOM LINE: FAILURE with task (%s)"%self.counter
+ def failure_epilogue (self): print "BOTTOM LINE: FAILURE with task (%s)"%self.counter
def main ():
import sys