Files
mtusi2026lab5/src/messenger/attachments/entrypoints/journal.py
T
ArtyomandClaude Opus 5.5 b568fcf9c9 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>
2026-10-29 03:12:19 +03:00

79 lines
2.4 KiB
Python

from messenger.attachments.domain.events import (
AttachmentDiscarded,
AttachmentQueued,
AttachmentReplayRequested,
JobSettled,
)
from messenger.shared.application.messagebus import EventBus
from messenger.shared.application.ports import Logger
LOG_FIELDS = frozenset(
{
"attachment_id",
"chat_id",
"actor_id",
"size_bytes",
"count",
"job_id",
"attempt",
"pipeline_version",
"outcome",
"error_code",
"delay_seconds",
"operator",
"result",
}
)
class AttachmentsJournal:
__slots__ = ("_logger",)
def __init__(self, logger: Logger) -> None:
self._logger = logger
async def on_attachment_queued(self, event: AttachmentQueued) -> None:
self._logger.info(
"audit.attachment_queued",
attachment_id=str(event.attachment_id),
chat_id=str(event.chat_id),
actor_id=str(event.owner_id),
size_bytes=event.size_bytes,
)
async def on_attachment_discarded(self, event: AttachmentDiscarded) -> None:
self._logger.info(
"audit.attachment_discarded",
attachment_id=str(event.attachment_id),
chat_id=str(event.chat_id),
reason=event.reason,
)
async def on_job_settled(self, event: JobSettled) -> None:
self._logger.info(
"audit.job_settled",
job_id=str(event.job_id),
attachment_id=str(event.attachment_id),
pipeline_version=event.pipeline_version,
attempt=event.attempt,
outcome=event.outcome,
error_code=event.error_code,
delay_seconds=event.delay_seconds,
)
async def on_replay_requested(self, event: AttachmentReplayRequested) -> None:
self._logger.info(
"audit.attachment_replay",
attachment_id=str(event.attachment_id),
operator=event.operator,
result=event.result,
)
def subscribe_journal(bus: EventBus, logger: Logger) -> None:
journal = AttachmentsJournal(logger)
bus.subscribe(AttachmentQueued, journal.on_attachment_queued)
bus.subscribe(AttachmentDiscarded, journal.on_attachment_discarded)
bus.subscribe(JobSettled, journal.on_job_settled)
bus.subscribe(AttachmentReplayRequested, journal.on_replay_requested)