simplified task management. no longer using a task wrapper and task runner to accomo...
authorerickson <erickson@6d9bc8c9-1ec2-4278-b937-99fde70a366f>
Fri, 15 Jan 2010 19:43:17 +0000 (19:43 +0000)
committererickson <erickson@6d9bc8c9-1ec2-4278-b937-99fde70a366f>
Fri, 15 Jan 2010 19:43:17 +0000 (19:43 +0000)
git-svn-id: svn://svn.open-ils.org/ILS-Contrib/constrictor/trunk@757 6d9bc8c9-1ec2-4278-b937-99fde70a366f

constrictor/db.py
constrictor/script.py
constrictor/task.py
samples/sleep.py

index dc12969..cb9f3b5 100644 (file)
@@ -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)
 
index 662c70c..14eadaf 100644 (file)
@@ -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
 
index 5fbfa34..6356d06 100644 (file)
@@ -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
index 5c7382c..2518a19 100755 (executable)
@@ -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