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>
158 lines
5.0 KiB
Python
158 lines
5.0 KiB
Python
"""Scheduler für den Papierkorb-Job: 03:00 Europe/Berlin, ohne echten Löschlauf."""
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from datetime import datetime, timedelta
|
|
from pathlib import Path
|
|
from zoneinfo import ZoneInfo
|
|
|
|
import pytest
|
|
|
|
from scripts.media_retention_scheduler import (
|
|
next_run_at,
|
|
run_job,
|
|
seconds_until_next_0300,
|
|
serve,
|
|
)
|
|
|
|
BERLIN = ZoneInfo("Europe/Berlin")
|
|
REPO = Path(__file__).resolve().parents[2]
|
|
COMPOSE = REPO / "docker-compose.yml"
|
|
|
|
|
|
def test_winter_afternoon_waits_until_next_morning() -> None:
|
|
now = datetime(2026, 1, 15, 10, 0, tzinfo=BERLIN)
|
|
assert next_run_at(now).isoformat() == "2026-01-16T03:00:00+01:00"
|
|
assert seconds_until_next_0300(now) == 17 * 3600
|
|
|
|
|
|
def test_summer_before_three_is_same_morning() -> None:
|
|
now = datetime(2026, 7, 15, 2, 0, tzinfo=BERLIN)
|
|
assert next_run_at(now).isoformat() == "2026-07-15T03:00:00+02:00"
|
|
assert seconds_until_next_0300(now) == 3600
|
|
|
|
|
|
def test_exactly_0300_waits_until_next_day() -> None:
|
|
now = datetime(2026, 1, 15, 3, 0, tzinfo=BERLIN)
|
|
assert next_run_at(now).isoformat() == "2026-01-16T03:00:00+01:00"
|
|
assert seconds_until_next_0300(now) == 24 * 3600
|
|
|
|
|
|
def test_spring_forward_sleeps_absolute_22_hours() -> None:
|
|
now = datetime(2026, 3, 28, 4, 0, tzinfo=BERLIN)
|
|
target = next_run_at(now)
|
|
assert target.isoformat() == "2026-03-29T03:00:00+02:00"
|
|
assert seconds_until_next_0300(now) == 22 * 3600
|
|
assert (target - now).total_seconds() == 23 * 3600
|
|
|
|
|
|
def test_fall_back_sleeps_absolute_24_hours() -> None:
|
|
now = datetime(2026, 10, 24, 4, 0, tzinfo=BERLIN)
|
|
target = next_run_at(now)
|
|
assert target.isoformat() == "2026-10-25T03:00:00+01:00"
|
|
assert seconds_until_next_0300(now) == 24 * 3600
|
|
assert (target - now).total_seconds() == 23 * 3600
|
|
|
|
|
|
def test_naive_datetime_is_rejected() -> None:
|
|
with pytest.raises(ValueError):
|
|
next_run_at(datetime(2026, 1, 15, 10, 0))
|
|
|
|
|
|
def test_serve_sleeps_before_first_run(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.delenv("RETENTION_RUN_ON_START", raising=False)
|
|
state = {"now": datetime(2026, 1, 15, 10, 0, tzinfo=BERLIN)}
|
|
events: list[tuple] = []
|
|
|
|
def clock() -> datetime:
|
|
return state["now"]
|
|
|
|
def sleep(seconds: float) -> None:
|
|
events.append(("sleep", seconds))
|
|
state["now"] = state["now"] + timedelta(seconds=seconds)
|
|
|
|
def run() -> None:
|
|
events.append(("run",))
|
|
|
|
serve(iterations=1, sleep=sleep, clock=clock, run=run)
|
|
assert events == [("sleep", 17 * 3600), ("run",)]
|
|
|
|
|
|
def test_serve_can_run_once_on_start(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.setenv("RETENTION_RUN_ON_START", "1")
|
|
state = {"now": datetime(2026, 7, 15, 2, 0, tzinfo=BERLIN)}
|
|
events: list[str] = []
|
|
|
|
def sleep(seconds: float) -> None:
|
|
events.append("sleep")
|
|
state["now"] = state["now"] + timedelta(seconds=seconds)
|
|
|
|
serve(
|
|
iterations=1,
|
|
sleep=sleep,
|
|
clock=lambda: state["now"],
|
|
run=lambda: events.append("run"),
|
|
)
|
|
assert events == ["run", "sleep", "run"]
|
|
|
|
|
|
def test_serve_continues_after_failed_run(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.delenv("RETENTION_RUN_ON_START", raising=False)
|
|
state = {"now": datetime(2026, 1, 15, 10, 0, tzinfo=BERLIN)}
|
|
runs: list[int] = []
|
|
|
|
def sleep(seconds: float) -> None:
|
|
state["now"] = state["now"] + timedelta(seconds=seconds)
|
|
|
|
def run() -> int:
|
|
runs.append(1)
|
|
return 1
|
|
|
|
serve(iterations=2, sleep=sleep, clock=lambda: state["now"], run=run)
|
|
assert runs == [1, 1]
|
|
|
|
|
|
def test_run_job_logs_failure_without_starting_real_job(
|
|
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
|
|
) -> None:
|
|
class Done:
|
|
returncode = 1
|
|
|
|
monkeypatch.setattr(
|
|
"scripts.media_retention_scheduler.subprocess.run",
|
|
lambda *args, **kwargs: Done(),
|
|
)
|
|
with caplog.at_level(logging.INFO, logger="retention.scheduler"):
|
|
assert run_job() == 1
|
|
assert "RETENTION_FAIL exit=1" in caplog.text
|
|
|
|
|
|
def test_run_job_logs_success(
|
|
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
|
|
) -> None:
|
|
class Done:
|
|
returncode = 0
|
|
|
|
seen: list[list[str]] = []
|
|
|
|
def fake_run(cmd, check=False):
|
|
seen.append(list(cmd))
|
|
return Done()
|
|
|
|
monkeypatch.setattr("scripts.media_retention_scheduler.subprocess.run", fake_run)
|
|
with caplog.at_level(logging.INFO, logger="retention.scheduler"):
|
|
assert run_job() == 0
|
|
assert seen and seen[0][-1].endswith("media_retention_job.py")
|
|
assert "RETENTION_OK" in caplog.text
|
|
assert "RETENTION_FAIL" not in caplog.text
|
|
|
|
|
|
def test_compose_starts_scheduler_script() -> None:
|
|
text = COMPOSE.read_text(encoding="utf-8")
|
|
start = text.index(" retention-cron:")
|
|
block = text[start : text.index("\nvolumes:", start)]
|
|
assert 'command: ["python", "-u", "scripts/media_retention_scheduler.py"]' in block
|
|
assert "python -c" not in block
|
|
assert 'PYTHONUNBUFFERED: "1"' in block
|
|
assert "RETENTION_RUN_ON_START" not in block
|