-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandlers.py
More file actions
322 lines (290 loc) · 11.3 KB
/
Copy pathhandlers.py
File metadata and controls
322 lines (290 loc) · 11.3 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import dataclasses
from collections.abc import Awaitable, Callable
import structlog
from dishka.integrations.faststream import FromDishka
from faststream import AckPolicy
from faststream.kafka import KafkaBroker
from app.core.metrics import EventMetrics
from app.domain.enums import EventType
from app.domain.events import (
CreatePodCommandEvent,
DeletePodCommandEvent,
DomainEvent,
ExecutionCancelledEvent,
ExecutionCompletedEvent,
ExecutionFailedEvent,
ExecutionRequestedEvent,
ExecutionTimeoutEvent,
)
from app.domain.idempotency import KeyStrategy
from app.domain.sse import SSEExecutionEventData
from app.services.idempotency import IdempotencyManager
from app.services.k8s_worker import KubernetesWorker
from app.services.notification_service import NotificationService
from app.services.result_processor import ResultProcessor
from app.services.saga import SagaOrchestrator
from app.services.sse import SSEService
from app.settings import Settings
_sse_field_names: frozenset[str] = frozenset(f.name for f in dataclasses.fields(SSEExecutionEventData))
async def _track_consumed(
metrics: EventMetrics, event: DomainEvent, consumer_group: str, coro: Awaitable[None],
) -> None:
"""Await *coro* and record domain-level failure metric on error."""
try:
await coro
except Exception as e:
metrics.record_events_processing_failed(
topic=event.event_type, event_type=event.event_type,
consumer_group=consumer_group, error_type=type(e).__name__,
)
raise
# --8<-- [start:with_idempotency]
async def with_idempotency(
event: DomainEvent,
handler: Callable[..., Awaitable[None]],
idem: IdempotencyManager,
key_strategy: KeyStrategy,
ttl_seconds: int,
logger: structlog.stdlib.BoundLogger,
) -> None:
"""Run *handler* inside an idempotency guard (check -> execute -> mark)."""
result = await idem.check_and_reserve(
event=event, key_strategy=key_strategy, ttl_seconds=ttl_seconds,
)
if result.is_duplicate:
logger.info(f"Duplicate event: {event.event_type} ({event.event_id})")
return
try:
await handler(event)
await idem.mark_completed(event=event, key_strategy=key_strategy)
except Exception as e:
await idem.mark_failed(
event=event, error=str(e), key_strategy=key_strategy,
)
raise
# --8<-- [end:with_idempotency]
def register_k8s_worker_subscriber(broker: KafkaBroker) -> None:
@broker.subscriber(
EventType.CREATE_POD_COMMAND,
group_id="k8s-worker",
ack_policy=AckPolicy.ACK,
)
async def on_create_pod(
body: CreatePodCommandEvent,
worker: FromDishka[KubernetesWorker],
idem: FromDishka[IdempotencyManager],
logger: FromDishka[structlog.stdlib.BoundLogger],
event_metrics: FromDishka[EventMetrics],
) -> None:
await _track_consumed(event_metrics, body, "k8s-worker",
with_idempotency(body, worker.handle_create_pod_command, idem, KeyStrategy.CONTENT_HASH, 3600, logger))
@broker.subscriber(
EventType.DELETE_POD_COMMAND,
group_id="k8s-worker",
ack_policy=AckPolicy.ACK,
)
async def on_delete_pod(
body: DeletePodCommandEvent,
worker: FromDishka[KubernetesWorker],
idem: FromDishka[IdempotencyManager],
logger: FromDishka[structlog.stdlib.BoundLogger],
event_metrics: FromDishka[EventMetrics],
) -> None:
await _track_consumed(event_metrics, body, "k8s-worker",
with_idempotency(body, worker.handle_delete_pod_command, idem, KeyStrategy.CONTENT_HASH, 3600, logger))
def register_result_processor_subscriber(broker: KafkaBroker) -> None:
@broker.subscriber(
EventType.EXECUTION_COMPLETED,
group_id="result-processor",
ack_policy=AckPolicy.ACK,
max_poll_records=1,
auto_offset_reset="earliest",
)
async def on_execution_completed(
body: ExecutionCompletedEvent,
processor: FromDishka[ResultProcessor],
idem: FromDishka[IdempotencyManager],
logger: FromDishka[structlog.stdlib.BoundLogger],
event_metrics: FromDishka[EventMetrics],
) -> None:
await _track_consumed(event_metrics, body, "result-processor",
with_idempotency(body, processor.handle_execution_completed, idem, KeyStrategy.CONTENT_HASH, 7200, logger))
@broker.subscriber(
EventType.EXECUTION_FAILED,
group_id="result-processor",
ack_policy=AckPolicy.ACK,
max_poll_records=1,
auto_offset_reset="earliest",
)
async def on_execution_failed(
body: ExecutionFailedEvent,
processor: FromDishka[ResultProcessor],
idem: FromDishka[IdempotencyManager],
logger: FromDishka[structlog.stdlib.BoundLogger],
event_metrics: FromDishka[EventMetrics],
) -> None:
await _track_consumed(event_metrics, body, "result-processor",
with_idempotency(body, processor.handle_execution_failed, idem, KeyStrategy.CONTENT_HASH, 7200, logger))
@broker.subscriber(
EventType.EXECUTION_TIMEOUT,
group_id="result-processor",
ack_policy=AckPolicy.ACK,
max_poll_records=1,
auto_offset_reset="earliest",
)
async def on_execution_timeout(
body: ExecutionTimeoutEvent,
processor: FromDishka[ResultProcessor],
idem: FromDishka[IdempotencyManager],
logger: FromDishka[structlog.stdlib.BoundLogger],
event_metrics: FromDishka[EventMetrics],
) -> None:
await _track_consumed(event_metrics, body, "result-processor",
with_idempotency(body, processor.handle_execution_timeout, idem, KeyStrategy.CONTENT_HASH, 7200, logger))
def register_saga_subscriber(broker: KafkaBroker) -> None:
@broker.subscriber(
EventType.EXECUTION_REQUESTED,
group_id="saga-orchestrator",
ack_policy=AckPolicy.ACK,
)
async def on_execution_requested(
body: ExecutionRequestedEvent,
orchestrator: FromDishka[SagaOrchestrator],
idem: FromDishka[IdempotencyManager],
logger: FromDishka[structlog.stdlib.BoundLogger],
event_metrics: FromDishka[EventMetrics],
) -> None:
coro = with_idempotency(
body, orchestrator.handle_execution_requested, idem, KeyStrategy.EVENT_BASED, 3600, logger,
)
await _track_consumed(event_metrics, body, "saga-orchestrator", coro)
@broker.subscriber(
EventType.EXECUTION_COMPLETED,
group_id="saga-orchestrator",
ack_policy=AckPolicy.ACK,
)
async def on_execution_completed(
body: ExecutionCompletedEvent,
orchestrator: FromDishka[SagaOrchestrator],
event_metrics: FromDishka[EventMetrics],
) -> None:
await _track_consumed(event_metrics, body, "saga-orchestrator",
orchestrator.handle_execution_completed(body))
@broker.subscriber(
EventType.EXECUTION_FAILED,
group_id="saga-orchestrator",
ack_policy=AckPolicy.ACK,
)
async def on_execution_failed(
body: ExecutionFailedEvent,
orchestrator: FromDishka[SagaOrchestrator],
event_metrics: FromDishka[EventMetrics],
) -> None:
await _track_consumed(event_metrics, body, "saga-orchestrator",
orchestrator.handle_execution_failed(body))
@broker.subscriber(
EventType.EXECUTION_TIMEOUT,
group_id="saga-orchestrator",
ack_policy=AckPolicy.ACK,
)
async def on_execution_timeout(
body: ExecutionTimeoutEvent,
orchestrator: FromDishka[SagaOrchestrator],
event_metrics: FromDishka[EventMetrics],
) -> None:
await _track_consumed(event_metrics, body, "saga-orchestrator",
orchestrator.handle_execution_timeout(body))
@broker.subscriber(
EventType.EXECUTION_CANCELLED,
group_id="saga-orchestrator",
ack_policy=AckPolicy.ACK,
)
async def on_execution_cancelled(
body: ExecutionCancelledEvent,
orchestrator: FromDishka[SagaOrchestrator],
event_metrics: FromDishka[EventMetrics],
) -> None:
await _track_consumed(event_metrics, body, "saga-orchestrator",
orchestrator.handle_execution_cancelled(body))
_SSE_EVENT_TYPES = [
EventType.EXECUTION_REQUESTED,
EventType.EXECUTION_QUEUED,
EventType.EXECUTION_STARTED,
EventType.EXECUTION_RUNNING,
EventType.EXECUTION_COMPLETED,
EventType.EXECUTION_FAILED,
EventType.EXECUTION_TIMEOUT,
EventType.EXECUTION_CANCELLED,
EventType.RESULT_STORED,
EventType.RESULT_FAILED,
EventType.POD_CREATED,
EventType.POD_SCHEDULED,
EventType.POD_RUNNING,
EventType.POD_SUCCEEDED,
EventType.POD_FAILED,
EventType.POD_TERMINATED,
EventType.POD_DELETED,
]
def register_sse_subscriber(broker: KafkaBroker, settings: Settings) -> None:
group_id = "sse-bridge-pool"
@broker.subscriber(
*_SSE_EVENT_TYPES,
group_id=group_id,
ack_policy=AckPolicy.ACK_FIRST,
auto_offset_reset="latest",
max_workers=settings.SSE_CONSUMER_POOL_SIZE,
)
async def on_sse_event(
body: DomainEvent,
sse_service: FromDishka[SSEService],
) -> None:
execution_id = getattr(body, "execution_id", None)
if execution_id:
sse_data = SSEExecutionEventData(**{
k: v for k, v in body.model_dump().items() if k in _sse_field_names
})
await sse_service.publish_event(execution_id, sse_data)
def register_notification_subscriber(broker: KafkaBroker) -> None:
group_id = "notification-service"
@broker.subscriber(
EventType.EXECUTION_COMPLETED,
group_id=group_id,
ack_policy=AckPolicy.ACK,
max_poll_records=10,
auto_offset_reset="latest",
)
async def on_execution_completed(
body: ExecutionCompletedEvent,
service: FromDishka[NotificationService],
event_metrics: FromDishka[EventMetrics],
) -> None:
await _track_consumed(event_metrics, body, group_id,
service.handle_execution_completed(body))
@broker.subscriber(
EventType.EXECUTION_FAILED,
group_id=group_id,
ack_policy=AckPolicy.ACK,
max_poll_records=10,
auto_offset_reset="latest",
)
async def on_execution_failed(
body: ExecutionFailedEvent,
service: FromDishka[NotificationService],
event_metrics: FromDishka[EventMetrics],
) -> None:
await _track_consumed(event_metrics, body, group_id,
service.handle_execution_failed(body))
@broker.subscriber(
EventType.EXECUTION_TIMEOUT,
group_id=group_id,
ack_policy=AckPolicy.ACK,
max_poll_records=10,
auto_offset_reset="latest",
)
async def on_execution_timeout(
body: ExecutionTimeoutEvent,
service: FromDishka[NotificationService],
event_metrics: FromDishka[EventMetrics],
) -> None:
await _track_consumed(event_metrics, body, group_id,
service.handle_execution_timeout(body))