-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdlq_v2.py
More file actions
54 lines (37 loc) · 1.15 KB
/
dlq_v2.py
File metadata and controls
54 lines (37 loc) · 1.15 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import random
from contextlib import asynccontextmanager
from functools import lru_cache
import aiokafka
import pydantic
from fasteda import adapter, app, interface
from fasteda.middleware.dlq import DLQ, HEADER_TOPIC_ORIG
DLQ_TOPIC = "myapp.dlq"
@lru_cache
def get_producer() -> aiokafka.AIOKafkaProducer:
# Need wrapper becouse AIOKafkaProducer should be created with running loop
return aiokafka.AIOKafkaProducer(
bootstrap_servers="kafka:9092",
)
@asynccontextmanager
async def lifespan():
async with get_producer():
yield
apps = app.FastEDA(
adapter=adapter.pydantic,
middlewares=[DLQ(DLQ_TOPIC, get_producer)],
lifespan=lifespan(),
)
async def dlq(event: interface.Event) -> None:
topic_orig = event.headers[HEADER_TOPIC_ORIG]
event.topic = topic_orig
await apps.handle(event)
apps.add_handler(DLQ_TOPIC, dlq)
class Client(pydantic.BaseModel):
id: int
name: str
@apps.add("myapp.create.v1")
def create_client(client: Client) -> None:
print(f"{client=} creating")
if random.random() > 0.01: # noqa: S311
raise ValueError("Something went wrong")
print(f"{client=} created")