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
@@ -40,3 +40,10 @@ class RecoverExpiredLeases(Command):
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class CleanStaging(Command):
|
||||
limit: int
|
||||
retention: timedelta
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class ReplayAttachment(Command):
|
||||
attachment_id: UUID
|
||||
operator: str
|
||||
|
||||
@@ -7,6 +7,7 @@ from messenger.attachments.application.commands import (
|
||||
ProcessJob,
|
||||
PublishOutbox,
|
||||
RecoverExpiredLeases,
|
||||
ReplayAttachment,
|
||||
)
|
||||
from messenger.attachments.application.ports import (
|
||||
AttachmentsUnitOfWork,
|
||||
@@ -17,9 +18,17 @@ from messenger.attachments.application.ports import (
|
||||
VariantRenderer,
|
||||
)
|
||||
from messenger.attachments.domain.errors import LeaseLost, UnreadableImage
|
||||
from messenger.attachments.domain.events import JobSettled
|
||||
from messenger.attachments.domain.jobs import Claim, FailureCode, Job, Lease, Outcome, RetryPolicy
|
||||
from messenger.attachments.domain.models import original_key
|
||||
from messenger.attachments.domain.events import AttachmentReplayRequested, JobSettled
|
||||
from messenger.attachments.domain.jobs import (
|
||||
Claim,
|
||||
FailureCode,
|
||||
Job,
|
||||
Lease,
|
||||
Outcome,
|
||||
ReplayResult,
|
||||
RetryPolicy,
|
||||
)
|
||||
from messenger.attachments.domain.models import Attachment, AttachmentStatus, original_key
|
||||
from messenger.attachments.domain.variants import Variant, variant_key
|
||||
from messenger.shared.application.errors import Unavailable
|
||||
from messenger.shared.application.ports import IdGenerator
|
||||
@@ -256,8 +265,37 @@ class CleanStagingHandler:
|
||||
|
||||
async def __call__(self, command: CleanStaging) -> int:
|
||||
async with self._uow.transaction():
|
||||
staged = await self._uow.attachments.staged(command.limit)
|
||||
staged = await self._uow.attachments.staged(command.limit, command.retention)
|
||||
await asyncio.gather(*(self._storage.delete(item.key) for item in staged))
|
||||
await self._uow.attachments.unstage([item.attachment_id for item in staged])
|
||||
await self._uow.commit()
|
||||
return len(staged)
|
||||
|
||||
|
||||
class ReplayAttachmentHandler:
|
||||
__slots__ = ("_uow",)
|
||||
|
||||
def __init__(self, uow: AttachmentsUnitOfWork) -> None:
|
||||
self._uow = uow
|
||||
|
||||
async def __call__(self, command: ReplayAttachment) -> ReplayResult:
|
||||
async with self._uow.transaction():
|
||||
attachment = await self._uow.attachments.lock(command.attachment_id)
|
||||
result = await self._replay(attachment, command.operator)
|
||||
if attachment is not None and result is not ReplayResult.REPLAYED:
|
||||
await self._uow.jobs.note_replay(attachment.id, command.operator, result.value)
|
||||
self._uow.record(
|
||||
AttachmentReplayRequested(command.attachment_id, command.operator, result.value)
|
||||
)
|
||||
await self._uow.commit()
|
||||
return result
|
||||
|
||||
async def _replay(self, attachment: Attachment | None, operator: str) -> ReplayResult:
|
||||
if attachment is None:
|
||||
return ReplayResult.NOT_FOUND
|
||||
if attachment.status is not AttachmentStatus.FAILED:
|
||||
return ReplayResult.NOOP
|
||||
if attachment.staging_key is None:
|
||||
return ReplayResult.EXPIRED
|
||||
job = await self._uow.jobs.replay(attachment, operator)
|
||||
return ReplayResult.NOOP if job is None else ReplayResult.REPLAYED
|
||||
|
||||
@@ -4,7 +4,7 @@ from datetime import datetime, timedelta
|
||||
from typing import Protocol, runtime_checkable
|
||||
from uuid import UUID
|
||||
|
||||
from messenger.attachments.domain.jobs import Claim, FailureCode, Job, Lease, Outcome
|
||||
from messenger.attachments.domain.jobs import Claim, FailedJob, FailureCode, Job, Lease, Outcome
|
||||
from messenger.attachments.domain.models import Attachment
|
||||
from messenger.attachments.domain.values import ImageInfo, ImageKind
|
||||
from messenger.attachments.domain.variants import Variant
|
||||
@@ -95,7 +95,7 @@ class AttachmentRepository(Protocol):
|
||||
|
||||
async def remove(self, attachment: Attachment) -> None: ...
|
||||
|
||||
async def staged(self, limit: int) -> list[StagedObject]: ...
|
||||
async def staged(self, limit: int, retention: timedelta) -> list[StagedObject]: ...
|
||||
|
||||
async def unstage(self, attachment_ids: Sequence[UUID]) -> None: ...
|
||||
|
||||
@@ -122,6 +122,10 @@ class JobRepository(Protocol):
|
||||
|
||||
async def expired(self, limit: int) -> list[Lease]: ...
|
||||
|
||||
async def replay(self, attachment: Attachment, operator: str) -> Job | None: ...
|
||||
|
||||
async def note_replay(self, attachment_id: UUID, operator: str, result: str) -> None: ...
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
class OutboxRepository(Protocol):
|
||||
@@ -147,3 +151,5 @@ class AttachmentDirectory(Protocol):
|
||||
async def visible(self, attachment_id: UUID) -> Attachment | None: ...
|
||||
|
||||
async def failed_among(self, chat_id: UUID, attachment_ids: Sequence[UUID]) -> bool: ...
|
||||
|
||||
async def failed_jobs(self, limit: int) -> list[FailedJob]: ...
|
||||
|
||||
Reference in New Issue
Block a user