Merge pull request #44984 from garethgreenaway/scheduler_fixes_loop_interval

[oxygen] scheduler loop interval fixes
This commit is contained in:
Nicole Thomas 2017-12-21 15:11:09 -05:00 committed by GitHub
commit 67df35ecd3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 157 additions and 4 deletions

View file

@ -923,7 +923,8 @@ class Schedule(object):
elif 'once' in data:
if data['_next_fire_time'] and \
data['_next_fire_time'] != now and \
data['_next_fire_time'] < now - self.opts['loop_interval'] and \
data['_next_fire_time'] > now and \
not data['_splay']:
continue
@ -939,7 +940,10 @@ class Schedule(object):
log.error('Date string could not be parsed: %s, %s',
data['once'], once_fmt)
continue
if data['_next_fire_time'] != now:
# If _next_fire_time is less than now or greater
# than now, continue.
if data['_next_fire_time'] < now - self.opts['loop_interval'] and \
data['_next_fire_time'] > now:
continue
elif 'when' in data:
@ -1007,7 +1011,7 @@ class Schedule(object):
if '_run' not in data:
# Prevent run of jobs from the past
data['_run'] = bool(when >= now)
data['_run'] = bool(when >= now - self.opts['loop_interval'])
if not data['_next_fire_time']:
data['_next_fire_time'] = when
@ -1054,7 +1058,7 @@ class Schedule(object):
continue
when = int(time.mktime(when__.timetuple()))
if when < now and \
if when < now - self.opts['loop_interval'] and \
not data.get('_run', False) and \
not run and \
not data['_splay']:
@ -1133,6 +1137,9 @@ class Schedule(object):
if seconds <= 0:
data['_next_fire_time'] = None
run = True
elif 'once' in data:
if data['_next_fire_time'] <= now <= (data['_next_fire_time'] + self.opts['loop_interval']):
run = True
elif seconds == 0:
run = True

View file

@ -5,6 +5,7 @@ from __future__ import absolute_import
import copy
import logging
import os
import random
import time
import dateutil.parser as dateutil_parser
@ -42,6 +43,7 @@ class SchedulerEvalTest(ModuleCase, SaltReturnAssertsMixin):
with patch('salt.utils.schedule.clean_proc_dir', MagicMock(return_value=None)):
functions = {'test.ping': ping}
self.schedule = salt.utils.schedule.Schedule(copy.deepcopy(DEFAULT_CONFIG), functions, returners={})
self.schedule.opts['loop_interval'] = 1
def test_eval(self):
'''
@ -70,3 +72,145 @@ class SchedulerEvalTest(ModuleCase, SaltReturnAssertsMixin):
self.schedule.eval(now=run_time2)
ret = self.schedule.job_status('job1')
self.assertEqual(ret['_last_run'], run_time2)
def test_eval_multiple_whens(self):
'''
verify that scheduled job runs
'''
job = {
'schedule': {
'job1': {
'function': 'test.ping',
'when': [
'11/29/2017 4:00pm',
'11/29/2017 5:00pm',
]
}
}
}
run_time1 = int(time.mktime(dateutil_parser.parse('11/29/2017 4:00pm').timetuple()))
run_time2 = int(time.mktime(dateutil_parser.parse('11/29/2017 5:00pm').timetuple()))
# Add the job to the scheduler
self.schedule.opts.update(job)
# Evaluate run time1
self.schedule.eval(now=run_time1)
ret = self.schedule.job_status('job1')
self.assertEqual(ret['_last_run'], run_time1)
# Evaluate run time2
self.schedule.eval(now=run_time2)
ret = self.schedule.job_status('job1')
self.assertEqual(ret['_last_run'], run_time2)
def test_eval_loop_interval(self):
'''
verify that scheduled job runs
'''
job = {
'schedule': {
'job1': {
'function': 'test.ping',
'when': '11/29/2017 4:00pm',
}
}
}
# 30 second loop interval
LOOP_INTERVAL = random.randint(30, 59)
self.schedule.opts['loop_interval'] = LOOP_INTERVAL
run_time2 = int(time.mktime(dateutil_parser.parse('11/29/2017 4:00pm').timetuple()))
# Add the job to the scheduler
self.schedule.opts.update(job)
# Evaluate 1 second at the run time
self.schedule.eval(now=run_time2 + LOOP_INTERVAL)
ret = self.schedule.job_status('job1')
self.assertEqual(ret['_last_run'], run_time2 + LOOP_INTERVAL)
def test_eval_multiple_whens_loop_interval(self):
'''
verify that scheduled job runs
'''
job = {
'schedule': {
'job1': {
'function': 'test.ping',
'when': [
'11/29/2017 4:00pm',
'11/29/2017 5:00pm',
]
}
}
}
# 30 second loop interval
LOOP_INTERVAL = random.randint(30, 59)
self.schedule.opts['loop_interval'] = LOOP_INTERVAL
run_time1 = int(time.mktime(dateutil_parser.parse('11/29/2017 4:00pm').timetuple()))
run_time2 = int(time.mktime(dateutil_parser.parse('11/29/2017 5:00pm').timetuple()))
# Add the job to the scheduler
self.schedule.opts.update(job)
# Evaluate 1 second at the run time
self.schedule.eval(now=run_time1 + LOOP_INTERVAL)
ret = self.schedule.job_status('job1')
self.assertEqual(ret['_last_run'], run_time1 + LOOP_INTERVAL)
# Evaluate 1 second at the run time
self.schedule.eval(now=run_time2 + LOOP_INTERVAL)
ret = self.schedule.job_status('job1')
self.assertEqual(ret['_last_run'], run_time2 + LOOP_INTERVAL)
def test_eval_once(self):
'''
verify that scheduled job runs
'''
job = {
'schedule': {
'job1': {
'function': 'test.ping',
'once': '2017-12-13T13:00:00',
}
}
}
run_time = int(time.mktime(dateutil_parser.parse('12/13/2017 1:00pm').timetuple()))
# Add the job to the scheduler
self.schedule.opts.update(job)
# Evaluate 1 second at the run time
self.schedule.eval(now=run_time)
ret = self.schedule.job_status('job1')
self.assertEqual(ret['_last_run'], run_time)
def test_eval_once_loop_interval(self):
'''
verify that scheduled job runs
'''
job = {
'schedule': {
'job1': {
'function': 'test.ping',
'once': '2017-12-13T13:00:00',
}
}
}
# Randomn second loop interval
LOOP_INTERVAL = random.randint(0, 59)
self.schedule.opts['loop_interval'] = LOOP_INTERVAL
# Run the job at the right plus LOOP_INTERVAL
run_time = int(time.mktime(dateutil_parser.parse('12/13/2017 1:00:{0}pm'.format(LOOP_INTERVAL)).timetuple()))
# Add the job to the scheduler
self.schedule.opts.update(job)
# Evaluate at the run time
self.schedule.eval(now=run_time)
ret = self.schedule.job_status('job1')
self.assertEqual(ret['_last_run'], run_time)

View file

@ -39,6 +39,7 @@ class SchedulerPostponeTest(ModuleCase, SaltReturnAssertsMixin):
with patch('salt.utils.schedule.clean_proc_dir', MagicMock(return_value=None)):
functions = {'test.ping': ping}
self.schedule = salt.utils.schedule.Schedule(copy.deepcopy(DEFAULT_CONFIG), functions, returners={})
self.schedule.opts['loop_interval'] = 1
def test_postpone(self):
'''

View file

@ -42,6 +42,7 @@ class SchedulerSkipTest(ModuleCase, SaltReturnAssertsMixin):
with patch('salt.utils.schedule.clean_proc_dir', MagicMock(return_value=None)):
functions = {'test.ping': ping}
self.schedule = salt.utils.schedule.Schedule(copy.deepcopy(DEFAULT_CONFIG), functions, returners={})
self.schedule.opts['loop_interval'] = 1
def test_skip(self):
'''