-
Notifications
You must be signed in to change notification settings - Fork 0
/
dlq.py
60 lines (44 loc) · 1.35 KB
/
dlq.py
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
55
56
57
58
59
60
import asyncio
from unittest.mock import Mock
import pydantic
from fasteda import adapter, app, entity, interface
producer = Mock()
async def dlq_middleware(
event: interface.Event, next_: interface.Handler
) -> None:
try:
return await next_(event)
except Exception:
event.headers = {"dlq.topic-orig": event.topic, **event.headers}
event.topic = "dlq.topic"
producer.send(event)
apps = app.FastEDA(
adder=adapter.pydantic,
middlewares=[dlq_middleware],
)
class Client(pydantic.BaseModel):
id: int
name: str
@apps.add("client.create.v1")
def create_client(client: Client) -> None:
raise ValueError("Something went wrong")
async def dlq(event: interface.Event) -> None:
topic_orig = event.headers["dlq.topic-orig"]
event.topic = topic_orig
await apps.handle(event)
if __name__ == "__main__":
apps.add_handler("dlq.topic", dlq)
event = entity.Event(
topic="client.create.v1",
headers={},
body=b'{"id": 1, "name": "John Doe"}',
)
asyncio.run(apps.handle(event))
event = entity.Event(
topic="dlq.topic",
headers={"dlq.topic-orig": "client.create.v1"},
body=b'{"id": 1, "name": "John Doe"}',
)
producer.send.assert_called_once_with(event)
asyncio.run(apps.handle(event))
producer.send.assert_called_with(event)