Merge "reset job interval when audit was updated"

This commit is contained in:
Zuul 2018-01-11 09:12:38 +00:00 committed by Gerrit Code Review
commit 7297603f65
2 changed files with 51 additions and 6 deletions

View File

@ -129,14 +129,25 @@ class ContinuousAuditHandler(base.AuditHandler):
audits = objects.Audit.list(
audit_context, filters=audit_filters, eager=True)
scheduler_job_args = [
job.args for job in self.scheduler.get_jobs()
(job.args[0].uuid, job) for job
in self.scheduler.get_jobs()
if job.name == 'execute_audit']
for args in scheduler_job_args:
if self._is_audit_inactive(args[0]):
scheduler_job_args.remove(args)
scheduler_jobs = dict(scheduler_job_args)
# if audit isn't in active states, audit's job should be removed
for job in scheduler_jobs.values():
if self._is_audit_inactive(job.args[0]):
scheduler_jobs.pop(job.args[0].uuid)
for audit in audits:
# if audit is not presented in scheduled audits yet.
if audit.uuid not in [arg[0].uuid for arg in scheduler_job_args]:
existing_job = scheduler_jobs.get(audit.uuid, None)
# if audit is not presented in scheduled audits yet,
# just add a new audit job.
# if audit is already in the job queue, and interval has changed,
# we need to remove the old job and add a new one.
if (existing_job is None) or (
existing_job and
audit.interval != existing_job.args[0].interval):
if existing_job:
self.scheduler.remove_job(existing_job.id)
# if interval is provided with seconds
if utils.is_int_like(audit.interval):
# if audit has already been provided and we need

View File

@ -383,3 +383,37 @@ class TestContinuousAuditHandler(base.DbTestCase):
audit_handler.execute_audit(self.audits[0], self.context)
m_execute.assert_called_once_with(self.audits[0], self.context)
self.assertIsNotNone(self.audits[0].next_run_time)
@mock.patch.object(objects.service.Service, 'list')
@mock.patch.object(sq_api, 'get_engine')
@mock.patch.object(scheduling.BackgroundSchedulerService, 'remove_job')
@mock.patch.object(scheduling.BackgroundSchedulerService, 'add_job')
@mock.patch.object(scheduling.BackgroundSchedulerService, 'get_jobs')
@mock.patch.object(objects.audit.Audit, 'list')
def test_launch_audits_periodically_with_diff_interval(
self, mock_list, mock_jobs, m_add_job, m_remove_job,
m_engine, m_service):
audit_handler = continuous.ContinuousAuditHandler()
mock_list.return_value = self.audits
self.audits[0].next_run_time = (datetime.datetime.now() -
datetime.timedelta(seconds=1800))
m_job1 = mock.MagicMock()
m_job1.name = 'execute_audit'
m_audit = mock.MagicMock()
m_audit.uuid = self.audits[0].uuid
m_audit.interval = 60
m_job1.args = [m_audit]
mock_jobs.return_value = [m_job1]
m_engine.return_value = mock.MagicMock()
m_add_job.return_value = mock.MagicMock()
audit_handler.launch_audits_periodically()
m_service.assert_called()
m_engine.assert_called()
m_add_job.assert_called()
mock_jobs.assert_called()
self.assertIsNotNone(self.audits[0].next_run_time)
self.assertIsNone(self.audits[1].next_run_time)
audit_handler.launch_audits_periodically()
m_remove_job.assert_called()