1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 08:56:07 +00:00

fix(vmbackups): a schedule run's failure reason in the reader's language

A failed run kept the error as English text and the schedule page passed
the whole sentence to gettext, which found no catalog entry once values
were filled in. The run now keeps the error as messages (text and values),
as jobs do, and the page shows them with the new words filter.
This commit is contained in:
catborise 2026-11-18 14:05:38 +03:00
parent fb6a427a47
commit e1611efd53
7 changed files with 40 additions and 15 deletions

View file

@ -11,6 +11,7 @@ from django.utils.translation import gettext, ngettext, pgettext, pgettext_lazy
from operations.jobs import take_remembered
from operations.models import Operation
from operations.utils import words as _words
register = template.Library()
@ -54,6 +55,12 @@ def state_label(state):
return STATE_LABELS[state]
@register.filter
def words(messages):
"""A job's stored messages (operations.utils.say) in the reader's language."""
return _words(messages)
@register.filter
def duration(start, end):
"""The time from start to end, in seconds under a minute: timesince counts minutes.

View file

@ -0,0 +1,13 @@
from django.db import migrations, models
class Migration(migrations.Migration):
"""A run's note becomes messages, shown in the reader's language; the
English notes of earlier runs are dropped."""
dependencies = [("vmbackups", "0005_owners")]
operations = [
migrations.RemoveField(model_name="backuprun", name="note"),
migrations.AddField(model_name="backuprun", name="note", field=models.JSONField(default=list)),
]

View file

@ -78,7 +78,7 @@ class BackupRun(models.Model):
day = models.DateField() # the local day of the schedule's time
state = models.CharField(max_length=16, choices=STATES)
operation = models.ForeignKey(Operation, on_delete=models.SET_NULL, null=True)
note = models.CharField(max_length=255, blank=True)
note = models.JSONField(default=list) # messages (operations.utils.say), shown in the reader's language
created_at = models.DateTimeField(auto_now_add=True)
class Meta:

View file

@ -17,7 +17,7 @@ from libvirt import libvirtError
from computes.models import Compute
from operations import claims, jobs
from operations.models import Operation
from operations.utils import safe_text
from operations.utils import safe_text, said, say
from vrtManager.util import ConnectionFailed
from .jobs import beyond_keep
@ -50,7 +50,7 @@ def run_schedules(now=None):
continue
in_maintenance = Compute.objects.filter(pk=vm.compute_id, maintenance_since__isnull=False).exists()
if not open_:
_record(policy, vm, day, BackupRun.SKIPPED, IN_MAINTENANCE if in_maintenance else NOT_STARTED)
_record(policy, vm, day, BackupRun.SKIPPED, say(IN_MAINTENANCE if in_maintenance else NOT_STARTED))
elif not in_maintenance: # deferred while the window lasts
due.setdefault(vm.compute_id, []).append((policy.pk, vm.pk, day, start))
for compute_id, items in due.items():
@ -75,9 +75,9 @@ def occurrences(policy, now):
yield day, start
def _record(policy, vm, day, state, note="", operation=None):
def _record(policy, vm, day, state, note=(), operation=None):
BackupRun.objects.get_or_create(policy=policy, instance=vm, day=day,
defaults={"state": state, "note": note[:255], "operation": operation})
defaults={"state": state, "note": list(note), "operation": operation})
def _active_superuser(user):
@ -106,7 +106,7 @@ def _submit(policy_id, vm_id, day, start):
if Compute.objects.filter(pk=vm.compute_id, maintenance_since__isnull=False).exists():
return
if not _active_superuser(policy.owner):
_record(policy, vm, day, BackupRun.FAILED, NO_OWNER)
_record(policy, vm, day, BackupRun.FAILED, say(NO_OWNER))
return
# the same key for the same run: a worker stopped before the row was written gets the same job back
key = f"bk-{policy.pk}-{vm.pk}-{day:%Y%m%d}"
@ -117,7 +117,7 @@ def _submit(policy_id, vm_id, day, start):
except (claims.Busy, ConnectionFailed):
return
except (libvirtError, PermissionDenied, jobs.SubmissionConflict) as err: # it would be refused again
_record(policy, vm, day, BackupRun.FAILED, safe_text(str(err) or type(err).__name__))
_record(policy, vm, day, BackupRun.FAILED, said(err) if str(err) else say(type(err).__name__))
return
except Exception as err:
logger.error("schedule %s: backup of %s not submitted: %s", policy.name, vm.name, safe_text(err))

View file

@ -2,6 +2,7 @@
{% load i18n %}
{% load static %}
{% load bootstrap_icons %}
{% load operations %}
{% block title %}{{ policy.name }} - {% trans "Backup schedules" %}{% endblock %}
{% block style %}<link href="{% static 'css/tom-select.bootstrap5.min.css' %}" rel="stylesheet">{% endblock %}
@ -36,7 +37,7 @@
{% if run.state == "submitted" %}{% if run.operation_id %}<a href="{% url 'operations:job' run.operation_id %}">{% trans "Backup job" %}</a>{% else %}{% trans "Started" %}{% endif %}
{% elif run.state == "skipped" %}<span class="badge text-bg-secondary">{% trans "Skipped" %}</span>
{% else %}<span class="badge text-bg-danger">{% trans "Failed" %}</span>{% endif %}
{% if run.note %}<div class="small text-body-secondary">{% trans run.note %}</div>{% endif %}
{% if run.note %}<div class="small text-body-secondary">{{ run.note|words }}</div>{% endif %}
</td>
</tr>
{% endfor %}

View file

@ -12,6 +12,7 @@ from computes.models import Compute
from instances.models import Instance
from instances.tests.test_page_controls import PageControls
from logs.models import Logs
from operations.utils import say
from vmbackups.models import Backup, BackupPolicy, BackupRun, BackupTarget, BackupTargetPool
@ -85,7 +86,7 @@ class PolicyPageTests(TestCase):
def test_edit_and_the_runs(self):
policy = self.schedule()
BackupRun.objects.create(policy=policy, instance=self.vm1, day=datetime.date.today(), state=BackupRun.SKIPPED,
note="The host was in maintenance")
note=say("The host was in maintenance"))
url = reverse("vmbackups:policy", args=[policy.pk])
page = self.client.get(url)
self.assertContains(page, "The host was in maintenance")

View file

@ -9,7 +9,7 @@ from unittest.mock import MagicMock, patch
import libvirt
from django.contrib.auth import get_user_model
from django.test import TransactionTestCase, override_settings
from django.utils import timezone
from django.utils import timezone, translation
from appsettings.settings import get_settings
from computes.models import Compute
@ -17,6 +17,7 @@ from instances.models import Instance
from operations import jobs
from operations.jobs import Context
from operations.models import Operation, acquire
from operations.utils import words
from vmbackups import schedule
from vmbackups.jobs import CheckTarget, DeleteBackup, _beyond_keep
from vmbackups.models import Backup, BackupFile, BackupPolicy, BackupRun, BackupTarget, BackupTargetPool
@ -192,7 +193,7 @@ class ScheduleTests(TransactionTestCase):
self.pass_at(at(2026, 11, 17, 8, 1))
run = BackupRun.objects.get()
self.assertEqual(run.state, BackupRun.SKIPPED)
self.assertIn("maintenance", run.note)
self.assertIn("maintenance", words(run.note))
self.assertEqual(self.creates(), [])
def test_maintenance_ended_inside_the_window(self):
@ -212,7 +213,7 @@ class ScheduleTests(TransactionTestCase):
self.pass_at(at(2026, 11, 17, 8, 1)) # the worker was down from before 02:00
run = BackupRun.objects.get()
self.assertEqual(run.state, BackupRun.SKIPPED)
self.assertIn("6 hours", run.note)
self.assertIn("6 hours", words(run.note))
self.assertEqual(self.creates(), [])
def test_a_new_schedule_does_not_skip_the_times_before_it(self):
@ -270,14 +271,16 @@ class ScheduleTests(TransactionTestCase):
self.pass_at(at(2026, 11, 17, 2, 0))
run = BackupRun.objects.get()
self.assertEqual(run.state, BackupRun.FAILED)
self.assertIn("owner", run.note)
self.assertIn("owner", words(run.note))
def test_a_vm_that_cannot_be_backed_up_fails_once_with_the_reason(self):
self.proxy.backup_plan.side_effect = OperationError("%(vm)s has TPM state; TPM backups come later", vm="vm1")
self.proxy.backup_plan.side_effect = OperationError("%(vm)s has TPM state; shut it down to back it up", vm="vm1")
self.pass_at(at(2026, 11, 17, 2, 0))
self.pass_at(at(2026, 11, 17, 2, 1))
run = BackupRun.objects.get()
self.assertEqual((run.state, run.note), (BackupRun.FAILED, "vm1 has TPM state; TPM backups come later"))
self.assertEqual((run.state, words(run.note)), (BackupRun.FAILED, "vm1 has TPM state; shut it down to back it up"))
with translation.override("tr"): # in the reader's language, its values in place
self.assertEqual(words(run.note), "vm1 TPM durumu taşıyor; yedeklemek için kapatın")
def test_an_unreachable_host_is_tried_again(self):
from vrtManager.util import ConnectionFailed