From: erickson Date: Fri, 15 Jan 2010 19:43:17 +0000 (+0000) Subject: simplified task management. no longer using a task wrapper and task runner to accomo... X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=ff430708da4e60f2a1634c70a1850ea6461022bb;p=working%2Frandom.git simplified task management. no longer using a task wrapper and task runner to accomodate threads. approach now is to instantiate tasks inside the script (post thread creation). updated sample script. contrib scripts to follow git-svn-id: svn://svn.open-ils.org/ILS-Contrib/constrictor/trunk@757 6d9bc8c9-1ec2-4278-b937-99fde70a366f --- diff --git a/constrictor/db.py b/constrictor/db.py index dc1296981..cb9f3b598 100644 --- a/constrictor/db.py +++ b/constrictor/db.py @@ -168,19 +168,19 @@ class DBConnection(object): dbSema.release() return cur - def insertTask(self, taskRunner): + def insertTask(self, task): from script import ScriptThread SQL = """ insert into task (task_name, runtime, runner, thread_id, task_set, duration, success) values ('%s', %f, %d, %d, %d, %f, %d) """ % ( - taskRunner.task.name, + task.name, time.time(), 1, # XXX get me from the task runner ? ScriptThread.getThreadID(), DBConnection.taskSetID, - taskRunner.duration, - taskRunner.success + task.duration, + task.success ) self.execute(SQL, True) diff --git a/constrictor/script.py b/constrictor/script.py index 662c70c8b..14eadaf10 100644 --- a/constrictor/script.py +++ b/constrictor/script.py @@ -18,7 +18,7 @@ import time, sys, threading from threading import Thread, local from properties import Properties from db import DBConnection -import threading +import threading, traceback from log import * @@ -66,6 +66,7 @@ class ScriptThread(Thread): try: self.script.run() except Exception, e: + traceback.print_exc() logError("Script exception: %s" % str(e)) break diff --git a/constrictor/task.py b/constrictor/task.py index 5fbfa3413..6356d0672 100644 --- a/constrictor/task.py +++ b/constrictor/task.py @@ -30,53 +30,31 @@ class Task(object): def __init__(self, name=''): self.name = name + self.duration = 0 + self.success = False + self.complete = False + def run(self, **kwargs): """Override this method with the work to perform""" logError('Override me!') - def wrap(self): - """ Static method for wrapping and registering a task. - Statistics will only be collected for registered tasks. - """ - return TaskWrapper(self) - - -class TaskWrapper(object): - """ Provides a new run() method for the client to call directly. - The main purpose of the TaskWrapper is to allow the creation of - a TaskRunner after thread creation. - """ - - def __init__(self, task): - self.task = task - def run(self, **kwargs): - return TaskRunner(self.task).go(**kwargs) + def start(self, **kwargs): - -class TaskRunner(object): - """ Runs a single task and collects timing information on the task for a given thread. """ - - def __init__(self, task): - self.task = task - self.duration = 0 - self.success = False - self.complete = False - - def go(self, **kwargs): - start = time.time() ret = None # capture the return value from the task + start = time.time() try: - ret = self.task.run(**kwargs) + ret = self.run(**kwargs) self.duration = time.time() - start self.complete = True self.success = True + except Exception, E: sys.stderr.write(str(E)) # store the error info somewhere? logDebug('%s: thread = %d : duration = %f' % ( - self.task.name, ScriptThread.getThreadID(), self.duration)) + self.name, ScriptThread.getThreadID(), self.duration)) sys.stdout.flush() dbConn = ScriptThread.currentScriptThread().dbConnection diff --git a/samples/sleep.py b/samples/sleep.py index 5c7382c32..2518a191e 100755 --- a/samples/sleep.py +++ b/samples/sleep.py @@ -26,22 +26,13 @@ class MyTask(Task): return self.name -# wrap the tasks so they can be analyzed -task = MyTask('task1').wrap() -task2 = MyTask('task2').wrap() -task3 = MyTask('task3').wrap() -task4 = MyTask('task4').wrap() - - class MyScript(Script): ''' Subclass the Script class, define the run() method ''' def run(self): - ret = task.run() - ret = task2.run() - ret = task3.run() - ret = task4.run() - ret = task.run() - ret = task2.run() + + for task in ['task1', 'task2', 'task3', 'task4', 'task1', 'task2']: + MyTask(task).start() + return True # Launch the script