introduce "jobs"
paulfantom committed
Jun 17, 2016 at 16:30 UTC
25d8bc82fa788c194ee3885abad1aa582e529c84
1 file changed
+57
-7
plugins.d/python.d.plugin
+57
-7
@@ -35,26 +35,75 @@ class PythonCharts(object):
35
# load configuration files
36
self._load_configs()
37
38
+ self.jobs = self._create_jobs(self.modules)
39
+
40
# set timetable dict (last execution, next execution and frequency)
41
# set priorities
42
self.timetable = {}
43
freq = 1
42
- for m in self.modules:
44
+ for job in self.jobs:
45
try:
44
- m.priority = int(m.priority)
46
+ job.priority = int(job.priority)
47
except (AttributeError,ValueError):
46
- m.priority = self.default_priority
48
+ job.priority = self.default_priority
49
50
if interval is None:
51
try:
50
- freq = int(m.update_every)
52
+ freq = int(job.update_every)
53
except (AttributeError, ValueError):
54
freq = 1
55
56
now = time.time()
55
- self.timetable[m.__name__] = {'last' : now,
56
- 'next' : now - (now % freq) + freq,
57
- 'freq' : freq}
57
+ self.timetable[job.__name__] = {'last' : now,
58
+ 'next' : now - (now % freq) + freq,
59
+ 'freq' : freq}
60
+
61
+
62
+ def _create_jobs(self,modules):
63
+ # modules are a list of modules to load
64
+ # module store a definition of Service class
65
+ # module store configuration in module.config
66
+ # configs are list of dicts or a dict
67
+ # one dict is one service
68
+ # iterate over list of modules and inside one loop iterate over configs
69
+ jobs = []
70
+ for module in modules:
71
+ if type(module.config) is dict:
72
+ jobs.append(self._job(module,module.config))
73
+ elif type(module.config) is list:
74
+ for conf in module.config:
75
+ jobs.append(self._job(module,conf))
76
+
77
+ return [j for j in jobs if j is not None]
78
+
79
+
80
+ def _job(self,module,conf):
81
+ update = 0
82
+ prio = 0
83
+ retries = 0
84
+ try:
85
+ update = int(conf['update_every'])
86
+ except (KeyError,ValueError):
87
+ update = module.update_every
88
+ try:
89
+ priority = int(conf['priority'])
90
+ except (KeyError,ValueError):
91
+ priority = module.priority
92
+ try:
93
+ retries = int(conf['retries'])
94
+ except (KeyError,ValueError):
95
+ retries = module.retries
96
+
97
+ try:
98
+ return module.Service(configuration=conf,
99
+ update_every=update,
100
+ priority=prio,
101
+ retries=retries)
102
+ except Exception as e:
103
+ print(e)
104
+ #TODO print some error
105
+ return None
106
+
107
108
def _import_plugin(self, path, name=None):
109
# try to import module using only its path
@@ -95,6 +144,7 @@ class PythonCharts(object):
144
145
def _load_configs(self):
146
# function modifies every loaded module in self.modules
147
+ # TODO ensure module has update_every, priority and retries variables
148
for m in self.modules:
149
configfile = self.configs + m.__name__ + ".conf"
150
if os.path.isfile(configfile):