2026-10-05 09:36:20 +02:00
|
|
|
# retoor <retoor@molodetz.nl>
|
|
|
|
|
from datetime import datetime, timedelta, timezone
|
|
|
|
|
|
|
|
|
|
from molodetz.database import count_audit, get_int_setting, prune_audit, prune_expired_sessions
|
|
|
|
|
from molodetz.services.base import BaseService
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class HousekeepingService(BaseService):
|
|
|
|
|
name = "housekeeping"
|
|
|
|
|
title = "Housekeeping"
|
2026-10-05 11:25:41 +02:00
|
|
|
description = "Clears expired sessions and prunes the audit log to the retention period."
|
2026-10-05 09:36:20 +02:00
|
|
|
default_interval = 3600
|
|
|
|
|
min_interval = 60
|
|
|
|
|
metrics_interval = 300
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
super().__init__()
|
|
|
|
|
self.last_sessions = 0
|
|
|
|
|
self.last_audit = 0
|
|
|
|
|
|
|
|
|
|
async def run_once(self):
|
|
|
|
|
self.last_sessions = prune_expired_sessions()
|
|
|
|
|
days = max(1, get_int_setting("audit_retention_days", 365))
|
|
|
|
|
cutoff = (datetime.now(timezone.utc) - timedelta(days=days)).isoformat()
|
|
|
|
|
self.last_audit = prune_audit(cutoff)
|
2026-10-05 11:25:41 +02:00
|
|
|
self.log(f"sessions removed: {self.last_sessions}, audit rows pruned: {self.last_audit}")
|
2026-10-05 09:36:20 +02:00
|
|
|
|
|
|
|
|
def collect_metrics(self):
|
|
|
|
|
return {
|
|
|
|
|
"cards": [
|
2026-10-05 11:25:41 +02:00
|
|
|
{"label": "Audit rows", "value": count_audit()},
|
|
|
|
|
{"label": "Last session cleanup", "value": self.last_sessions},
|
|
|
|
|
{"label": "Last audit prune", "value": self.last_audit},
|
2026-10-05 09:36:20 +02:00
|
|
|
],
|
|
|
|
|
"table": None,
|
|
|
|
|
}
|