forked from mtusi2026/mtusi2026lab6
Carry over the lab 5 solution
Bring the application, own tests, Dockerfile, toolchain, compose stack, HAProxy, k6, RustFS and RabbitMQ configuration, migrations, hooks, scenario scripts and the CI job from the lab 5 solution commit 64d5c67, keep the lab 5 Makefile targets on top of the unchanged skeleton targets, and append the lab 5 solution variables to the skeleton .env.example. The package and the application follow the contract version 1.6.0, and the image, the throwaway test containers and the broker vhost description are tagged lab6 so they do not collide with a lab 5 stand on the same host. This is the starting point of the lab 6 cycle: lint, own tests and the offline skeleton check pass before any lab 6 change. 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
a0d3947710
commit
5ae3d77152
@@ -0,0 +1,50 @@
|
||||
import asyncio
|
||||
|
||||
from dishka import FromDishka
|
||||
from faststream import AckPolicy
|
||||
from faststream.rabbit import Channel, QueueType, RabbitBroker, RabbitQueue
|
||||
from faststream.rabbit.annotations import RabbitMessage
|
||||
from pydantic import ValidationError
|
||||
|
||||
from messenger.attachments.application.bus import AttachmentsCommandBus
|
||||
from messenger.attachments.application.commands import ProcessJob
|
||||
from messenger.attachments.entrypoints.schemas import JobMessage
|
||||
from messenger.attachments.infrastructure.rabbitmq import QUEUE
|
||||
from messenger.shared.application.ports import Logger
|
||||
|
||||
REQUEUE_DELAY_SECONDS = 1.0
|
||||
JOBS = RabbitQueue(QUEUE, queue_type=QueueType.QUORUM, declare=False)
|
||||
|
||||
|
||||
async def raw(message: RabbitMessage) -> bytes:
|
||||
return message.body
|
||||
|
||||
|
||||
async def handle_job(
|
||||
message: RabbitMessage,
|
||||
bus: FromDishka[AttachmentsCommandBus],
|
||||
logger: FromDishka[Logger],
|
||||
) -> None:
|
||||
try:
|
||||
job = JobMessage.model_validate_json(message.body).job()
|
||||
except ValidationError:
|
||||
logger.warning("job.rejected", reason="message does not match jobs.schema.json")
|
||||
await message.reject(requeue=False)
|
||||
return
|
||||
try:
|
||||
await bus.execute(ProcessJob(job))
|
||||
except Exception as error:
|
||||
logger.warning("job.requeued", job_id=str(job.id), error=type(error).__name__)
|
||||
await asyncio.sleep(REQUEUE_DELAY_SECONDS)
|
||||
await message.nack(requeue=True)
|
||||
return
|
||||
await message.ack()
|
||||
|
||||
|
||||
def subscribe(broker: RabbitBroker, concurrency: int) -> None:
|
||||
broker.subscriber(
|
||||
JOBS,
|
||||
channel=Channel(prefetch_count=concurrency),
|
||||
ack_policy=AckPolicy.MANUAL,
|
||||
decoder=raw,
|
||||
)(handle_job)
|
||||
Reference in New Issue
Block a user