-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbroker.py
More file actions
27 lines (21 loc) · 826 Bytes
/
Copy pathbroker.py
File metadata and controls
27 lines (21 loc) · 826 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import logging
from typing import Any
from faststream import StreamMessage
from faststream.kafka import KafkaBroker
from app.domain.events.typed import DomainEvent, DomainEventAdapter
from app.events.schema.schema_registry import SchemaRegistryManager
from app.settings import Settings
def create_broker(
settings: Settings,
schema_registry: SchemaRegistryManager,
logger: logging.Logger,
) -> KafkaBroker:
"""Create a KafkaBroker with Avro decoder for standalone workers."""
async def avro_decoder(msg: StreamMessage[Any]) -> DomainEvent:
payload = await schema_registry.serializer.decode_message(msg.body)
return DomainEventAdapter.validate_python(payload)
return KafkaBroker(
settings.KAFKA_BOOTSTRAP_SERVERS,
decoder=avro_decoder,
logger=logger,
)