From 892c54f6b5ccc309cb54a37b8fe3cca659ef93d3 Mon Sep 17 00:00:00 2001 From: Alexander Haase Date: Mon, 7 Oct 2024 00:40:30 +0200 Subject: [PATCH] Fix check for existing jobs If a job is to be enqueued once and no specific scheduled time is specified, any scheduled time of existing jobs will be valid. Only if a specific scheduled time is specified for 'enqueue_once()' can it be evaluated. --- netbox/netbox/jobs.py | 2 +- netbox/netbox/tests/test_jobs.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/netbox/netbox/jobs.py b/netbox/netbox/jobs.py index 087c24896..b357c7144 100644 --- a/netbox/netbox/jobs.py +++ b/netbox/netbox/jobs.py @@ -127,7 +127,7 @@ class JobRunner(ABC): if job: # If the job parameters haven't changed, don't schedule a new job and keep the current schedule. Otherwise, # delete the existing job and schedule a new job instead. - if (schedule_at and job.scheduled == schedule_at) and (job.interval == interval): + if (not schedule_at or job.scheduled == schedule_at) and (job.interval == interval): return job job.delete() diff --git a/netbox/netbox/tests/test_jobs.py b/netbox/netbox/tests/test_jobs.py index cb3024038..fec9ba837 100644 --- a/netbox/netbox/tests/test_jobs.py +++ b/netbox/netbox/tests/test_jobs.py @@ -90,6 +90,15 @@ class EnqueueTest(JobRunnerTestCase): self.assertEqual(job1, job2) self.assertEqual(TestJobRunner.get_jobs(instance).count(), 1) + def test_enqueue_once_twice_same_no_schedule_at(self): + instance = Job() + schedule_at = self.get_schedule_at() + job1 = TestJobRunner.enqueue_once(instance, schedule_at=schedule_at) + job2 = TestJobRunner.enqueue_once(instance) + + self.assertEqual(job1, job2) + self.assertEqual(TestJobRunner.get_jobs(instance).count(), 1) + def test_enqueue_once_twice_different_schedule_at(self): instance = Job() job1 = TestJobRunner.enqueue_once(instance, schedule_at=self.get_schedule_at())