forked from mtusi2026/mtusi2026lab2
Add the failed registry, an explicit replay and failed staging retention
python -m messenger.admin is a new process assembly over the attachments zone with only a database: "failed" lists failed jobs (served by the new partial index jobs_failed_idx), "replay --attachment-id" moves a failed attachment back to queued with the same job_id, attempt 1 and a fresh outbox row, writing a replayed event with the operator. A ready, queued or processing attachment is a noop (replay_noop event), a failed one whose staging is already gone is expired. Every request is audited. The relay now also removes the staging object of a failed attachment once STAGING_RETENTION_SECONDS have passed since it failed; replay and the cleanup take the same row lock. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
ecb4827e75
commit
b568fcf9c9
@@ -154,7 +154,7 @@ def test_every_zone_owns_its_http_layer() -> None:
|
||||
assert (SOURCE / zone / "entrypoints" / part).is_file(), f"{zone}: нет {part}"
|
||||
|
||||
|
||||
PROCESSES = {"relay.py": (ATTACHMENTS,), "worker.py": (ATTACHMENTS,)}
|
||||
PROCESSES = {"relay.py": (ATTACHMENTS,), "worker.py": (ATTACHMENTS,), "admin.py": (ATTACHMENTS,)}
|
||||
|
||||
|
||||
def test_only_main_knows_several_zones() -> None:
|
||||
|
||||
@@ -48,12 +48,15 @@ async def counts() -> list[int]:
|
||||
async def test_migrations_are_ordered_and_versioned() -> None:
|
||||
versions = [version for version, _ in migrations()]
|
||||
assert versions == sorted(versions)
|
||||
assert versions[:4] == [
|
||||
assert versions[:5] == [
|
||||
"0001_init",
|
||||
"0002_idempotency_keys",
|
||||
"0003_attachments",
|
||||
"0004_jobs",
|
||||
"0005_failed_registry",
|
||||
]
|
||||
plan = await scalar("SELECT indexdef FROM pg_indexes WHERE indexname = 'jobs_failed_idx'")
|
||||
assert "WHERE (status = 'failed'::text)" in plan
|
||||
applied = await scalar("SELECT array_agg(version ORDER BY version) FROM schema_migrations")
|
||||
assert applied == versions
|
||||
|
||||
|
||||
@@ -1,14 +1,18 @@
|
||||
import asyncio
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
from datetime import timedelta
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
import pytest
|
||||
from httpx import AsyncClient
|
||||
from test_attachments import storage
|
||||
from test_pipeline import accepted, events, job_of, rows, state, variants
|
||||
from test_pipeline import FailingFaults, accepted, events, job_of, rows, state, variants
|
||||
|
||||
from conftest import DATABASE
|
||||
from fakes import NullLogger
|
||||
from messenger.admin import serve
|
||||
from messenger.attachments.application.pipeline import (
|
||||
AFTER_CLAIM,
|
||||
AFTER_COMMIT_BEFORE_ACK,
|
||||
@@ -19,7 +23,7 @@ from messenger.attachments.application.pipeline import (
|
||||
TRANSIENT_ERROR,
|
||||
)
|
||||
from messenger.attachments.application.ports import Rendition
|
||||
from messenger.attachments.domain.jobs import Outcome
|
||||
from messenger.attachments.domain.jobs import Outcome, ReplayResult
|
||||
from messenger.attachments.infrastructure.faults import EnvironmentFaults
|
||||
from messenger.shared.application.errors import Unavailable
|
||||
from pipeline import InlinePipeline, InlineRenderer, pipeline_of
|
||||
@@ -229,3 +233,74 @@ def test_environment_fault_crashes_the_process_with_137() -> None:
|
||||
env={"PYTHONPATH": ":".join(sys.path)},
|
||||
)
|
||||
assert (done.returncode, done.stdout) == (137, "")
|
||||
|
||||
|
||||
async def failed(client: AsyncClient) -> tuple[InlinePipeline, str]:
|
||||
_, _, attachment_id = await accepted(client)
|
||||
pipeline = await pipeline_of(client)
|
||||
pipeline.faults = FailingFaults()
|
||||
await pipeline.drain()
|
||||
pipeline.faults = RecordingFaults()
|
||||
assert (await state(attachment_id))["error_code"] == "retry_exhausted"
|
||||
return pipeline, attachment_id
|
||||
|
||||
|
||||
async def test_replay_of_failed_reaches_ready_once(client: AsyncClient) -> None:
|
||||
pipeline, attachment_id = await failed(client)
|
||||
job = await job_of(attachment_id)
|
||||
assert await pipeline.replay(UUID(attachment_id)) is ReplayResult.REPLAYED
|
||||
queued = await state(attachment_id)
|
||||
assert (queued["status"], queued["job_status"], queued["attempt"], queued["error_code"]) == (
|
||||
"queued",
|
||||
"queued",
|
||||
1,
|
||||
None,
|
||||
)
|
||||
assert await outbox(attachment_id) == [True] * 5 + [False]
|
||||
await pipeline.drain()
|
||||
done = await state(attachment_id)
|
||||
assert (done["status"], done["attempt"]) == ("ready", 1)
|
||||
assert (await job_of(attachment_id)).id == job.id
|
||||
before = await variants(attachment_id)
|
||||
assert await pipeline.replay(UUID(attachment_id)) is ReplayResult.NOOP
|
||||
assert await outbox(attachment_id) == [True] * 6
|
||||
assert await variants(attachment_id) == before
|
||||
assert (await events(attachment_id))[-1] == "replay_noop"
|
||||
assert "replayed" in await events(attachment_id)
|
||||
assert await pipeline.replay(uuid4()) is ReplayResult.NOT_FOUND
|
||||
|
||||
|
||||
async def test_failed_staging_is_kept_for_the_retention_period(client: AsyncClient) -> None:
|
||||
pipeline, attachment_id = await failed(client)
|
||||
_, _, waiting = await accepted(client)
|
||||
objects = (await storage(client)).objects
|
||||
await pipeline.clean(timedelta(days=1))
|
||||
assert (await state(attachment_id))["staging_key"] == f"staging/{attachment_id}"
|
||||
assert f"staging/{attachment_id}" in objects
|
||||
await pipeline.clean(timedelta(0))
|
||||
assert (await state(attachment_id))["staging_key"] is None
|
||||
assert f"staging/{attachment_id}" not in objects
|
||||
assert (await state(waiting))["staging_key"] == f"staging/{waiting}"
|
||||
assert f"staging/{waiting}" in objects
|
||||
assert await pipeline.replay(UUID(attachment_id)) is ReplayResult.EXPIRED
|
||||
assert (await state(attachment_id))["status"] == "failed"
|
||||
|
||||
|
||||
async def test_operator_cli_lists_failed_jobs_and_replays(
|
||||
client: AsyncClient, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
||||
) -> None:
|
||||
_, attachment_id = await failed(client)
|
||||
monkeypatch.setenv("DATABASE_URL", DATABASE["url"])
|
||||
monkeypatch.setenv("LOG_LEVEL", "WARNING")
|
||||
assert await serve(["failed", "--limit", "500"]) == 0
|
||||
registry = json.loads(capsys.readouterr().out.splitlines()[-1])["failed"]
|
||||
(mine,) = [row for row in registry if row["attachment_id"] == attachment_id]
|
||||
assert (mine["error_code"], mine["attempt"], mine["last_error"]) == (
|
||||
"retry_exhausted",
|
||||
5,
|
||||
"Unavailable",
|
||||
)
|
||||
assert await serve(["replay", "--attachment-id", attachment_id, "--operator", "alice"]) == 0
|
||||
report = json.loads(capsys.readouterr().out.splitlines()[-1])
|
||||
assert report == {"result": "replayed", "attachment_id": attachment_id, "operator": "alice"}
|
||||
assert await serve(["replay", "--attachment-id", str(uuid4())]) == 1
|
||||
|
||||
Reference in New Issue
Block a user