Merge pull request #46995 from s0undt3ch/develop

[schedule][develop] Minor fixes and enhancements
This commit is contained in:
Nicole Thomas 2018-04-10 13:51:10 -04:00 committed by GitHub
commit fb035953f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -91,7 +91,12 @@ class Schedule(object):
# it in a WeakValueDictionary-- which will remove the item if no one
# references it-- this forces a reference while we return to the caller
instance = object.__new__(cls)
instance.__singleton_init__(opts, functions, returners, intervals, cleanup, proxy, standalone)
instance.__singleton_init__(opts, functions,
returners=returners,
intervals=intervals,
cleanup=cleanup,
proxy=proxy,
standalone=standalone)
if new_instance is True:
return instance
cls.instance = instance
@ -858,7 +863,7 @@ class Schedule(object):
data['_next_scheduled_fire_time'] = now + datetime.timedelta(seconds=data['_seconds'])
def _handle_once(job, data, loop_interval):
def _handle_once(data, loop_interval):
'''
Handle schedule item with once
'''
@ -880,7 +885,9 @@ class Schedule(object):
data['_error'] = ('Date string could not '
'be parsed: {0}, {1}. '
'Ignoring job {2}.'.format(
data['once'], once_fmt, job))
data['once'],
once_fmt,
data['name']))
log.error(data['_error'])
return
data['_next_fire_time'] = once
@ -889,13 +896,13 @@ class Schedule(object):
if once < now - loop_interval:
data['_continue'] = True
def _handle_when(job, data, loop_interval):
def _handle_when(data, loop_interval):
'''
Handle schedule item with when
'''
if not _WHEN_SUPPORTED:
data['_error'] = ('Missing python-dateutil. '
'Ignoring job {0}.'.format(job))
'Ignoring job {0}.'.format(data['name']))
log.error(data['_error'])
return
@ -908,7 +915,7 @@ class Schedule(object):
dict):
data['_error'] = ('Pillar item "whens" '
'must be a dict. '
'Ignoring job {0}.'.format(job))
'Ignoring job {0}.'.format(data['name']))
log.error(data['_error'])
return
when_ = self.opts['pillar']['whens'][i]
@ -917,7 +924,7 @@ class Schedule(object):
if not isinstance(self.opts['grains']['whens'],
dict):
data['_error'] = ('Grain "whens" must be a dict.'
'Ignoring job {0}.'.format(job))
'Ignoring job {0}.'.format(data['name']))
log.error(data['_error'])
return
when_ = self.opts['grains']['whens'][i]
@ -929,7 +936,7 @@ class Schedule(object):
when_ = dateutil_parser.parse(when_)
except ValueError:
data['_error'] = ('Invalid date string {0}. '
'Ignoring job {1}.'.format(i, job))
'Ignoring job {1}.'.format(i, data['name']))
log.error(data['_error'])
return
@ -978,7 +985,7 @@ class Schedule(object):
data['when'] in self.opts['pillar']['whens']):
if not isinstance(self.opts['pillar']['whens'], dict):
data['_error'] = ('Pillar item "whens" must be dict.'
'Ignoring job {0}.'.format(job))
'Ignoring job {0}.'.format(data['name']))
log.error(data['_error'])
return
when = self.opts['pillar']['whens'][data['when']]
@ -986,7 +993,7 @@ class Schedule(object):
data['when'] in self.opts['grains']['whens']):
if not isinstance(self.opts['grains']['whens'], dict):
data['_error'] = ('Grain "whens" must be a dict. '
'Ignoring job {0}.'.format(job))
'Ignoring job {0}.'.format(data['name']))
log.error(data['_error'])
return
when = self.opts['grains']['whens'][data['when']]
@ -998,7 +1005,7 @@ class Schedule(object):
when = dateutil_parser.parse(when)
except ValueError:
data['_error'] = ('Invalid date string. '
'Ignoring job {0}.'.format(job))
'Ignoring job {0}.'.format(data['name']))
log.error(data['_error'])
return
@ -1022,13 +1029,13 @@ class Schedule(object):
data['_next_fire_time'] = when
data['_run'] = True
def _handle_cron(job, data, loop_interval):
def _handle_cron(data, loop_interval):
'''
Handle schedule item with cron
'''
if not _CRON_SUPPORTED:
data['_error'] = ('Missing python-croniter. '
'Ignoring job {0}.'.format(job))
'Ignoring job {0}.'.format(data['name']))
log.error(data['_error'])
return
@ -1040,7 +1047,7 @@ class Schedule(object):
data['_next_scheduled_fire_time'] = croniter.croniter(data['cron'], now).get_next(datetime.datetime)
except (ValueError, KeyError):
data['_error'] = ('Invalid cron string. '
'Ignoring job {0}.'.format(job))
'Ignoring job {0}.'.format(data['name']))
log.error(data['_error'])
return
@ -1108,20 +1115,20 @@ class Schedule(object):
else:
data['run'] = True
def _handle_skip_during_range(job, data, loop_interval):
def _handle_skip_during_range(data, loop_interval):
'''
Handle schedule item with skip_explicit
'''
if not _RANGE_SUPPORTED:
data['_error'] = ('Missing python-dateutil. '
'Ignoring job {0}.'.format(job))
'Ignoring job {0}.'.format(data['name']))
log.error(data['_error'])
return
if not isinstance(data['skip_during_range'], dict):
data['_error'] = ('schedule.handle_func: Invalid, range '
'must be specified as a dictionary. '
'Ignoring job {0}.'.format(job))
'Ignoring job {0}.'.format(data['name']))
log.error(data['_error'])
return
@ -1133,7 +1140,7 @@ class Schedule(object):
except ValueError:
data['_error'] = ('Invalid date string for start in '
'skip_during_range. Ignoring '
'job {0}.'.format(job))
'job {0}.'.format(data['name']))
log.error(data['_error'])
return
@ -1143,7 +1150,7 @@ class Schedule(object):
except ValueError:
data['_error'] = ('Invalid date string for end in '
'skip_during_range. Ignoring '
'job {0}.'.format(job))
'job {0}.'.format(data['name']))
log.error(data['_error'])
return
@ -1175,23 +1182,23 @@ class Schedule(object):
else:
data['_error'] = ('schedule.handle_func: Invalid '
'range, end must be larger than '
'start. Ignoring job {0}.'.format(job))
'start. Ignoring job {0}.'.format(data['name']))
log.error(data['_error'])
def _handle_range(job, data):
def _handle_range(data):
'''
Handle schedule item with skip_explicit
'''
if not _RANGE_SUPPORTED:
data['_error'] = ('Missing python-dateutil. '
'Ignoring job {0}'.format(job))
'Ignoring job {0}'.format(data['name']))
log.error(data['_error'])
return
if not isinstance(data['range'], dict):
data['_error'] = ('schedule.handle_func: Invalid, range '
'must be specified as a dictionary.'
'Ignoring job {0}.'.format(job))
'Ignoring job {0}.'.format(data['name']))
log.error(data['_error'])
return
@ -1202,7 +1209,7 @@ class Schedule(object):
start = dateutil_parser.parse(start)
except ValueError:
data['_error'] = ('Invalid date string for start. '
'Ignoring job {0}.'.format(job))
'Ignoring job {0}.'.format(data['name']))
log.error(data['_error'])
return
@ -1211,7 +1218,7 @@ class Schedule(object):
end = dateutil_parser.parse(end)
except ValueError:
data['_error'] = ('Invalid date string for end.'
' Ignoring job {0}.'.format(job))
' Ignoring job {0}.'.format(data['name']))
log.error(data['_error'])
return
@ -1235,16 +1242,16 @@ class Schedule(object):
else:
data['_error'] = ('schedule.handle_func: Invalid '
'range, end must be larger '
'than start. Ignoring job {0}.'.format(job))
'than start. Ignoring job {0}.'.format(data['name']))
log.error(data['_error'])
def _handle_after(job, data):
def _handle_after(data):
'''
Handle schedule item with after
'''
if not _WHEN_SUPPORTED:
data['_error'] = ('Missing python-dateutil. '
'Ignoring job {0}'.format(job))
'Ignoring job {0}'.format(data['name']))
log.error(data['_error'])
return
@ -1264,13 +1271,13 @@ class Schedule(object):
else:
data['run'] = True
def _handle_until(job, data):
def _handle_until(data):
'''
Handle schedule item with until
'''
if not _WHEN_SUPPORTED:
data['_error'] = ('Missing python-dateutil. '
'Ignoring job {0}'.format(job))
'Ignoring job {0}'.format(data['name']))
log.error(data['_error'])
return
@ -1320,10 +1327,15 @@ class Schedule(object):
del data[item]
run = False
if 'name' in data:
job_name = data['name']
else:
job_name = data['name'] = job
if not isinstance(data, dict):
log.error(
'Scheduled job "%s" should have a dict value, not %s',
job, type(data)
job_name, type(data)
)
continue
if 'function' in data:
@ -1337,10 +1349,8 @@ class Schedule(object):
if func not in self.functions:
log.info(
'Invalid function: %s in scheduled job %s.',
func, job
func, job_name
)
if 'name' not in data:
data['name'] = job
if '_next_fire_time' not in data:
data['_next_fire_time'] = None
@ -1395,11 +1405,11 @@ class Schedule(object):
if True in [True for item in time_elements if item in data]:
_handle_time_elements(data)
elif 'once' in data:
_handle_once(job, data, loop_interval)
_handle_once(data, loop_interval)
elif 'when' in data:
_handle_when(job, data, loop_interval)
_handle_when(data, loop_interval)
elif 'cron' in data:
_handle_cron(job, data, loop_interval)
_handle_cron(data, loop_interval)
else:
continue
@ -1459,7 +1469,7 @@ class Schedule(object):
data['_run_on_start'] = False
elif run:
if 'range' in data:
_handle_range(job, data)
_handle_range(data)
# An error occurred so we bail out
if '_error' in data and data['_error']:
@ -1476,7 +1486,7 @@ class Schedule(object):
data['skip_during_range'] = self.skip_during_range
if 'skip_during_range' in data and data['skip_during_range']:
_handle_skip_during_range(job, data, loop_interval)
_handle_skip_during_range(data, loop_interval)
# An error occurred so we bail out
if '_error' in data and data['_error']:
@ -1500,7 +1510,7 @@ class Schedule(object):
func = data['func']
if 'until' in data:
_handle_until(job, data)
_handle_until(data)
# An error occurred so we bail out
if '_error' in data and data['_error']:
@ -1509,7 +1519,7 @@ class Schedule(object):
run = data['run']
if 'after' in data:
_handle_after(job, data)
_handle_after(data)
# An error occurred so we bail out
if '_error' in data and data['_error']:
@ -1530,7 +1540,7 @@ class Schedule(object):
# Job is disabled, set run to False
if 'enabled' in data and not data['enabled']:
log.debug('Job: %s is disabled', job)
log.debug('Job: %s is disabled', job_name)
data['_skip_reason'] = 'disabled'
run = False
@ -1542,7 +1552,7 @@ class Schedule(object):
try:
# Job is disabled, continue
if 'enabled' in data and not data['enabled']:
log.debug('Job: %s is disabled', job)
log.debug('Job: %s is disabled', job_name)
data['_skip_reason'] = 'disabled'
data['_skipped_time'] = now
data['_skipped'] = True
@ -1550,14 +1560,15 @@ class Schedule(object):
if 'jid_include' not in data or data['jid_include']:
data['jid_include'] = True
log.debug('schedule: This job was scheduled with jid_include, '
'adding to cache (jid_include defaults to True)')
log.debug('schedule: Job %s was scheduled with jid_include, '
'adding to cache (jid_include defaults to True)',
job_name)
if 'maxrunning' in data:
log.debug('schedule: This job was scheduled with a max '
'number of %s', data['maxrunning'])
log.debug('schedule: Job %s was scheduled with a max '
'number of %s', job_name, data['maxrunning'])
else:
log.info('schedule: maxrunning parameter was not specified for '
'job %s, defaulting to 1.', job)
'job %s, defaulting to 1.', job_name)
data['maxrunning'] = 1
if not self.standalone:
@ -1569,15 +1580,15 @@ class Schedule(object):
run = data['run']
if run:
log.info('Running scheduled job: %s%s', job, miss_msg)
log.info('Running scheduled job: %s%s', job_name, miss_msg)
self._run_job(func, data)
finally:
# Only set _last_run if the job ran
if run:
data['_last_run'] = now
if '_seconds' in data:
data['_next_fire_time'] = now + datetime.timedelta(seconds=data['_seconds'])
data['_splay'] = None
if '_seconds' in data:
data['_next_fire_time'] = now + datetime.timedelta(seconds=data['_seconds'])
def _run_job(self, func, data):
job_dry_run = data.get('dry_run', False)