forked from mtusi2026/mtusi2026lab2
Source: immutable lab5 snapshot f88e9ba43cdfd7eb6a5a7ae5feb4247b4e820071; forward the allowed diff from 64d5c67. Carry SQLAlchemy, Alembic, reliability fixes, versioned jobs, infrastructure and student tests without changing protected coursework files.
Validation: make check, validate, lint; 515 student tests passed with one Linux-only skip; public smoke with telemetry enabled and repeat migration passed. Stop the owned stack and retain volumes. Preserve the existing OTel work in progress; grade implementation remains a separate phase.
90 lines
2.8 KiB
Python
90 lines
2.8 KiB
Python
from messenger.attachments.domain.events import (
|
|
AttachmentDiscarded,
|
|
AttachmentQueued,
|
|
AttachmentReplayRequested,
|
|
AttachmentReprocessRequested,
|
|
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,
|
|
)
|
|
|
|
async def on_reprocess_requested(self, event: AttachmentReprocessRequested) -> None:
|
|
self._logger.info(
|
|
"audit.attachment_reprocess",
|
|
attachment_id=str(event.attachment_id),
|
|
pipeline_version=event.pipeline_version,
|
|
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)
|
|
bus.subscribe(AttachmentReprocessRequested, journal.on_reprocess_requested)
|