(feature) Add cancellable backup runs and clearer dry-run logs
Add a cancel action for queued and running backup runs. Queued runs are cancelled immediately, while running runs are marked for cancellation and the worker terminates the active rsync process group. Make dry-run log paths run-specific and add a defensive default dry-run timeout so stuck dry-runs do not remain running indefinitely. Remove rsync exit codes from run overview tables while keeping detailed diagnostics available on the run detail payload.
This commit is contained in:
@@ -48,22 +48,87 @@ class BackupWorkerTests(TestCase):
|
||||
run = queue_backup_run(host=host)
|
||||
|
||||
with patch("pobsync_backend.backup_runner.run_scheduled") as run_scheduled:
|
||||
run_scheduled.return_value = {
|
||||
"ok": True,
|
||||
"dry_run": False,
|
||||
"host": host.host,
|
||||
"snapshot": str(snapshot_dir),
|
||||
"base": None,
|
||||
"rsync": {"exit_code": 0},
|
||||
}
|
||||
def fake_run_scheduled(**kwargs):
|
||||
run.refresh_from_db()
|
||||
self.assertIn("execution", run.result)
|
||||
return {
|
||||
"ok": True,
|
||||
"dry_run": False,
|
||||
"host": host.host,
|
||||
"snapshot": str(snapshot_dir),
|
||||
"base": None,
|
||||
"rsync": {"exit_code": 0},
|
||||
}
|
||||
|
||||
run_scheduled.side_effect = fake_run_scheduled
|
||||
count = Command()._run_once(prefix=Path(tmp) / "home")
|
||||
run_scheduled.assert_called_once()
|
||||
|
||||
self.assertEqual(count, 1)
|
||||
self.assertEqual(run_scheduled.call_args.kwargs["run_id"], run.id)
|
||||
run.refresh_from_db()
|
||||
self.assertEqual(run.status, BackupRun.Status.SUCCESS)
|
||||
self.assertEqual(SnapshotRecord.objects.count(), 1)
|
||||
self.assertEqual(run.snapshot, SnapshotRecord.objects.get())
|
||||
|
||||
def test_worker_records_dry_run_log_path_while_running(self) -> None:
|
||||
with TemporaryDirectory() as tmp:
|
||||
GlobalConfig.objects.create(name="default", backup_root=str(Path(tmp) / "backups"))
|
||||
host = HostConfig.objects.create(host="web-01", address="web-01.example.test")
|
||||
run = queue_backup_run(host=host, dry_run=True)
|
||||
|
||||
with patch("pobsync_backend.backup_runner.run_scheduled") as run_scheduled:
|
||||
def fake_run_scheduled(**kwargs):
|
||||
run.refresh_from_db()
|
||||
self.assertEqual(
|
||||
run.result["execution"]["log"],
|
||||
f"/tmp/pobsync-dryrun/{host.host}/run-{run.id}/rsync.log",
|
||||
)
|
||||
return {
|
||||
"ok": True,
|
||||
"dry_run": True,
|
||||
"host": host.host,
|
||||
"base": None,
|
||||
"log": run.result["execution"]["log"],
|
||||
"rsync": {"exit_code": 0},
|
||||
}
|
||||
|
||||
run_scheduled.side_effect = fake_run_scheduled
|
||||
count = Command()._run_once(prefix=Path(tmp) / "home")
|
||||
|
||||
self.assertEqual(count, 1)
|
||||
run.refresh_from_db()
|
||||
self.assertEqual(run.status, BackupRun.Status.SUCCESS)
|
||||
self.assertEqual(run.result["log"], f"/tmp/pobsync-dryrun/{host.host}/run-{run.id}/rsync.log")
|
||||
|
||||
def test_worker_preserves_cancelled_status_from_running_run(self) -> None:
|
||||
with TemporaryDirectory() as tmp:
|
||||
GlobalConfig.objects.create(name="default", backup_root=str(Path(tmp) / "backups"))
|
||||
host = HostConfig.objects.create(host="web-01", address="web-01.example.test")
|
||||
run = queue_backup_run(host=host, dry_run=True)
|
||||
|
||||
with patch("pobsync_backend.backup_runner.run_scheduled") as run_scheduled:
|
||||
def fake_run_scheduled(**kwargs):
|
||||
BackupRun.objects.filter(id=run.id).update(status=BackupRun.Status.CANCELLED)
|
||||
self.assertTrue(kwargs["cancel_check"]())
|
||||
return {
|
||||
"ok": False,
|
||||
"dry_run": True,
|
||||
"cancelled": True,
|
||||
"host": host.host,
|
||||
"base": None,
|
||||
"log": f"/tmp/pobsync-dryrun/{host.host}/run-{run.id}/rsync.log",
|
||||
"rsync": {"exit_code": 130},
|
||||
}
|
||||
|
||||
run_scheduled.side_effect = fake_run_scheduled
|
||||
count = Command()._run_once(prefix=Path(tmp) / "home")
|
||||
|
||||
self.assertEqual(count, 1)
|
||||
run.refresh_from_db()
|
||||
self.assertEqual(run.status, BackupRun.Status.CANCELLED)
|
||||
self.assertEqual(run.rsync_exit_code, 130)
|
||||
|
||||
def test_worker_returns_zero_without_queued_runs(self) -> None:
|
||||
count = Command()._run_once(prefix=Path("/opt/pobsync"))
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ class RunScheduledConfigSourceTests(SimpleTestCase):
|
||||
run_rsync.assert_called_once()
|
||||
|
||||
def test_failed_dry_run_includes_log_tail(self) -> None:
|
||||
def fake_run_rsync(command, log_path, timeout_seconds):
|
||||
def fake_run_rsync(command, log_path, timeout_seconds, cancel_check=None):
|
||||
log_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
log_path.write_text("Permission denied (publickey).\nrsync error\n", encoding="utf-8")
|
||||
return RsyncResult(exit_code=12, command=command)
|
||||
@@ -68,13 +68,13 @@ class RunScheduledConfigSourceTests(SimpleTestCase):
|
||||
self.assertEqual(result["rsync"]["log_tail"], ["Permission denied (publickey).", "rsync error"])
|
||||
|
||||
def test_dry_run_clears_previous_log_before_running(self) -> None:
|
||||
def fake_run_rsync(command, log_path, timeout_seconds):
|
||||
def fake_run_rsync(command, log_path, timeout_seconds, cancel_check=None):
|
||||
self.assertFalse(log_path.exists())
|
||||
log_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
log_path.write_text("current run only\n", encoding="utf-8")
|
||||
return RsyncResult(exit_code=0, command=command)
|
||||
|
||||
old_log = Path("/tmp/pobsync-dryrun/web-01/rsync.log")
|
||||
old_log = Path("/tmp/pobsync-dryrun/web-01/adhoc/rsync.log")
|
||||
old_log.parent.mkdir(parents=True, exist_ok=True)
|
||||
old_log.write_text("old failure\n", encoding="utf-8")
|
||||
|
||||
@@ -89,6 +89,47 @@ class RunScheduledConfigSourceTests(SimpleTestCase):
|
||||
self.assertTrue(result["ok"])
|
||||
self.assertEqual(result["rsync"]["log_tail"], ["current run only"])
|
||||
|
||||
def test_dry_run_uses_run_specific_log_path_and_default_timeout(self) -> None:
|
||||
def fake_run_rsync(command, log_path, timeout_seconds, cancel_check=None):
|
||||
self.assertEqual(log_path, Path("/tmp/pobsync-dryrun/web-01/run-42/rsync.log"))
|
||||
self.assertEqual(timeout_seconds, 900)
|
||||
log_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
log_path.write_text("run 42\n", encoding="utf-8")
|
||||
return RsyncResult(exit_code=0, command=command)
|
||||
|
||||
with patch("pobsync.commands.run_scheduled.run_rsync", side_effect=fake_run_rsync):
|
||||
result = run_scheduled(
|
||||
prefix=Path("/missing-prefix"),
|
||||
host="web-01",
|
||||
dry_run=True,
|
||||
config_source=FakeConfigSource(),
|
||||
run_id=42,
|
||||
)
|
||||
|
||||
self.assertTrue(result["ok"])
|
||||
self.assertEqual(result["log"], "/tmp/pobsync-dryrun/web-01/run-42/rsync.log")
|
||||
self.assertEqual(result["timeout_seconds"], 900)
|
||||
|
||||
def test_dry_run_reports_cancelled_rsync(self) -> None:
|
||||
def fake_run_rsync(command, log_path, timeout_seconds, cancel_check=None):
|
||||
self.assertTrue(cancel_check())
|
||||
log_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
log_path.write_text("[pobsync] rsync cancelled\n", encoding="utf-8")
|
||||
return RsyncResult(exit_code=130, command=command, cancelled=True)
|
||||
|
||||
with patch("pobsync.commands.run_scheduled.run_rsync", side_effect=fake_run_rsync):
|
||||
result = run_scheduled(
|
||||
prefix=Path("/missing-prefix"),
|
||||
host="web-01",
|
||||
dry_run=True,
|
||||
config_source=FakeConfigSource(),
|
||||
cancel_check=lambda: True,
|
||||
)
|
||||
|
||||
self.assertFalse(result["ok"])
|
||||
self.assertTrue(result["cancelled"])
|
||||
self.assertEqual(result["rsync"]["exit_code"], 130)
|
||||
|
||||
def test_successful_real_run_applies_prune_when_requested(self) -> None:
|
||||
with TemporaryDirectory() as tmp:
|
||||
prefix = Path(tmp) / "home"
|
||||
|
||||
@@ -764,6 +764,44 @@ class ViewTests(TestCase):
|
||||
self.assertContains(response, ""ok": true")
|
||||
self.assertContains(response, reverse("snapshot_detail", args=[snapshot.id]))
|
||||
|
||||
def test_run_detail_offers_cancel_for_running_run(self) -> None:
|
||||
self.client.force_login(self.staff_user)
|
||||
host = HostConfig.objects.create(host="web-01", address="web-01.example.test")
|
||||
run = BackupRun.objects.create(host=host, status=BackupRun.Status.RUNNING)
|
||||
|
||||
response = self.client.get(reverse("run_detail", args=[run.id]))
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertContains(response, "Cancel run")
|
||||
self.assertContains(response, reverse("cancel_run", args=[run.id]))
|
||||
|
||||
def test_cancel_run_marks_queued_run_cancelled(self) -> None:
|
||||
self.client.force_login(self.staff_user)
|
||||
host = HostConfig.objects.create(host="web-01", address="web-01.example.test")
|
||||
run = BackupRun.objects.create(host=host, status=BackupRun.Status.QUEUED)
|
||||
|
||||
response = self.client.post(reverse("cancel_run", args=[run.id]), follow=True)
|
||||
|
||||
self.assertRedirects(response, reverse("run_detail", args=[run.id]))
|
||||
self.assertContains(response, "Cancellation requested")
|
||||
run.refresh_from_db()
|
||||
self.assertEqual(run.status, BackupRun.Status.CANCELLED)
|
||||
self.assertIsNotNone(run.ended_at)
|
||||
self.assertEqual(run.result["cancellation"]["previous_status"], BackupRun.Status.QUEUED)
|
||||
|
||||
def test_cancel_run_requests_running_run_cancellation(self) -> None:
|
||||
self.client.force_login(self.staff_user)
|
||||
host = HostConfig.objects.create(host="web-01", address="web-01.example.test")
|
||||
run = BackupRun.objects.create(host=host, status=BackupRun.Status.RUNNING)
|
||||
|
||||
response = self.client.post(reverse("cancel_run", args=[run.id]), follow=True)
|
||||
|
||||
self.assertRedirects(response, reverse("run_detail", args=[run.id]))
|
||||
run.refresh_from_db()
|
||||
self.assertEqual(run.status, BackupRun.Status.CANCELLED)
|
||||
self.assertIsNone(run.ended_at)
|
||||
self.assertEqual(run.result["cancellation"]["previous_status"], BackupRun.Status.RUNNING)
|
||||
|
||||
def test_snapshot_detail_renders_metadata_runs_and_children(self) -> None:
|
||||
self.client.force_login(self.staff_user)
|
||||
host = HostConfig.objects.create(host="web-01", address="web-01.example.test")
|
||||
|
||||
Reference in New Issue
Block a user