|
|
@@ -172,6 +172,28 @@ class JobRunnerTestCase(BaseJobRunnerTestCase):
|
|
|
job_end.send.assert_not_called()
|
|
|
self.assertFalse(Job.objects.filter(pk=stale_job.pk).exists())
|
|
|
|
|
|
+ @tag('regression') # Ref: #23844
|
|
|
+ def test_handle_job_deleted_during_run(self):
|
|
|
+ """A Job deleted while running is not re-created and keeps its schedule."""
|
|
|
+ for interval in (None, 60):
|
|
|
+ with self.subTest(interval=interval):
|
|
|
+ job = Job.objects.create(name=f'Deleted job {interval}', job_id=uuid.uuid4(), interval=interval)
|
|
|
+
|
|
|
+ def delete_job():
|
|
|
+ Job.objects.get(pk=job.pk).delete()
|
|
|
+
|
|
|
+ with (
|
|
|
+ patch.object(TestJobRunner, 'run', side_effect=delete_job),
|
|
|
+ patch.object(job, '_dispatch_termination') as dispatch,
|
|
|
+ self.assertLogs('netbox.jobs', 'WARNING'),
|
|
|
+ ):
|
|
|
+ TestJobRunner.handle(job)
|
|
|
+
|
|
|
+ self.assertFalse(Job.objects.filter(pk=job.pk).exists())
|
|
|
+ dispatch.assert_not_called()
|
|
|
+ successors = Job.objects.filter(name=job.name).values_list('status', 'interval')
|
|
|
+ self.assertEqual(list(successors), [(JobStatusChoices.STATUS_SCHEDULED, 60)] if interval else [])
|
|
|
+
|
|
|
def test_handle_non_waiting_job(self):
|
|
|
"""A running or terminated Job neither runs nor reschedules and keeps its row."""
|
|
|
for status in (JobStatusChoices.STATUS_RUNNING, *JobStatusChoices.TERMINAL_STATE_CHOICES):
|
|
|
@@ -331,6 +353,26 @@ class JobRunnerTestCase(BaseJobRunnerTestCase):
|
|
|
send_end.assert_called_once_with(job)
|
|
|
self.assertEqual(Notification.objects.filter(user=user, object_id=job.pk).count(), 1)
|
|
|
|
|
|
+ @tag('regression') # Ref: #23844
|
|
|
+ def test_handle_deleted_job_with_invalid_reschedule(self):
|
|
|
+ """A reschedule rejected as invalid does not re-create a Job deleted while running."""
|
|
|
+ job = Job.objects.create(name=TestJobRunner.name, job_id=uuid.uuid4(), interval=60)
|
|
|
+
|
|
|
+ def delete_job():
|
|
|
+ Job.objects.get(pk=job.pk).delete()
|
|
|
+
|
|
|
+ with (
|
|
|
+ patch.object(TestJobRunner, 'run', side_effect=delete_job),
|
|
|
+ patch.object(TestJobRunner, 'enqueue', side_effect=ValidationError('Simulated validation error')),
|
|
|
+ patch.object(job, '_dispatch_termination') as dispatch,
|
|
|
+ disable_logging(),
|
|
|
+ ):
|
|
|
+ TestJobRunner.handle(job)
|
|
|
+
|
|
|
+ self.assertIn('Simulated validation error', job.log_entries[-1]['message'])
|
|
|
+ self.assertFalse(Job.objects.filter(pk=job.pk).exists())
|
|
|
+ dispatch.assert_not_called()
|
|
|
+
|
|
|
def test_handle_terminates_job_when_reschedule_fails(self):
|
|
|
"""An unexpected reschedule failure still ends the job once, then propagates."""
|
|
|
user = User.objects.create_user(username='job-owner')
|
|
|
@@ -350,6 +392,25 @@ class JobRunnerTestCase(BaseJobRunnerTestCase):
|
|
|
send_end.assert_called_once_with(job)
|
|
|
self.assertEqual(Notification.objects.filter(user=user, object_id=job.pk).count(), 1)
|
|
|
|
|
|
+ def test_handle_deleted_job_with_reschedule_error(self):
|
|
|
+ """An unexpected reschedule failure still propagates for a Job deleted while running."""
|
|
|
+ job = Job.objects.create(name=TestJobRunner.name, job_id=uuid.uuid4(), interval=60)
|
|
|
+
|
|
|
+ def delete_job():
|
|
|
+ Job.objects.get(pk=job.pk).delete()
|
|
|
+
|
|
|
+ with (
|
|
|
+ patch.object(TestJobRunner, 'run', side_effect=delete_job),
|
|
|
+ patch.object(TestJobRunner, 'enqueue', side_effect=RuntimeError('Simulated queue error')),
|
|
|
+ patch.object(job, '_dispatch_termination') as dispatch,
|
|
|
+ disable_warnings('netbox.jobs'),
|
|
|
+ self.assertRaisesMessage(RuntimeError, 'Simulated queue error'),
|
|
|
+ ):
|
|
|
+ TestJobRunner.handle(job)
|
|
|
+
|
|
|
+ self.assertFalse(Job.objects.filter(pk=job.pk).exists())
|
|
|
+ dispatch.assert_not_called()
|
|
|
+
|
|
|
def test_handle_terminates_job_once_when_end_receiver_fails(self):
|
|
|
"""A failing job_end receiver after a successful run leaves the job completed and propagates."""
|
|
|
user = User.objects.create_user(username='job-owner')
|
|
|
@@ -381,6 +442,23 @@ class JobRunnerTestCase(BaseJobRunnerTestCase):
|
|
|
successor = TestJobRunner.get_jobs(instance).exclude(pk=job.pk).get()
|
|
|
self.assertEqual((successor.status, successor.interval), (JobStatusChoices.STATUS_SCHEDULED, 60))
|
|
|
|
|
|
+ def test_handle_keeps_reschedule_error_after_interrupt(self):
|
|
|
+ """An interrupted Job keeps the error of a rejected reschedule without recording an outcome."""
|
|
|
+ job = Job.objects.create(name=TestJobRunner.name, job_id=uuid.uuid4(), interval=60)
|
|
|
+
|
|
|
+ with (
|
|
|
+ patch.object(TestJobRunner, 'run', side_effect=SystemExit),
|
|
|
+ patch.object(TestJobRunner, 'enqueue', side_effect=ValidationError('Simulated validation error')),
|
|
|
+ disable_logging(),
|
|
|
+ self.assertRaises(SystemExit),
|
|
|
+ ):
|
|
|
+ TestJobRunner.handle(job)
|
|
|
+
|
|
|
+ job.refresh_from_db()
|
|
|
+ self.assertEqual(job.status, JobStatusChoices.STATUS_RUNNING)
|
|
|
+ self.assertIsNone(job.completed)
|
|
|
+ self.assertIn('Simulated validation error', job.log_entries[-1]['message'])
|
|
|
+
|
|
|
def test_handle_terminal_save_error(self):
|
|
|
"""A failed terminal save propagates and sends no notification or job_end."""
|
|
|
user = User.objects.create_user(username='job-owner')
|
|
|
@@ -811,6 +889,24 @@ class SystemJobTestCase(BaseJobRunnerTestCase):
|
|
|
waiting = TestSystemJobRunner.get_jobs().filter(status__in=JobStatusChoices.ENQUEUED_STATE_CHOICES)
|
|
|
self.assertEqual(list(waiting.values_list('status', 'interval')), [(JobStatusChoices.STATUS_SCHEDULED, 60)])
|
|
|
|
|
|
+ def test_handle_keeps_schedule_of_deleted_system_job(self):
|
|
|
+ """A system job deleted while running is not re-created and leaves one successor."""
|
|
|
+ job = TestSystemJobRunner.enqueue(interval=60)
|
|
|
+
|
|
|
+ def delete_job():
|
|
|
+ Job.objects.get(pk=job.pk).delete()
|
|
|
+
|
|
|
+ with (
|
|
|
+ patch.object(TestSystemJobRunner, 'run', side_effect=delete_job),
|
|
|
+ patch.object(job, '_dispatch_termination') as dispatch,
|
|
|
+ self.assertLogs('netbox.jobs', 'WARNING'),
|
|
|
+ ):
|
|
|
+ TestSystemJobRunner.handle(job)
|
|
|
+
|
|
|
+ dispatch.assert_not_called()
|
|
|
+ jobs = TestSystemJobRunner.get_jobs().values_list('status', 'interval')
|
|
|
+ self.assertEqual(list(jobs), [(JobStatusChoices.STATUS_SCHEDULED, 60)])
|
|
|
+
|
|
|
def test_handle_sends_job_end_outside_schedule_lock(self):
|
|
|
"""A system job is saved as ended under the schedule lock, then notifies and sends job_end outside it."""
|
|
|
user = User.objects.create_user(username='job-owner')
|