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

feat(operations): the Logs page's rows and expired sessions are removed

Nothing removed the Logs page's rows, which every action and every
failed login adds, or the database sessions past their expiry. The job
worker's housekeeping now deletes the rows older than Log Retention
Days, a new setting under Lists and pages (1-3650, 365 by default), and
the expired sessions.
This commit is contained in:
catborise 2026-11-05 09:40:44 +03:00
parent 93b420ae81
commit b2deb042e8
10 changed files with 243 additions and 173 deletions

View file

@ -302,6 +302,20 @@ class RequestJobTestCase(TransactionTestCase):
self.assertEqual(fakes.EXPIRED, [str(idle.pk)])
self.assertNotIn(str(fresh.pk), fakes.EXPIRED)
def test_housekeeping_removes_old_log_rows_and_expired_sessions(self):
from django.contrib.sessions.models import Session
from logs.models import Logs
AppSettings.objects.update_or_create(key="LOG_RETENTION_DAYS", defaults={"name": "t", "value": "10", "choices": ""})
old, kept = (Logs.objects.create(user="u", host="h", instance="vm", message=m) for m in ("old", "kept"))
Logs.objects.filter(pk=old.pk).update(date=timezone.now() - timedelta(days=11))
Logs.objects.filter(pk=kept.pk).update(date=timezone.now() - timedelta(days=9))
Session.objects.create(session_key="expired-session", session_data="", expire_date=timezone.now() - timedelta(1))
Session.objects.create(session_key="current-session", session_data="", expire_date=timezone.now() + timedelta(1))
housekeeping()
self.assertEqual(list(Logs.objects.values_list("message", flat=True)), ["kept"])
self.assertEqual(list(Session.objects.values_list("session_key", flat=True)), ["current-session"])
def test_one_failing_expire_does_not_stop_the_others(self):
self.request_job("key-00000001", 61, uuid="u1", fail_expire=True)
other = self.request_job("key-00000002", 61, uuid="u2")

View file

@ -51,7 +51,7 @@ TRANSPORT_ERROR_CODES = {
}
UNREACHABLE = gettext_noop("The host of this virtual machine cannot be reached right now.")
BOUNDS = {"WORKER_CONCURRENCY": (1, 16, 4), "JOB_RETENTION_DAYS": (1, 3650, 30)}
BOUNDS = {"WORKER_CONCURRENCY": (1, 16, 4), "JOB_RETENTION_DAYS": (1, 3650, 30), "LOG_RETENTION_DAYS": (1, 3650, 365)}
def safe_text(text):
@ -177,3 +177,7 @@ def concurrency():
def retention_days():
return _value("JOB_RETENTION_DAYS")
def log_retention_days():
return _value("LOG_RETENTION_DAYS")

View file

@ -15,6 +15,7 @@ import uuid
from collections import Counter
from concurrent.futures import ThreadPoolExecutor
from datetime import timedelta
from importlib import import_module
from pathlib import Path
from django.conf import settings
@ -23,6 +24,7 @@ from django.utils import timezone
from django.utils.translation import gettext_noop
from appsettings.settings import get_settings
from logs.models import Logs
from . import claims, jobs, utils
from .models import Claim, LeaseLost, Operation, acquire, fenced, renew
@ -367,6 +369,9 @@ def housekeeping():
)
Operation.objects.filter(pk__in=old).delete()
Claim.objects.filter(operation__isnull=True, expires_at__lt=timezone.now()).delete()
# nothing else removes the Logs page's rows or the expired sessions
Logs.objects.filter(date__lt=timezone.now() - timedelta(days=utils.log_retention_days())).delete()
import_module(settings.SESSION_ENGINE).SessionStore.clear_expired()
# each kind ends its idle jobs; HOUSEKEEPING_EVERY bounds how late an idle request job ends
for op in Operation.objects.filter(state__in=ACTIVE, worker=Operation.REQUEST):
try: