From 810abe7475716c7bdbd162bb53a02c8fa5a849f6 Mon Sep 17 00:00:00 2001 From: catborise Date: Wed, 18 Nov 2026 15:53:18 +0300 Subject: [PATCH] fix(vmbackups): eighth audit: retention reads each VM once, no reload while typing - The backups' default ordering put created_at into the SELECT DISTINCT of the retention pass and of the checkpoint pruning, so a VM came once per backup: the pass read every backup's chain again. The ordering is cleared before both. - A page with a backup being made or deleted reloads every 4 s; it now waits while a field is focused, so a filter or the backup form being filled in is not lost. --- vmbackups/jobs.py | 2 +- vmbackups/schedule.py | 2 +- vmbackups/static/vmbackups/pending.js | 4 ++-- vmbackups/tests/test_schedule.py | 6 ++++++ 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/vmbackups/jobs.py b/vmbackups/jobs.py index 3d18d3d5..545b6974 100644 --- a/vmbackups/jobs.py +++ b/vmbackups/jobs.py @@ -219,7 +219,7 @@ def _prune_checkpoints(op, proxy, plan): one, the base of its next incremental backup.""" backups = Backup.objects.filter(vm_uuid=op.intent["uuid"]).exclude(checkpoint="").exclude(uuid=op.intent["backup"]) keep = set() - for policy_id in backups.filter(policy__full_every__gt=1).values_list("policy", flat=True).distinct(): + for policy_id in backups.filter(policy__full_every__gt=1).order_by().values_list("policy", flat=True).distinct(): newest = backups.filter(policy=policy_id, state=Backup.READY).order_by("-created_at").first() if newest is not None: keep.add(newest.checkpoint) diff --git a/vmbackups/schedule.py b/vmbackups/schedule.py index 14c58d25..1b546425 100644 --- a/vmbackups/schedule.py +++ b/vmbackups/schedule.py @@ -150,7 +150,7 @@ def retain(): if policy.target_id in tried or not _active_superuser(policy.owner): continue vms = (Backup.objects.filter(policy=policy, state=Backup.READY, instance__isnull=False) - .values_list("instance_id", flat=True).distinct()) + .order_by().values_list("instance_id", flat=True).distinct()) # no created_at in the distinct for instance_id in vms: # a chain goes when all of it is beyond keep, newest first: only a backup nothing builds on is deleted beyond = [b for b in beyond_keep(policy, instance_id) if not b.depended_on()] diff --git a/vmbackups/static/vmbackups/pending.js b/vmbackups/static/vmbackups/pending.js index f524f095..f6c5c11d 100644 --- a/vmbackups/static/vmbackups/pending.js +++ b/vmbackups/static/vmbackups/pending.js @@ -1,12 +1,12 @@ // A backup being made or deleted: the page reads it again until it is done, // while the backups are in view (the VM page's Backups tab, the Backups page) -// and nothing is open on them (a menu, a dialog). +// and nothing is open on them (a menu, a dialog) or being typed into. (function () { if (!document.querySelector("[data-backup-pending]")) return; var check = function () { var pane = document.getElementById("backups"); var hidden = pane && pane.classList.contains("tab-pane") && !pane.classList.contains("active"); - var busy = document.querySelector(".dropdown-menu.show, .modal.show"); + var busy = document.querySelector(".dropdown-menu.show, .modal.show, input:focus, select:focus, textarea:focus"); if (hidden || busy || document.hidden) { setTimeout(check, 4000); } else { diff --git a/vmbackups/tests/test_schedule.py b/vmbackups/tests/test_schedule.py index 13619fe6..d07f43a2 100644 --- a/vmbackups/tests/test_schedule.py +++ b/vmbackups/tests/test_schedule.py @@ -367,6 +367,12 @@ class ScheduleTests(TransactionTestCase): schedule.retain() self.assertEqual(self.deletes()[-1].params["backup"], str(older.uuid)) + def test_retention_reads_each_vm_once(self): + self.backups(*[Backup.READY] * 4) + with patch("vmbackups.schedule.beyond_keep", return_value=[]) as beyond: + schedule.retain() + self.assertEqual(beyond.call_count, 1) + def chained(self, *backups): for parent, child in zip(backups, backups[1:]): Backup.objects.filter(pk=child.pk).update(parent=parent)