fix: reconcile and expose failed product cleanup

This commit is contained in:
2026-09-12 15:24:41 +08:00
parent bd78eef161
commit 21ad6fbf8d
6 changed files with 176 additions and 6 deletions
+57
View File
@@ -2,6 +2,7 @@ from __future__ import annotations
import json
import os
import sqlite3
import sys
from datetime import datetime, timedelta, timezone
from pathlib import Path
@@ -254,6 +255,62 @@ def test_failed_cleanup_product_can_be_restored_or_requeued(tmp_path, monkeypatc
]
def test_stale_purging_claim_is_reconciled_to_failed_cleanup(tmp_path, monkeypatch):
service = make_cleanup_service(tmp_path)
product = create_pending_product(service, purge_after=NOW)
store = service.product_store
original_fail = store.fail_product_purge
calls = {"n": 0}
def flaky_fail(product_id, now=None):
calls["n"] += 1
if calls["n"] == 1:
raise sqlite3.OperationalError("simulated busy")
return original_fail(product_id, now=now)
monkeypatch.setattr(store, "fail_product_purge", flaky_fail)
monkeypatch.setattr(
"service.cleanup_service.shutil.rmtree",
lambda *_args, **_kwargs: (_ for _ in ()).throw(OSError("disk failure")),
)
with pytest.raises(sqlite3.OperationalError, match="simulated busy"):
service.apply(now=NOW)
stranded = store._fetchone(
"SELECT status, claimed_at FROM cleanup_records WHERE product_id = ?",
(product.product_id,),
)
assert stranded["status"] == "purging"
assert datetime.fromisoformat(stranded["claimed_at"]) == NOW
report = service.preview(now=NOW + timedelta(hours=2))
assert [item.product_id for item in report.failed_products] == [product.product_id]
assert store.get_product(product.product_id).status == "failed_cleanup"
assert store.due_product_cleanups(NOW + timedelta(days=1)) == []
def test_cleanup_candidates_reports_failed_products(tmp_path, monkeypatch):
service = make_cleanup_service(tmp_path)
product = create_pending_product(service, purge_after=NOW)
store = service.product_store
store.claim_product_purge(product.product_id, NOW)
store.fail_product_purge(product.product_id, now=NOW)
monkeypatch.setattr(service_app, "cleanup_service", service, raising=False)
client = TestClient(service_app.app)
response = client.get("/api/maintenance/cleanup-candidates", headers=auth_header())
assert response.status_code == 200
data = response.json()
assert [item["product_id"] for item in data["failed_products"]] == [
product.product_id
]
assert data["pending_products"] == []
def test_apply_rechecks_archive_reference_immediately_before_job_deletion(tmp_path):
service = make_cleanup_service(tmp_path)
due_job = create_success_job(service, age_days=31)