This repository has been archived by the owner on Aug 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
job.py
54 lines (47 loc) · 1.68 KB
/
job.py
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
from twisted.internet.task import LoopingCall
import copy
class JobQueue(object):
"""Create and poll jobs"""
threads = []
jobs_def = []
def __init__(self, definition, sink, interval):
"""
Read job definitions from a config source, create an instance of the job using its configuration, and store the config for reference.
"""
self.sink = sink
self.interval = interval
JobQueue.jobs_def = []
for type_name, options in definition.items():
classname = type_name.capitalize() + "Poller"
m = __import__(type_name, fromlist=[classname])
if hasattr(m, classname):
klass = getattr(m, classname)
job = klass(**options)
job.config = copy.deepcopy(options)
job.config['class'] = type_name
JobQueue.jobs_def.append(job)
else:
raise Exception("Failed to create job of type " + classname)
@staticmethod
def describe():
jobs_desc = ", ".join(
[("%s: %s" % (j.config['class'], j.config))
for j in JobQueue.jobs_def]
)
return jobs_desc
def check(self):
for job in JobQueue.jobs_def:
for line in job.check():
if line:
self.sink.write(line)
@staticmethod
def killall():
for old in JobQueue.threads:
old.stop()
JobQueue.threads = []
def run(self):
JobQueue.killall()
task = LoopingCall(self.check)
JobQueue.threads = [task]
task.start(self.interval)
print "Started polling jobs, every %d seconds." % (self.interval, )