Merge pull request #50216 from garethgreenaway/50162_when_plus_splay_endless_loop

[2018.3] Fixes to scheduler, list of whens plus splay
This commit is contained in:
Nicole Thomas 2018-10-25 09:31:25 -04:00 committed by GitHub
commit 51b3fa4bb6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 78 additions and 0 deletions

View file

@ -1059,6 +1059,13 @@ class Schedule(object):
# last scheduled time in the past.
when = _when[0]
if when < now - loop_interval and \
not data.get('_run', False) and \
not run and \
not data['_splay']:
data['_next_fire_time'] = None
continue
if '_run' not in data:
# Prevent run of jobs from the past
data['_run'] = bool(when >= now - loop_interval)
@ -1179,6 +1186,8 @@ class Schedule(object):
if data['_splay']:
# The "splay" configuration has been already processed, just use it
seconds = (data['_splay'] - now).total_seconds()
if 'when' in data:
data['_next_fire_time'] = data['_splay']
if '_seconds' in data:
if seconds <= 0:

View file

@ -709,3 +709,72 @@ class SchedulerEvalTest(ModuleCase, SaltReturnAssertsMixin):
ret = self.schedule.job_status(job_name)
self.assertEqual(ret['_last_run'], run_time)
self.assertEqual(ret['_next_fire_time'], next_run_time)
def test_eval_when_splay(self):
'''
verify that scheduled job runs
'''
job_name = 'test_eval_when_splay'
splay = 300
job = {
'schedule': {
job_name: {
'function': 'test.ping',
'when': '11/29/2017 4:00pm',
'splay': splay
}
}
}
run_time1 = dateutil_parser.parse('11/29/2017 4:00pm')
run_time2 = run_time1 + datetime.timedelta(seconds=splay)
# Add the job to the scheduler
self.schedule.opts.update(job)
with patch('random.randint', MagicMock(return_value=splay)):
# Evaluate to prime
run_time = dateutil_parser.parse('11/29/2017 3:00pm')
self.schedule.eval(now=run_time)
ret = self.schedule.job_status(job_name)
# Evaluate at expected runtime1, should not run
self.schedule.eval(now=run_time1)
ret = self.schedule.job_status(job_name)
self.assertNotIn('_last_run', ret)
# Evaluate at expected runtime2, should run
self.schedule.eval(now=run_time2)
ret = self.schedule.job_status(job_name)
self.assertEqual(ret['_last_run'], run_time2)
def test_eval_when_splay_in_past(self):
'''
verify that scheduled job runs
'''
job_name = 'test_eval_when_splay_in_past'
splay = 300
job = {
'schedule': {
job_name: {
'function': 'test.ping',
'when': ['11/29/2017 6:00am'],
'splay': splay
}
}
}
run_time1 = dateutil_parser.parse('11/29/2017 4:00pm')
# Add the job to the scheduler
self.schedule.opts.update(job)
# Evaluate to prime
run_time = dateutil_parser.parse('11/29/2017 3:00pm')
self.schedule.eval(now=run_time)
ret = self.schedule.job_status(job_name)
# Evaluate at expected runtime1, should not run
# and _next_fire_time should be None
self.schedule.eval(now=run_time1)
ret = self.schedule.job_status(job_name)
self.assertNotIn('_last_run', ret)
self.assertEqual(ret['_next_fire_time'], None)