iterating over list which mutated inside loop is not a good idea.
paulfantom committed
Jun 20, 2016 at 10:41 UTC
183e5d0d22b78d52f30dc20cda50c768c9de7c43
1 file changed
+27
-13
plugins.d/python.d.plugin
+27
-13
@@ -267,7 +267,7 @@ class PythonCharts(object):
267
if job.name is not None:
268
prefix = "'" + job.name + "' in "
269
270
- prefix += "'" + job.__module__ + "' "
270
+ prefix += "'" + job.__module__ + MODULE_EXTENSION + "' "
271
self.jobs.remove(job)
272
if reason is None:
273
return
@@ -295,10 +295,14 @@ class PythonCharts(object):
295
This cannot fail thus it is catching every exception
296
If job.check() fails job is stopped
297
"""
298
- for job in self.jobs:
298
+ i = 0
299
+ while i < len(self.jobs):
300
+ job = self.jobs[i]
301
try:
302
if not job.check():
303
self._stop(job, "failed check")
304
+ else:
305
+ i += 1
306
except AttributeError:
307
self._stop(job, "no check")
308
except (UnboundLocalError, Exception) as e:
@@ -311,7 +315,9 @@ class PythonCharts(object):
315
If job.create() fails job is stopped.
316
This is also creating job run time chart.
317
"""
314
- for job in self.jobs:
318
+ i = 0
319
+ while i < len(self.jobs):
320
+ job = self.jobs[i]
321
try:
322
if not job.create():
323
self._stop(job, "failed create")
@@ -327,6 +333,7 @@ class PythonCharts(object):
333
'\n')
334
sys.stdout.write("DIMENSION run_time 'run time' absolute 1 1\n\n")
335
sys.stdout.flush()
336
+ i += 1
337
except AttributeError:
338
self._stop(job, "no create")
339
except (UnboundLocalError, Exception) as e:
@@ -340,12 +347,14 @@ class PythonCharts(object):
347
If there are no more retries, job is stopped.
348
Job is also stopped if it throws an exception.
349
This is also updating job run time chart.
350
+ Return False if job is stopped
351
:param job: object
352
+ :return: boolean
353
"""
354
t_start = time.time()
355
# check if it is time to execute job update() function
356
if job.timetable['next'] > t_start:
348
- return
357
+ return True
358
try:
359
if self.first_run:
360
since_last = 0
@@ -354,15 +363,16 @@ class PythonCharts(object):
363
if not job.update(since_last):
364
if job.retries_left <= 0:
365
self._stop(job, "update failed")
366
+ return False
367
job.retries_left -= 1
368
job.timetable['next'] += job.timetable['freq']
359
- return
369
+ return True
370
except AttributeError:
371
self._stop(job, "no update")
362
- return
372
+ return False
373
except (UnboundLocalError, Exception) as e:
374
self._stop(job, "misbehaving. Reason: " + str(e))
365
- return
375
+ return False
376
t_end = time.time()
377
job.timetable['next'] = t_end - (t_end % job.timetable['freq']) + job.timetable['freq']
378
# draw performance graph
@@ -373,6 +383,7 @@ class PythonCharts(object):
383
job.timetable['last'] = t_start
384
job.retries_left = job.retries
385
self.first_run = False
386
+ return True
387
388
def update(self):
389
"""
@@ -382,12 +393,15 @@ class PythonCharts(object):
393
self.first_run = True
394
while True:
395
next_runs = []
385
- for job in self.jobs:
386
- self._update_job(job)
387
- try:
388
- next_runs.append(job.timetable['next'])
389
- except KeyError:
390
- pass
396
+ i = 0
397
+ while i < len(self.jobs):
398
+ job = self.jobs[i]
399
+ if self._update_job(job):
400
+ try:
401
+ next_runs.append(job.timetable['next'])
402
+ except KeyError:
403
+ pass
404
+ i += 1
405
if len(next_runs) == 0:
406
debug("No plugins loaded")
407
sys.stdout.write("DISABLE\n")