]> git.evergreen-ils.org Git - working/random.git/blob - constrictor/task.py
it's now possible to run a single task more than once inside a given script. No...
[working/random.git] / constrictor / task.py
1 #!/usr/bin/python
2 # -----------------------------------------------------------------------
3 # Copyright (C) 2007-2008  King County Library System
4 # Bill Erickson <erickson@esilibrary.com>
5
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 3
9 # of the License, or (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 # -----------------------------------------------------------------------
16
17
18 import time, sys
19 from script import ScriptThread
20 from threading import local
21 import threading
22 from log import *
23
24
25 class Task(object):
26     """ A Task represents a single unit of work.  They are created 
27         before thread allocation, so no state information is stored
28         on the actual Task object.
29         """
30
31     def __init__(self, name=''):
32         self.name = name
33         self.reset()
34
35     def reset(self):
36         self.duration = 0
37         self.success = False
38         self.complete = False
39
40     def run(self, **kwargs):
41         """Override this method with the work to perform"""
42         logError('Override me!')
43
44     def start(self, **kwargs):
45
46         self.reset()
47         ret = None # capture the return value from the task
48         start = time.time()
49
50         try:
51             ret = self.run(**kwargs)
52             self.duration = time.time() - start
53             self.complete = True
54             self.success = True
55
56         except Exception, E:
57             sys.stderr.write(str(E))
58             # store the error info somewhere?
59
60         logDebug('%s: thread = %d : duration = %f' % (
61             self.name, ScriptThread.getThreadID(), self.duration))
62
63         sys.stdout.flush()
64         dbConn = ScriptThread.currentScriptThread().dbConnection
65         dbConn.insertTask(self)
66         return ret
67
68
69
70