Schedule media retention at 03:00 Europe/Berlin.
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
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
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>
This commit is contained in:
1 parent
f10c28e5cd
commit
95d365d9fa
7 files changed
+317
-21
No files matched your search
@@ -2,12 +2,12 @@
|
||||
"""
|
||||
Automatische Medien-Retention (Papierkorb Stufe 1→2, Purge wenn fällig).
|
||||
|
||||
Lauf z. B. täglich per Cron:
|
||||
Einzellauf (der tägliche Planer ist media_retention_scheduler.py):
|
||||
cd /path/to/backend && python scripts/media_retention_job.py
|
||||
|
||||
Umgebung wie Backend (DB_*), optional:
|
||||
MEDIA_TRASH_SOFT_TO_HIDDEN_DAYS (Default 30)
|
||||
MEDIA_TRASH_HIDDEN_TO_PURGE_DAYS (Default 90)
|
||||
MEDIA_TRASH_HIDDEN_TO_PURGE_DAYS (Default 30)
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
#!/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())
|
||||
Reference in new issue
Block a user