Skip to content

Commit 408bc56

Browse files
authored
Merge pull request #818 from devagupt/4.0
Adding exception handling in running schedules
2 parents fb80333 + 1c8b8f5 commit 408bc56

File tree

1 file changed

+39
-34
lines changed

1 file changed

+39
-34
lines changed

hubblestack/daemon.py

+39-34
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import json
1010
import logging
1111
import math
12+
import traceback
1213
import os
1314
import pprint
1415
import re
@@ -307,41 +308,45 @@ def schedule():
307308
if 'user_schedule' in __opts__ and isinstance(__opts__['user_schedule'], dict):
308309
schedule_config.update(__opts__['user_schedule'])
309310
for jobname, jobdata in schedule_config.items():
310-
# Error handling galore
311-
if not jobdata or not isinstance(jobdata, dict):
312-
log.error('Scheduled job %s does not have valid data', jobname)
313-
continue
314-
if 'function' not in jobdata or 'seconds' not in jobdata:
315-
log.error('Scheduled job %s is missing a ``function`` or ``seconds`` argument', jobname)
316-
continue
317-
func = jobdata['function']
318-
if func not in __salt__:
319-
log.error('Scheduled job %s has a function %s which could not be found.', jobname, func)
320-
continue
321311
try:
322-
if 'cron' in jobdata:
323-
seconds = getsecondsbycronexpression(base, jobdata['cron'])
324-
else:
325-
seconds = int(jobdata['seconds'])
326-
splay = int(jobdata.get('splay', 0))
327-
min_splay = int(jobdata.get('min_splay', 0))
328-
except ValueError:
329-
log.error('Scheduled job %s has an invalid value for seconds or splay.', jobname)
330-
args = jobdata.get('args', [])
331-
if not isinstance(args, list):
332-
log.error('Scheduled job %s has args not formed as a list: %s', jobname, args)
333-
kwargs = jobdata.get('kwargs', {})
334-
if not isinstance(kwargs, dict):
335-
log.error('Scheduled job %s has kwargs not formed as a dict: %s', jobname, kwargs)
336-
returners = jobdata.get('returner', [])
337-
if not isinstance(returners, list):
338-
returners = [returners]
339-
# Actually process the job
340-
run = _process_job(jobdata, splay, seconds, min_splay, base)
341-
if run:
342-
_execute_function(jobdata, func, returners, args, kwargs)
343-
sf_count += 1
344-
312+
# Error handling galore
313+
if not jobdata or not isinstance(jobdata, dict):
314+
log.error('Scheduled job %s does not have valid data', jobname)
315+
continue
316+
if 'function' not in jobdata or 'seconds' not in jobdata:
317+
log.error('Scheduled job %s is missing a ``function`` or ``seconds`` argument', jobname)
318+
continue
319+
func = jobdata['function']
320+
if func not in __salt__:
321+
log.error('Scheduled job %s has a function %s which could not be found.', jobname, func)
322+
continue
323+
try:
324+
if 'cron' in jobdata:
325+
seconds = getsecondsbycronexpression(base, jobdata['cron'])
326+
else:
327+
seconds = int(jobdata['seconds'])
328+
splay = int(jobdata.get('splay', 0))
329+
min_splay = int(jobdata.get('min_splay', 0))
330+
except ValueError:
331+
log.error('Scheduled job %s has an invalid value for seconds or splay.', jobname)
332+
args = jobdata.get('args', [])
333+
if not isinstance(args, list):
334+
log.error('Scheduled job %s has args not formed as a list: %s', jobname, args)
335+
kwargs = jobdata.get('kwargs', {})
336+
if not isinstance(kwargs, dict):
337+
log.error('Scheduled job %s has kwargs not formed as a dict: %s', jobname, kwargs)
338+
returners = jobdata.get('returner', [])
339+
if not isinstance(returners, list):
340+
returners = [returners]
341+
# Actually process the job
342+
run = _process_job(jobdata, splay, seconds, min_splay, base)
343+
if run:
344+
_execute_function(jobdata, func, returners, args, kwargs)
345+
sf_count += 1
346+
except Exception as e:
347+
log.error("Exception in running job: {0}. Exception: {1} . Continuing with next job....".format(jobname, e))
348+
tb = traceback.format_exc()
349+
log.error("Exception stacktrace: {0}".format(tb))
345350
return sf_count
346351

347352

0 commit comments

Comments
 (0)