Add media retention cleanup command
All checks were successful
buildbot/nix-eval Build done.
buildbot/nix-build Build done.
buildbot/nix-effects Build done.

This commit is contained in:
Abel Luck 2026-05-27 13:04:47 +02:00
parent 3b6503a6ed
commit 507074b80e
10 changed files with 722 additions and 52 deletions

View file

@ -1,3 +1,5 @@
import subprocess
import sys
from pathlib import Path
import pytest
@ -72,6 +74,66 @@ def test_main_publishes_staged_feed_after_successful_crawl(
assert not staged_path.exists()
def test_main_holds_media_cleanup_lock_during_crawl(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
out_dir = tmp_path / "out"
public_path = feed_output_path(out_dir=out_dir, feed_slug="demo")
staged_path = staged_feed_output_path(out_dir=out_dir, feed_slug="demo")
public_path.parent.mkdir(parents=True)
public_path.write_text("<rss>old</rss>\n", encoding="utf-8")
staged_path.write_text(VALID_FEED, encoding="utf-8")
def assert_media_lock_is_held(*, process, feed, spider_arguments) -> int:
lock_path = out_dir.resolve() / ".media-retention.lock"
script = """
import fcntl
import sys
from pathlib import Path
lock_path = Path(sys.argv[1])
lock_path.parent.mkdir(parents=True, exist_ok=True)
with lock_path.open("a", encoding="utf-8") as lock_file:
try:
fcntl.flock(lock_file.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
except BlockingIOError:
sys.exit(0)
else:
fcntl.flock(lock_file.fileno(), fcntl.LOCK_UN)
sys.exit(2)
"""
completed = subprocess.run(
[sys.executable, "-c", script, str(lock_path)],
cwd=Path.cwd(),
capture_output=True,
check=False,
text=True,
)
assert completed.returncode == 0, completed.stdout + completed.stderr
return 0
_patch_worker_dependencies(
monkeypatch, exit_code=0, run_crawl=assert_media_lock_is_held
)
exit_code = job_runner_module.main(
[
"--job-id",
"1",
"--execution-id",
"2",
"--db-path",
str(tmp_path / "republisher.db"),
"--out-dir",
str(out_dir),
"--stats-path",
str(tmp_path / "stats.jsonl"),
]
)
assert exit_code == 0
def test_main_does_not_publish_unusable_staged_feed_after_successful_crawl(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
@ -137,7 +199,7 @@ def test_main_does_not_publish_staged_feed_after_failed_crawl(
def _patch_worker_dependencies(
monkeypatch: pytest.MonkeyPatch, *, exit_code: int
monkeypatch: pytest.MonkeyPatch, *, exit_code: int, run_crawl=None
) -> None:
monkeypatch.setattr(
job_runner_module,
@ -161,5 +223,5 @@ def _patch_worker_dependencies(
monkeypatch.setattr(
job_runner_module,
"_run_crawl",
lambda *, process, feed, spider_arguments: exit_code,
run_crawl or (lambda *, process, feed, spider_arguments: exit_code),
)