1
2
3
4
5
6
7
8
9
10
11
12
13
14 from Globals import InitializeClass
15 import transaction
16 from zope.interface import implements
17
18 from Products.ZenRelations.RelSchema import *
19 from Products.ZenModel.ZenModelRM import ZenModelRM
20
21 from interfaces import *
22 from status import JobStatus, FAILURE
23 import time
24 from copy import copy
25 from Products.ZenUtils import guid
26
31
33
34 implements(IJobManager)
35
36 meta_type = portal_type = 'JobManager'
37
38 _relations = (
39 ("jobs",
40 ToManyCont(
41 ToOne, "Products.Jobber.status.JobStatus", "jobmanager"
42 )
43 ),
44 )
45
47 """
48 Get a unique id for jobs.
49
50 If C{klass} is not None, its __name__ attribute will be used as the id
51 prefix.
52
53 @return: A unique id.
54 @rtype: str
55 """
56 if klass is not None:
57 name = klass.__name__
58 else:
59 name = 'job'
60 return "%s_%s" % (name, guid.generate())
61
62 - def addJob(self, klass, *args, **kwargs):
63 """
64 Create a new L{Job} and L{JobStatus} from the class specified.
65
66 C{klass} must implement L{IJob} and should subclass L{Job}.
67
68 @return: The L{JobStatus} object representing the job created.
69 @rtype: L{JobStatus}
70 """
71 assert IJob.implementedBy(klass), ("JobManager.addJob can accept"
72 " only IJob classes")
73 jobid = self._getId(klass)
74 instance = klass(jobid, *args, **kwargs)
75
76 status = JobStatus(instance)
77 self.jobs._setObject(status.id, status)
78 transaction.commit()
79 return self.jobs._getOb(status.id)
80
82 """
83 Return a L{JobStatus} object that matches the id specified.
84
85 @param jobid: id of the L{JobStatus}. The "JobStatus_" prefix is not
86 necessary.
87 @type jobid: str
88 @return: A matching L{JobStatus} object, or None if none is found
89 @rtype: L{JobStatus}, None
90 """
91
92
93
94 uid = jobid.split('_')[-1]
95 for jid in self.jobs.objectIds():
96 if jid.endswith(uid):
97 return self.jobs._getOb(jid)
98 return None
99
101 """
102 Return JobStatus objects that have not yet completed, including those
103 that have not yet started.
104
105 @return: A list of jobs.
106 @rtype: list
107 """
108 def isUnfinished(job):
109 return not job.isFinished()
110 return filter(isUnfinished, self.jobs())
111
113 """
114 Return JobStatus objects that have started but not finished.
115
116 @return: A list of jobs.
117 @rtype: list
118 """
119 def isRunning(job):
120 return not job.isFinished() and job.isStarted()
121 return filter(isRunning, self.jobs())
122
124 """
125 Return JobStatus objects that have not yet started.
126
127 @return: A list of jobs.
128 @rtype: list
129 """
130 def isPending(job):
131 return not job.isStarted()
132 return filter(isPending, self.jobs())
133
135 """
136 Return JobStatus objects that have finished.
137
138 @return: A list of jobs.
139 @rtype: list
140 """
141 def isFinished(job):
142 return job.isFinished()
143 return filter(isFinished, self.jobs())
144
146 """
147 Delete all jobs older than untiltime.
148 """
149 for job in self.getFinishedJobs():
150 if job.getTimes()[1] <= untiltime:
151 job.delete()
152
158
165
166
167 InitializeClass(JobManager)
168