-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathScheduler.py
More file actions
482 lines (391 loc) · 15.7 KB
/
Scheduler.py
File metadata and controls
482 lines (391 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
"""This is the task manager Python package.
It provides a system for running any number of predefined tasks in separate
threads in an organized and controlled manner.
A task in this package is a class derived from the Task class. The task
should have a run method that, when called, performs some task.
The Scheduler class is the organizing object. It manages the addition,
execution, deletion, and well being of a number of tasks. Once you have
created your task class, you call the Scheduler to get it added to the
tasks to be run.
"""
from threading import Thread, Event
from time import localtime, time
from .TaskHandler import TaskHandler
class Scheduler(Thread):
"""The top level class of the task manager system.
The Scheduler is a thread that handles organizing and running tasks.
The Scheduler class should be instantiated to start a task manager session.
Its start method should be called to start the task manager.
Its stop method should be called to end the task manager session.
"""
# region Init
def __init__(self, daemon=True, exceptionHandler=None):
Thread.__init__(self)
self._notifyEvent = Event()
self._nextTime = None
self._scheduled = {}
self._running = {}
self._onDemand = {}
self._isRunning = False
self._exceptionHandler = exceptionHandler
if daemon:
self.daemon = True
# endregion Init
# region Event Methods
def wait(self, seconds=None):
"""Our own version of wait().
When called, it waits for the specified number of seconds, or until
it is notified that it needs to wake up, through the notify event.
"""
try:
self._notifyEvent.wait(seconds)
except IOError:
pass
self._notifyEvent.clear()
# endregion Event Methods
# region Attributes
def runningTasks(self):
"""Return all running tasks."""
return self._running
def running(self, name, default=None):
"""Return running task with given name.
Returns a task with the given name from the "running" list,
if it is present there.
"""
return self._running.get(name, default)
def hasRunning(self, name):
"""Check to see if a task with the given name is currently running."""
return name in self._running
def setRunning(self, handle):
"""Add a task to the running dictionary.
Used internally only.
"""
self._running[handle.name()] = handle
def delRunning(self, name):
"""Delete a task from the running list.
Used internally.
"""
try:
handle = self._running[name]
del self._running[name]
return handle
except Exception:
return None
def scheduledTasks(self):
"""Return all scheduled tasks."""
return self._scheduled
def scheduled(self, name, default=None):
"""Return a task from the scheduled list."""
return self._scheduled.get(name, default)
def hasScheduled(self, name):
"""Checks whether task with given name is in the scheduled list."""
return name in self._scheduled
def setScheduled(self, handle):
"""Add the given task to the scheduled list."""
self._scheduled[handle.name()] = handle
def delScheduled(self, name):
"""Delete a task with the given name from the scheduled list."""
return self._scheduled.pop(name, None)
def onDemandTasks(self):
"""Return all on demand tasks."""
return self._onDemand
def onDemand(self, name, default=None):
"""Return a task from the onDemand list."""
return self._onDemand.get(name, default)
def hasOnDemand(self, name):
"""Checks whether task with given name is in the on demand list."""
return name in self._onDemand
def setOnDemand(self, handle):
"""Add the given task to the on demand list."""
self._onDemand[handle.name()] = handle
def delOnDemand(self, name):
"""Delete a task with the given name from the on demand list."""
return self._onDemand.pop(name, None)
def nextTime(self):
"""Get next execution time."""
return self._nextTime
def setNextTime(self, nextTime):
"""Set next execution time."""
self._nextTime = nextTime
def isRunning(self):
"""Check whether thread is running."""
return self._isRunning
# endregion Attributes
# region Adding Tasks
def addTimedAction(self, actionTime, task, name):
"""Add a task to be run once, at a specific time."""
handle = self.unregisterTask(name)
if handle:
handle.reset(actionTime, 0, task, True)
else:
handle = TaskHandler(self, actionTime, 0, task, name)
self.scheduleTask(handle)
def addActionOnDemand(self, task, name):
"""Add a task to be run only on demand.
Adds a task to the scheduler that will not be scheduled
until specifically requested.
"""
handle = self.unregisterTask(name)
if handle:
handle.reset(time(), 0, task, True)
else:
handle = TaskHandler(self, time(), 0, task, name)
handle.setOnDemand()
self.setOnDemand(handle)
def addDailyAction(self, hour, minute, task, name):
"""Add an action to be run every day at a specific time.
If a task with the given name is already registered with the
scheduler, that task will be removed from the scheduling queue
and registered anew as a periodic task.
Can we make this addCalendarAction? What if we want to run
something once a week? We probably don't need that for Webware,
but this is a more generally useful module. This could be a
difficult function, though. Particularly without mxDateTime.
"""
current = localtime()
currHour = current[3]
currMin = current[4]
if hour > currHour:
hourDifference = hour - currHour
if minute > currMin:
minuteDifference = minute - currMin
elif minute < currMin:
minuteDifference = 60 - currMin + minute
hourDifference -= 1
else:
minuteDifference = 0
elif hour < currHour:
hourDifference = 24 - currHour + hour
if minute > currMin:
minuteDifference = minute - currMin
elif minute < currMin:
minuteDifference = 60 - currMin + minute
hourDifference -= 1
else:
minuteDifference = 0
else:
if minute > currMin:
hourDifference = 0
minuteDifference = minute - currMin
elif minute < currMin:
minuteDifference = 60 - currMin + minute
hourDifference = 23
else:
hourDifference = 0
minuteDifference = 0
delay = (minuteDifference + (hourDifference * 60)) * 60
self.addPeriodicAction(time() + delay, 24 * 60 * 60, task, name)
def addPeriodicAction(self, start, period, task, name):
"""Add a task to be run periodically.
Adds an action to be run at a specific initial time,
and every period thereafter.
The scheduler will not reschedule a task until the last
scheduled instance of the task has completed.
If a task with the given name is already registered with
the scheduler, that task will be removed from the scheduling
queue and registered anew as a periodic task.
"""
handle = self.unregisterTask(name)
if handle:
handle.reset(start, period, task, True)
else:
handle = TaskHandler(self, start, period, task, name)
self.scheduleTask(handle)
# endregion Adding Tasks
# region Task methods
def unregisterTask(self, name):
"""Unregisters the named task.
After that it can be rescheduled with different parameters,
or simply removed.
"""
handle = (self.delRunning(name) or
self.delScheduled(name) or self.delOnDemand(name))
if handle:
handle.unregister()
return handle
def runTaskNow(self, name):
"""Allow a registered task to be immediately executed.
Returns True if the task is either currently running or was started,
or False if the task could not be found in the list of currently
registered tasks.
"""
if self.hasRunning(name):
return True
handle = self.scheduled(name)
if not handle:
handle = self.onDemand(name)
if not handle:
return False
self.runTask(handle)
return True
def demandTask(self, name):
"""Demand execution of a task.
Allow the server to request that a task listed as being registered
on-demand be run as soon as possible.
If the task is currently running, it will be flagged to run again
as soon as the current run completes.
Returns False if the task name could not be found on the on-demand
or currently running lists.
"""
if self.hasRunning(name) or self.hasOnDemand(name):
handle = self.running(name)
if handle:
handle.runOnCompletion()
return True
handle = self.onDemand(name)
if not handle:
return False
self.runTask(handle)
return True
return False
def stopTask(self, name):
"""Put an immediate halt to a running background task.
Returns True if the task was either not running, or was
running and was told to stop.
"""
handle = self.running(name)
if not handle:
return False
handle.stop()
return True
def stopAllTasks(self):
"""Terminate all running tasks."""
for i in self._running:
self.stopTask(i)
def disableTask(self, name):
"""Specify that a task be suspended.
Suspended tasks will not be scheduled until later enabled.
If the task is currently running, it will not be interfered
with, but the task will not be scheduled for execution in
future until re-enabled.
Returns True if the task was found and disabled.
"""
handle = self.running(name)
if not handle:
handle = self.scheduled(name)
if not handle:
return False
handle.disable()
return True
def enableTask(self, name):
"""Enable a task again.
This method is provided to specify that a task be re-enabled
after a suspension. A re-enabled task will be scheduled for
execution according to its original schedule, with any runtimes
that would have been issued during the time the task was suspended
simply skipped.
Returns True if the task was found and enabled.
"""
handle = self.running(name)
if not handle:
handle = self.scheduled(name)
if not handle:
return False
handle.enable()
return True
def runTask(self, handle):
"""Run a task.
Used by the Scheduler thread's main loop to put a task in
the scheduled hash onto the run hash.
"""
name = handle.name()
if self.delScheduled(name) or self.delOnDemand(name):
self.setRunning(handle)
handle.runTask()
def scheduleTask(self, handle):
"""Schedule a task.
This method takes a task that needs to be scheduled and adds it
to the scheduler. All scheduling additions or changes are handled
by this method. This is the only Scheduler method that can notify
the run() method that it may need to wake up early to handle a
newly registered task.
"""
self.setScheduled(handle)
if not self.nextTime() or handle.startTime() < self.nextTime():
self.setNextTime(handle.startTime())
self.notify()
# endregion Task methods
# region Misc Methods
def notifyCompletion(self, handle):
"""Notify completion of a task.
Used by instances of TaskHandler to let the Scheduler thread know
when their tasks have run to completion. This method is responsible
for rescheduling the task if it is a periodic task.
"""
name = handle.name()
if self.hasRunning(name):
self.delRunning(name)
if handle.startTime() and handle.startTime() > time():
self.scheduleTask(handle)
else:
if handle.reschedule():
self.scheduleTask(handle)
elif handle.isOnDemand():
self.setOnDemand(handle)
if handle.runAgain():
self.runTask(handle)
def notifyFailure(self, handle):
"""Notify failure of a task.
Used by instances of TaskHandler to let the Scheduler thread know
if an exception has occurred within the task thread.
"""
self.notifyCompletion(handle)
if self._exceptionHandler is not None:
self._exceptionHandler()
def notify(self):
"""Wake up scheduler by sending a notify even."""
self._notifyEvent.set()
def start(self):
"""Start the scheduler's activity."""
self._isRunning = True
Thread.start(self)
def stop(self):
"""Terminate the scheduler and its associated tasks."""
self._isRunning = False
self.notify()
self.stopAllTasks()
# Wait until the scheduler thread exits; otherwise it's possible for
# the interpreter to exit before this thread has a chance to shut down
# completely, which causes a traceback. Waiting 3 secs should suffice.
self.join(3)
# endregion Misc Methods
# region Main Method
def run(self):
"""The main method of the scheduler running as a background thread.
This method is responsible for carrying out the scheduling work of
this class on a background thread. The basic logic is to wait until
the next action is due to run, move the task from our scheduled
list to our running list, and run it. Other synchronized methods
such as runTask(), scheduleTask(), and notifyCompletion(), may
be called while this method is waiting for something to happen.
These methods modify the data structures that run() uses to
determine its scheduling needs.
"""
while self._isRunning:
if self.nextTime():
nextTime = self.nextTime()
currentTime = time()
if currentTime < nextTime:
sleepTime = nextTime - currentTime
self.wait(sleepTime)
if not self._isRunning:
return
currentTime = time()
if currentTime >= nextTime:
toRun = []
nextRun = None
for handle in list(self._scheduled.values()):
startTime = handle.startTime()
if startTime <= currentTime:
toRun.append(handle)
else:
if not nextRun:
nextRun = startTime
elif startTime < nextRun:
nextRun = startTime
self.setNextTime(nextRun)
for handle in toRun:
self.runTask(handle)
else:
self.wait()
# endregion Main Method