07a841d91e
Bound docker_logs growth in the periodic cleanup task (same architecture as the metrics/events retention). run_docker_retention gains logs_retention_days + logs_max_bytes_per_container: it prunes lines past the age window, then keeps only the newest ~cap bytes per (host, container) via a window-function ring (exclusive-prefix sum, so the newest line always survives even if it alone exceeds the cap). Containers rotate independently. - settings DEFAULTS: docker.logs.enabled/exclude/retention_days(3)/ max_bytes_per_container(5MB) — operator preference: ~3 days / ~5 MB - cleanup.py reads the two windows fresh each run (rule 25, no restart) - integration rotate test: age prune + per-container byte cap + isolation Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CAGR73DUowdVFVvYzLXC5C
90 lines
4.0 KiB
Python
90 lines
4.0 KiB
Python
from __future__ import annotations
|
|
import logging
|
|
from datetime import datetime, timedelta, timezone
|
|
from typing import TYPE_CHECKING
|
|
|
|
from sqlalchemy import delete
|
|
|
|
from steward.models.monitors import MonitorResult
|
|
from steward.models.ansible import AnsibleRun
|
|
|
|
if TYPE_CHECKING:
|
|
from quart import Quart
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
async def run_cleanup(app: "Quart") -> None:
|
|
"""Delete rows older than DATA_RETENTION_DAYS from time-series tables, then
|
|
run Docker-specific rollup + retention (delegated to the docker plugin)."""
|
|
retention_days: int = app.config.get("DATA_RETENTION_DAYS", 90)
|
|
now = datetime.now(timezone.utc)
|
|
cutoff = now - timedelta(days=retention_days)
|
|
|
|
async with app.db_sessionmaker() as session:
|
|
async with session.begin():
|
|
for model, ts_col in [
|
|
(MonitorResult, MonitorResult.checked_at),
|
|
(AnsibleRun, AnsibleRun.started_at),
|
|
]:
|
|
result = await session.execute(
|
|
delete(model).where(ts_col < cutoff)
|
|
)
|
|
if result.rowcount:
|
|
logger.info(f"Pruned {result.rowcount} rows from {model.__tablename__}")
|
|
|
|
# plugin_metrics is NOT blanket-deleted here — it's rolled up to hourly
|
|
# then pruned, so multi-week host history stays cheap.
|
|
await _run_metrics_retention(session, now)
|
|
await _run_docker_retention(session, now)
|
|
|
|
|
|
async def _run_metrics_retention(session, now: datetime) -> None:
|
|
"""Roll up + prune plugin_metrics (raw → hourly → gone). Windows read fresh
|
|
from settings each run (rule 25 — UI change takes effect next cleanup, no
|
|
restart). get_setting's SELECT autobegins, so read inside the begin block."""
|
|
from steward.core.metrics_retention import rollup_plugin_metrics
|
|
from steward.core.settings import get_setting
|
|
|
|
async with session.begin():
|
|
raw_days = int(await get_setting(session, "metrics.retention.raw_days") or 7)
|
|
rollup_days = int(await get_setting(session, "metrics.retention.rollup_days") or 90)
|
|
counts = await rollup_plugin_metrics(
|
|
session, raw_days=raw_days, rollup_days=rollup_days, now=now,
|
|
)
|
|
if counts and any(counts.values()):
|
|
logger.info("Metrics retention: %s", counts)
|
|
|
|
|
|
async def _run_docker_retention(session, now: datetime) -> None:
|
|
"""Drive the docker plugin's rollup + prune via its capability, if loaded.
|
|
|
|
Windows are read fresh from settings each run (rule 25 — a change in the
|
|
Settings UI takes effect on the next hourly cleanup, no restart). Kept in its
|
|
own transaction so a docker-side failure can't roll back the generic prune
|
|
above. No-op when the docker plugin is disabled (capability absent).
|
|
"""
|
|
from steward.core.capabilities import has_capability, invoke_capability
|
|
if not has_capability("docker.run_retention"):
|
|
return
|
|
from steward.core.settings import get_setting
|
|
from steward.models.users import UserRole
|
|
|
|
# Reads + rollup/prune share one transaction — get_setting's SELECT would
|
|
# otherwise autobegin one, making a later session.begin() raise.
|
|
async with session.begin():
|
|
raw_days = int(await get_setting(session, "docker.retention.metrics_raw_days") or 7)
|
|
rollup_days = int(await get_setting(session, "docker.retention.metrics_rollup_days") or 90)
|
|
events_days = int(await get_setting(session, "docker.retention.events_days") or 30)
|
|
logs_days = int(await get_setting(session, "docker.logs.retention_days") or 3)
|
|
logs_cap = int(
|
|
await get_setting(session, "docker.logs.max_bytes_per_container") or 5_000_000)
|
|
counts = await invoke_capability(
|
|
"docker.run_retention", UserRole.viewer, session,
|
|
events_days=events_days, metrics_raw_days=raw_days,
|
|
metrics_rollup_days=rollup_days, logs_retention_days=logs_days,
|
|
logs_max_bytes_per_container=logs_cap, now=now,
|
|
)
|
|
if counts and any(counts.values()):
|
|
logger.info("Docker retention: %s", counts)
|