Files
shinkan-jinkendo/backend/scripts/media_retention_scheduler.py
LarsandCursor 95d365d9fa
Deploy Development / deploy (push) Successful in 1m1s
Test Suite / pytest-backend (push) Failing after 49s
Test Suite / lint-backend (push) Successful in 1s
Test Suite / build-frontend (push) Successful in 19s
Test Suite / k6 /health Baseline (push) Successful in 35s
Test Suite / playwright-tests (push) Failing after 7m57s
Schedule media retention at 03:00 Europe/Berlin.
The folded python -c command crashed the cron container before any run. Content-report rate limiting now applies only after the good-faith declaration.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-28 11:14:37 +01:00

122 lines
3.5 KiB
Python

#!/usr/bin/env python3
"""Täglicher Start von media_retention_job.py um 03:00 Europe/Berlin.
Der Prozess löscht beim Start nichts. Ein Lauf sofort nach dem Start nur mit
RETENTION_RUN_ON_START=1. Ein fehlgeschlagener Lauf bleibt im Log
(RETENTION_FAIL); der Prozess bleibt aktiv, damit Docker ihn nicht sofort
erneut startet.
"""
from __future__ import annotations
import logging
import os
import subprocess
import sys
import time
from datetime import datetime, timedelta
from zoneinfo import ZoneInfo
LOG = logging.getLogger("retention.scheduler")
DEFAULT_TZ = "Europe/Berlin"
_RUN_ON_START = "RETENTION_RUN_ON_START"
_TZ_ENV = "RETENTION_TZ"
def retention_zone() -> ZoneInfo:
name = os.environ.get(_TZ_ENV, DEFAULT_TZ).strip() or DEFAULT_TZ
return ZoneInfo(name)
def next_run_at(now: datetime, *, hour: int = 3) -> datetime:
"""Nächste Uhrzeit `hour`:00 in der Zeitzone von `now`.
`now` muss zeitzonenbewusst sein. Die Zone von `now` ist die Planungszone.
"""
if now.tzinfo is None:
raise ValueError("now muss eine Zeitzone tragen")
zone = now.tzinfo
local = now.astimezone(zone)
day = local.date()
target = datetime(day.year, day.month, day.day, hour, 0, 0, tzinfo=zone)
if target.timestamp() <= local.timestamp():
day = day + timedelta(days=1)
target = datetime(day.year, day.month, day.day, hour, 0, 0, tzinfo=zone)
return target
def seconds_until_next_0300(now: datetime, *, hour: int = 3) -> float:
"""Sekunden bis zum nächsten Lauf, als absolute Dauer.
`target - now` zieht bei gleicher tzinfo die Wanduhr ab und liegt an
Umstellungstagen um eine Stunde daneben. `timestamp()` rechnet in UTC.
"""
target = next_run_at(now, hour=hour)
local = now.astimezone(target.tzinfo)
return target.timestamp() - local.timestamp()
def _job_path() -> str:
return os.path.join(os.path.dirname(os.path.abspath(__file__)), "media_retention_job.py")
def _run_on_start() -> bool:
return os.environ.get(_RUN_ON_START, "").strip() == "1"
def run_job() -> int:
"""Startet den Retention-Job. Führt keine Löschung in diesem Prozess aus."""
LOG.info("Retention-Lauf startet")
completed = subprocess.run([sys.executable, "-u", _job_path()], check=False)
if completed.returncode != 0:
LOG.error("RETENTION_FAIL exit=%s", completed.returncode)
else:
LOG.info("RETENTION_OK")
return int(completed.returncode)
def serve(
*,
iterations: int | None = None,
sleep=time.sleep,
clock=None,
run=None,
) -> None:
zone = retention_zone()
if clock is None:
def clock() -> datetime:
return datetime.now(zone)
execute = run if run is not None else run_job
if _run_on_start():
LOG.info("Sofortlauf wegen RETENTION_RUN_ON_START=1")
execute()
completed = 0
while iterations is None or completed < iterations:
target = next_run_at(clock())
LOG.info("Nächster Retention-Lauf %s", target.isoformat(timespec="seconds"))
while True:
remaining = target.timestamp() - clock().timestamp()
if remaining <= 1:
break
LOG.info("Warte %.0f Sekunden bis %s", remaining, target.isoformat(timespec="seconds"))
sleep(remaining)
execute()
completed += 1
def main() -> int:
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)s %(message)s",
stream=sys.stdout,
force=True,
)
serve()
return 0
if __name__ == "__main__":
raise SystemExit(main())