Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions docs/administration/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ RQ's scheduler can still queue a task it fetched just before cancellation.
NetBox does not start its failed Job, and the reference pass considers the run
for replay. The pass holds back a replacement if it finds the original started,
or requeued and waiting again. It checks once, so a run requeued after that
check can execute on both sides. If the original Job row is missing at that
check, the pass also considers the run for replay.
check can execute on both sides. If the cancelled Job's row is missing at that
check, the pass also considers the run for replay. A run whose Job was deleted
before cutover could confirm its cancellation is not recreated, even if the Job
reappears later.

This depends on every worker running the NetBox release this plugin requires.
Restart all workers after upgrading NetBox, because a worker started before the
Expand Down Expand Up @@ -498,12 +500,12 @@ running the reference pass finishes that Job first, but replayed runs do not
wait for the maintenance window to end.

Runs recorded as already executing or executed are not automatically
recreated. Review their outcome. Before arranging a recurring schedule by hand,
check [Recurring successors](#recurring-successors). Re-entering cutover may
already have captured the next occurrence. A cancelled run found to have
started or been requeued is also held back. The same check applies when a
Job reappears after cutover found it missing. A Job deleted while its run
executes can reappear when the run ends. See
recreated, and neither is a run whose Job was deleted before cutover could
confirm its cancellation, since it may have run. Review their outcome. Before
arranging a recurring schedule by hand, check
[Recurring successors](#recurring-successors). Re-entering cutover may already
have captured the next occurrence. A cancelled run found to have started or been
requeued is also held back. See
[Cancellation and replay limits](#cancellation-and-replay-limits) for the
limits of that check.

Expand All @@ -522,9 +524,10 @@ revision keeps its own message.
**Permanently skipped** means migration will not retry the item, not that you
cannot resolve it manually. Examples include classes absent from the captured
map, module-level references, constrained permissions, overdue one-shot runs,
invalid input, selected objects the owner cannot view, input the cutover could
not record and deleted or deactivated recorded owners. Review the warnings
separately. These items do not all keep migration open.
invalid input, selected objects the owner cannot view, runs whose Job was
deleted before cutover could confirm their cancellation, input the cutover
could not record and deleted or deactivated recorded owners. Review the
warnings separately. These items do not all keep migration open.

Retained built-in rows may lose their pages when NetBox removes the feature.
Export or reconcile history you need before that upgrade. Retaining rows does
Expand Down Expand Up @@ -607,8 +610,9 @@ retry, and a requeued run can execute after its cancellation. Check existing
work before retrying. See [Cancellation and replay limits](#cancellation-and-replay-limits).

Replay uses recorded cancellation outcomes and each Job's state when the
reference pass runs. It is not exactly once: a run requeued after that check,
or one that ran and whose Job row is missing at that check, is not detected.
reference pass runs. It is not exactly once: a cancelled run requeued after that
check, or one that ran after a requeue and whose Job row is missing at that
check, is not detected.
Keep the maintenance window and review results even when cutover reports
completion.

Expand Down
2 changes: 1 addition & 1 deletion netbox_scripts/migration/cutover.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ def _cancel_schedules(run, captured):
continue
job = Job.objects.filter(pk=entry['job_pk']).first()
if job is None:
# Taken by a worker, so deleting its row must not make it replayable.
# An outcome an earlier pass recorded for a worker's run survives the row's deletion.
entry['cancellation'] = 'executed' if entry.get('cancellation') in ('executed', 'running') else 'vanished'
continue
if entry.get('cancellation') == 'cancelling' and job.status not in legacy_source.cancellable_statuses():
Expand Down
40 changes: 24 additions & 16 deletions netbox_scripts/migration/references.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ def recreate_schedules(run):
for entry in run.journal.get('schedules', []):
if str(entry['job_pk']) in recreated:
continue
# Matched by outcome rather than inverted: 'vanished' and a journal recording none at all
# can still replay. Skipped rather than outstanding, or the step could never end.
# Matched by outcome rather than inverted: a journal recording none at all can still replay.
# Skipped rather than outstanding, or the step could never end.
if entry.get('cancellation') in ('executed', 'running'):
counts['skipped'] += 1
run.record_warnings(
Expand All @@ -310,27 +310,35 @@ def recreate_schedules(run):
]
)
continue
# Vanished too: a worker's Job.terminate() saves its whole copy, restoring a Job deleted while it ran.
if entry.get('cancellation') in ('cancelled', 'vanished'):
# Deleted before the cutover could confirm its cancellation, so nothing reliably shows whether it ran.
if entry.get('cancellation') == 'vanished':
counts['skipped'] += 1
run.record_warnings(
[
_(
'Schedule "{name}" (Job {pk}) has not been recreated because its Job was deleted before the '
'cutover could confirm its cancellation. Check its execution outcome and any recurring '
'successor before running or scheduling it again.'
).format(pk=entry['job_pk'], name=entry['name'])
]
)
continue
if entry.get('cancellation') == 'cancelled':
# By key alone: the history pass may already have moved this row to the Script.
changed = Job.objects.filter(pk=entry['job_pk']).exclude(
status=JobStatusChoices.STATUS_FAILED, started__isnull=True
)
# NetBox never starts the failed, unstarted row the cutover left, so any other state may mean a run.
if changed.exists():
counts['skipped'] += 1
if entry['cancellation'] == 'cancelled':
message = _(
'Schedule "{name}" (Job {pk}) changed after cancellation and has not been recreated. '
'Check its execution history and any recurring successor before scheduling it again.'
)
else:
message = _(
'Schedule "{name}" (Job {pk}) reappeared after the cutover found it missing and has not '
'been recreated. Check its execution history and any recurring successor before '
'scheduling it again.'
)
run.record_warnings([message.format(pk=entry['job_pk'], name=entry['name'])])
run.record_warnings(
[
_(
'Schedule "{name}" (Job {pk}) changed after cancellation and has not been recreated. '
'Check its execution history and any recurring successor before scheduling it again.'
).format(pk=entry['job_pk'], name=entry['name'])
]
)
continue
# By type too: a module key can equal a Script's, so a bare lookup can hit the wrong row.
if entry.get('legacy_object_type') != 'extras.script':
Expand Down
2 changes: 1 addition & 1 deletion netbox_scripts/tests/migration/test_cutover.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ def test_a_deleted_row_reads_as_executed_only_for_a_run_a_worker_took(self):
unrecorded = self.legacy_job(task_kwargs={})
cutover._capture(self.migration)
entries = {entry['job_pk']: entry for entry in self.migration.journal['schedules']}
# Never seen running, so it stays on the replay side.
# Interrupted mid-cancellation, before any outcome was recorded.
entries[interrupted.pk]['cancellation'] = 'cancelling'
self.migration.save(update_fields=('journal',))
Job.objects.filter(pk__in=[interrupted.pk, unrecorded.pk]).delete()
Expand Down
64 changes: 53 additions & 11 deletions netbox_scripts/tests/migration/test_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -942,19 +942,65 @@ def test_an_occurrence_a_worker_took_is_not_recreated(self):
# Counting it outstanding instead would leave this step, and the whole migration, open.
self.assertTrue(self.migration.step_done(references.SCHEDULES_STEP))

def test_an_occurrence_whose_row_vanished_is_still_recreated(self):
# With no row to check, nothing shows that it ran.
def test_an_occurrence_whose_row_vanished_is_held_back(self):
job = self.legacy_schedule(scheduled=self.future())
self.cross_over()
Job.objects.filter(pk=job.pk).delete()
self.migration.journal['schedules'][0]['cancellation'] = 'vanished'
self.migration.save(update_fields=('journal',))

counts = references.recreate_schedules(self.migration)
counts, warnings = self.warnings_added(references.recreate_schedules, self.migration)

self.assertEqual(counts['recreated'], 1)
self.assertEqual(counts['recreated'], 0)
self.assertEqual(counts['skipped'], 1)
self.assertEqual(counts['outstanding'], 0)
self.assertFalse(self.new_jobs().exists())
self.assertEqual(
warnings,
[
(
f'Schedule "Deploy" (Job {job.pk}) has not been recreated because its Job was deleted before the '
'cutover could confirm its cancellation. Check its execution outcome and any recurring successor '
'before running or scheduling it again.'
)
],
)
self.migration.refresh_from_db()
self.assertTrue(self.migration.step_done(references.SCHEDULES_STEP))

def test_a_vanished_occurrence_already_recreated_is_left_alone(self):
# Earlier versions replayed a vanished run, so their journal can already map one.
job = self.legacy_schedule(scheduled=self.future())
self.cross_over()
Job.objects.filter(pk=job.pk).delete()
self.migration.journal['schedules'][0]['cancellation'] = 'vanished'
self.migration.journal['recreated_schedules'] = {str(job.pk): 999999}
self.migration.save(update_fields=('journal',))

def test_a_vanished_run_whose_job_returns_is_held_back(self):
counts, warnings = self.warnings_added(references.recreate_schedules, self.migration)

self.assertEqual(counts, {'recreated': 0, 'skipped': 0, 'shifted': 0, 'outstanding': 0})
self.assertEqual(warnings, [])
self.migration.refresh_from_db()
self.assertEqual(self.migration.journal['recreated_schedules'], {str(job.pk): 999999})

def test_a_vanished_occurrence_with_a_failed_unstarted_row_is_held_back(self):
job = self.legacy_schedule(scheduled=self.future())
self.cross_over()
self.migration.journal['schedules'][0]['cancellation'] = 'vanished'
self.migration.save(update_fields=('journal',))
# Failed and unstarted, the state a cancelled run still replays from.
self.assertTrue(
Job.objects.filter(pk=job.pk, status=JobStatusChoices.STATUS_FAILED, started__isnull=True).exists()
)

counts, warnings = self.warnings_added(references.recreate_schedules, self.migration)

self.assertEqual(counts, {'recreated': 0, 'skipped': 1, 'shifted': 0, 'outstanding': 0})
self.assertFalse(self.new_jobs().exists())
self.assertTrue(any(f'(Job {job.pk}) has not been recreated because' in warning for warning in warnings))

def test_a_run_whose_job_is_deleted_while_it_runs_is_held_back(self):
job, task = self.built_in_task(schedule_at=self.past(), interval=60)
self.stage_and_validate()
# Interrupted between the capture and the cancellations.
Expand All @@ -981,23 +1027,19 @@ def delete_and_resume(*args, **kwargs):
self.migration.refresh_from_db()
self.assertEqual(self.migration.journal['schedules'][0]['cancellation'], 'vanished')
self.assertTrue(self.migration.step_done(cutover.STEP))
job.refresh_from_db()
self.assertEqual(job.status, JobStatusChoices.STATUS_COMPLETED)
self.assertIsNotNone(job.started)
cutover.activate_staged(self.migration)

reference_job = MigrationReferencesJob.enqueue(immediate=True)

job.refresh_from_db()
self.assertEqual(job.object_type_id, ObjectType.objects.get_for_model(NetBoxScript).pk)
self.migration.refresh_from_db()
self.assertEqual(self.migration.journal.get('recreated_schedules', {}), {})
self.assertEqual(
self.migration.recorded_counts(references.SCHEDULES_STEP),
{'recreated': 0, 'skipped': 1, 'shifted': 0, 'outstanding': 0},
)
self.assertIn(
f'(Job {job.pk}) reappeared after the cutover found it missing', self.log_messages(reference_job, 'warning')
f'(Job {job.pk}) has not been recreated because its Job was deleted',
self.log_messages(reference_job, 'warning'),
)

def test_a_cancelled_run_that_started_on_the_built_in_side_is_not_recreated(self):
Expand Down
Loading