Add the JobObserver port to the attachments zone and its adapter on the OpenTelemetry API: every delivery adds one to messenger.jobs.processed and records messenger.job.duration in seconds with buckets from 50 ms to 60 s, both labelled only by job.type and outcome, and puts the job id, the attachment id, the attempt and the outcome on the current consumer span, which turns to ERROR on retry_scheduled, failed and error. The worker entrypoint measures each delivery, reports the outcome of the handler or error when it raised, and writes a job.processed record. Without the SDK the adapter runs on the no-op providers of the API. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
266 lines
8.3 KiB
Python
266 lines
8.3 KiB
Python
import asyncio
|
|
import base64
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import re
|
|
import subprocess
|
|
import time
|
|
from collections.abc import AsyncIterator, Iterator
|
|
from contextlib import asynccontextmanager
|
|
from datetime import UTC, datetime
|
|
from typing import Any
|
|
from uuid import uuid4
|
|
|
|
import pytest
|
|
from dishka import Provider, Scope, make_async_container, provide
|
|
from dishka_faststream import setup_dishka
|
|
from faststream.rabbit import Channel, RabbitBroker
|
|
|
|
from conftest import ROOT, STARTUP_SECONDS, container, image, published
|
|
from fakes import NullLogger
|
|
from messenger.attachments.application.bus import AttachmentsCommandBus
|
|
from messenger.attachments.application.commands import ProcessJob
|
|
from messenger.attachments.application.ports import JobObserver
|
|
from messenger.attachments.domain.jobs import Job, Outcome
|
|
from messenger.attachments.entrypoints.worker import subscribe
|
|
from messenger.attachments.infrastructure.rabbitmq import RabbitJobPublisher
|
|
from messenger.attachments.infrastructure.telemetry import OtelJobObserver
|
|
from messenger.shared.application.errors import Unavailable
|
|
from messenger.shared.application.ports import Logger
|
|
|
|
RABBITMQ_IMAGE = re.compile(r"image: (rabbitmq:\S+)")
|
|
PASSWORDS = {role: os.urandom(12).hex() for role in ("api", "worker", "ops")}
|
|
GRANTS = {
|
|
"api": ("^$", "^attachments$", "^$"),
|
|
"worker": ("^$", "^$", "^attachments\\.process$"),
|
|
"ops": (".*", ".*", ".*"),
|
|
}
|
|
|
|
|
|
def user(role: str) -> dict[str, Any]:
|
|
salt = os.urandom(4)
|
|
digest = hashlib.sha256(salt + PASSWORDS[role].encode()).digest()
|
|
return {
|
|
"name": f"messenger-{role}",
|
|
"password_hash": base64.b64encode(salt + digest).decode(),
|
|
"hashing_algorithm": "rabbit_password_hashing_sha256",
|
|
"tags": ["administrator"] if role == "ops" else [],
|
|
}
|
|
|
|
|
|
def definitions() -> dict[str, Any]:
|
|
return {
|
|
"users": [user(role) for role in PASSWORDS],
|
|
"permissions": [
|
|
{
|
|
"user": f"messenger-{role}",
|
|
"vhost": "messenger",
|
|
**dict(zip(("configure", "write", "read"), grant, strict=True)),
|
|
}
|
|
for role, grant in GRANTS.items()
|
|
],
|
|
}
|
|
|
|
|
|
def ready(name: str) -> bool:
|
|
probe = ["docker", "exec", name, "rabbitmq-diagnostics", "-q", "check_port_connectivity"]
|
|
return subprocess.run(probe, capture_output=True, check=False).returncode == 0
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def rabbitmq() -> Iterator[dict[str, Any]]:
|
|
name = f"mtusi-lab6-test-rabbitmq-{uuid4().hex[:8]}"
|
|
folder = ROOT / "build" / name
|
|
folder.mkdir(parents=True)
|
|
users = folder / "20-users.json"
|
|
users.write_text(json.dumps(definitions()))
|
|
users.chmod(0o644)
|
|
infra = ROOT / "infra" / "rabbitmq"
|
|
mounts = [
|
|
f"{infra / 'rabbitmq.conf'}:/etc/rabbitmq/conf.d/20-messenger.conf:ro",
|
|
f"{infra / 'enabled_plugins'}:/etc/rabbitmq/enabled_plugins:ro",
|
|
f"{infra / 'definitions.json'}:/etc/rabbitmq/definitions.d/10-topology.json:ro",
|
|
f"{users}:/etc/rabbitmq/definitions.d/20-users.json:ro",
|
|
]
|
|
volumes = [part for mount in mounts for part in ("-v", mount)]
|
|
container(
|
|
name,
|
|
"--hostname",
|
|
"broker",
|
|
"--user",
|
|
"100:101",
|
|
*volumes,
|
|
"-p",
|
|
"127.0.0.1::5672",
|
|
image(RABBITMQ_IMAGE),
|
|
)
|
|
try:
|
|
deadline = time.monotonic() + STARTUP_SECONDS * 2
|
|
while not ready(name):
|
|
assert time.monotonic() < deadline, "RabbitMQ не стал готов"
|
|
time.sleep(1)
|
|
port = published(name, 5672)
|
|
urls = {
|
|
role: f"amqp://messenger-{role}:{secret}@127.0.0.1:{port}/messenger"
|
|
for role, secret in PASSWORDS.items()
|
|
}
|
|
yield {"name": name, "urls": urls}
|
|
finally:
|
|
subprocess.run(["docker", "rm", "-f", name], capture_output=True, check=False)
|
|
users.unlink()
|
|
folder.rmdir()
|
|
|
|
|
|
def depth(broker: dict[str, Any], queue: str) -> int:
|
|
listing = subprocess.run(
|
|
[
|
|
"docker",
|
|
"exec",
|
|
broker["name"],
|
|
"rabbitmqctl",
|
|
"-q",
|
|
"list_queues",
|
|
"-p",
|
|
"messenger",
|
|
"name",
|
|
"messages",
|
|
"--formatter",
|
|
"json",
|
|
],
|
|
capture_output=True,
|
|
check=True,
|
|
text=True,
|
|
).stdout
|
|
return {row["name"]: row["messages"] for row in json.loads(listing)}[queue]
|
|
|
|
|
|
async def wait_for(probe: Any, seconds: float = 20) -> None:
|
|
deadline = time.monotonic() + seconds
|
|
while not probe():
|
|
assert time.monotonic() < deadline
|
|
await asyncio.sleep(0.1)
|
|
|
|
|
|
class NoEvents:
|
|
@asynccontextmanager
|
|
async def transaction(self) -> AsyncIterator[None]:
|
|
yield
|
|
|
|
async def commit(self) -> None: ...
|
|
|
|
def record(self, event: object) -> None: ...
|
|
|
|
def collect_new_events(self) -> list[Any]:
|
|
return []
|
|
|
|
|
|
class Recorder:
|
|
def __init__(self) -> None:
|
|
self.jobs: list[Job] = []
|
|
self.failures = 0
|
|
|
|
async def __call__(self, command: ProcessJob) -> Outcome:
|
|
if self.failures:
|
|
self.failures -= 1
|
|
raise Unavailable("PostgreSQL недоступен")
|
|
self.jobs.append(command.job)
|
|
return Outcome.DONE
|
|
|
|
|
|
class FakeBus(Provider):
|
|
def __init__(self, recorder: Recorder) -> None:
|
|
super().__init__()
|
|
self._recorder = recorder
|
|
|
|
@provide(scope=Scope.APP)
|
|
def logger(self) -> Logger:
|
|
return NullLogger()
|
|
|
|
@provide(scope=Scope.APP)
|
|
def observer(self) -> JobObserver:
|
|
return OtelJobObserver()
|
|
|
|
@provide(scope=Scope.REQUEST)
|
|
def bus(self) -> AttachmentsCommandBus:
|
|
return AttachmentsCommandBus({ProcessJob: self._recorder}, NoEvents(), NoPublisher())
|
|
|
|
|
|
class NoPublisher:
|
|
async def publish(self, events: Any) -> None: ...
|
|
|
|
|
|
@asynccontextmanager
|
|
async def worker(rabbitmq: dict[str, Any], recorder: Recorder) -> AsyncIterator[None]:
|
|
broker = RabbitBroker(rabbitmq["urls"]["worker"], logger=None, graceful_timeout=2)
|
|
subscribe(broker, 2)
|
|
setup_dishka(make_async_container(FakeBus(recorder)), broker=broker, auto_inject=True)
|
|
await broker.start()
|
|
try:
|
|
yield
|
|
finally:
|
|
await broker.stop()
|
|
|
|
|
|
@asynccontextmanager
|
|
async def publisher(
|
|
rabbitmq: dict[str, Any], role: str = "api"
|
|
) -> AsyncIterator[RabbitJobPublisher]:
|
|
broker = RabbitBroker(
|
|
rabbitmq["urls"][role],
|
|
default_channel=Channel(publisher_confirms=True, on_return_raises=True),
|
|
logger=None,
|
|
)
|
|
await broker.connect()
|
|
try:
|
|
yield RabbitJobPublisher(broker, 5)
|
|
finally:
|
|
await broker.stop()
|
|
|
|
|
|
def job() -> Job:
|
|
return Job(uuid4(), uuid4(), 1, 1, datetime.now(UTC))
|
|
|
|
|
|
def body(payload: dict[str, Any]) -> bytes:
|
|
return json.dumps(payload).encode()
|
|
|
|
|
|
async def test_confirmed_job_is_consumed_and_acked(rabbitmq: dict[str, Any]) -> None:
|
|
recorder, sent = Recorder(), job()
|
|
async with publisher(rabbitmq) as jobs:
|
|
await jobs.publish(sent.id, body(sent.payload()))
|
|
await wait_for(lambda: depth(rabbitmq, "attachments.process") == 1)
|
|
async with worker(rabbitmq, recorder):
|
|
await wait_for(lambda: recorder.jobs)
|
|
await wait_for(lambda: depth(rabbitmq, "attachments.process") == 0)
|
|
assert recorder.jobs == [sent]
|
|
|
|
|
|
async def test_message_off_the_schema_goes_to_the_dead_letter_queue(
|
|
rabbitmq: dict[str, Any],
|
|
) -> None:
|
|
before = depth(rabbitmq, "attachments.process.dlq")
|
|
async with publisher(rabbitmq) as jobs:
|
|
await jobs.publish(uuid4(), body({"schema_version": 2, "job_id": str(uuid4())}))
|
|
async with worker(rabbitmq, Recorder()):
|
|
await wait_for(lambda: depth(rabbitmq, "attachments.process.dlq") == before + 1)
|
|
assert depth(rabbitmq, "attachments.process") == 0
|
|
|
|
|
|
async def test_failed_processing_is_requeued_and_retried(rabbitmq: dict[str, Any]) -> None:
|
|
recorder, sent = Recorder(), job()
|
|
recorder.failures = 1
|
|
async with publisher(rabbitmq) as jobs:
|
|
await jobs.publish(sent.id, body(sent.payload()))
|
|
async with worker(rabbitmq, recorder):
|
|
await wait_for(lambda: recorder.jobs)
|
|
assert recorder.jobs == [sent]
|
|
|
|
|
|
async def test_worker_account_cannot_publish(rabbitmq: dict[str, Any]) -> None:
|
|
sent = job()
|
|
with pytest.raises(Unavailable):
|
|
async with publisher(rabbitmq, "worker") as jobs:
|
|
await jobs.publish(sent.id, body(sent.payload()))
|