Pub Sub
zenml.pub_sub
Modules
base
Base abstractions for the pub/sub layer.
Classes
ConsumerBase(config: ConsumerRuntimeConfig, executor: Executor, event_handler: CriticalEventHandler | None = None)
Bases: ABC
Async core pipeline shared by polling and push transports.
The transport decides how raw messages arrive. The base class provides: - decode/preprocess - execute with retries - ack with retries (+ critical event on exhaustion) - invalid/failing handlers
Consumer base constructor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
ConsumerRuntimeConfig
|
The consumer config object. |
required |
executor
|
Executor
|
An executor instance. |
required |
event_handler
|
CriticalEventHandler | None
|
A CriticalEventHandler instance (optional). |
None
|
Source code in src/zenml/pub_sub/base.py
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | |
config: ConsumerRuntimeConfig
property
Implements the 'config' property.
Returns:
| Type | Description |
|---|---|
ConsumerRuntimeConfig
|
The consumer config object. |
acknowledge_message(raw_message: Any) -> None
abstractmethod
async
Acks/Deletes message from the queue.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_message
|
Any
|
The raw broker-specific message. |
required |
Source code in src/zenml/pub_sub/base.py
361 362 363 364 365 366 367 | |
handle_critical_event(event: CriticalEvent) -> None
async
Critical event handler.
A critical event indicates that the application behavior has degraded. For instance, the inability to serialize the message to a broker-compatible format is extremely severe as it most probably repeats across messages meaning no messages are published for execution at all.
A complete implementation of a critical event handling process would be the following:
- Log in detailed fashion the event so that it is traceable in the logs.
- Publish the event to an event aggregator service so that it is tracked long-term and alerts can be generated based on its frequency and severity.
- Hold the message in a DLQ to enable re-execution when a bug fix is introduced in the code.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
CriticalEvent
|
A critical event instance. |
required |
Source code in src/zenml/pub_sub/base.py
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 | |
handle_invalid_message(raw_message: Any) -> None
abstractmethod
async
Handles message of invalid format (not suitable for execution).
Messages that are invalid should still be acked to avoid looping execution and system poisoning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_message
|
Any
|
The raw broker-specific message. |
required |
Source code in src/zenml/pub_sub/base.py
369 370 371 372 373 374 375 376 377 378 379 | |
handle_raw_message(raw_message: Any) -> None
async
Handles one message at a time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_message
|
Any
|
A raw broker-specific message. |
required |
Source code in src/zenml/pub_sub/base.py
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 | |
preprocess_message(raw_message: Any) -> MessageEnvelope
abstractmethod
async
Decode/Validate broker message and wrap to MessageEnvelope.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_message
|
Any
|
Broker-specific message. |
required |
Returns:
| Type | Description |
|---|---|
MessageEnvelope
|
A MessageEnvelope object. |
Source code in src/zenml/pub_sub/base.py
342 343 344 345 346 347 348 349 350 351 | |
process_message(message: MessageEnvelope) -> None
async
Execution of message payload.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
MessageEnvelope
|
A MessageEnvelope object. |
required |
Source code in src/zenml/pub_sub/base.py
353 354 355 356 357 358 359 | |
run() -> None
abstractmethod
async
Starts the consumer task.
Source code in src/zenml/pub_sub/base.py
470 471 472 473 | |
ConsumerRuntimeConfig
Bases: ConfigBase
Config shared by polling and push consumers.
prefixes() -> list[str]
staticmethod
Implementation of the prefixes config method.
Returns:
| Type | Description |
|---|---|
list[str]
|
A list of prefixes to use for env var name resolution. |
Source code in src/zenml/pub_sub/base.py
232 233 234 235 236 237 238 239 | |
CriticalEventHandler
Bases: ABC
Abstraction for handling critical events.
handle(event: CriticalEvent) -> None
abstractmethod
async
Handles a critical event.
Handling of a critical event would be include the following operations: - Tracking of the event instance and their evolution - Alerting mechanisms based on event monitoring alarms - Mitigation actions
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
CriticalEvent
|
A CriticalEvent instance. |
required |
Source code in src/zenml/pub_sub/base.py
79 80 81 82 83 84 85 86 87 88 89 90 91 | |
Executor
Bases: ABC
Abstract executor interface.
execute(payload: MessagePayload) -> None
abstractmethod
async
Executor main method.
Responsible with executing the payload and adding implementing additional layers of error handling and retries:
- Should decide whether to propagate errors to the queue layer.
- Should decide whether retries are applicable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
MessagePayload
|
A standard message payload. |
required |
Source code in src/zenml/pub_sub/base.py
272 273 274 275 276 277 278 279 280 281 282 283 284 | |
max_message_capacity() -> int | None
abstractmethod
Helper utility for variable batch size consumption.
Helpful when execution needs to be throttled. The executor responds with the number of messages it can pickup for execution at the moment.
Returns:
| Type | Description |
|---|---|
int | None
|
Number of messages the executor can handle (None if no limit). |
Source code in src/zenml/pub_sub/base.py
291 292 293 294 295 296 297 298 299 300 301 | |
shutdown() -> None
abstractmethod
async
Graceful executor shutdown (completes work, then exits).
Source code in src/zenml/pub_sub/base.py
286 287 288 289 | |
MessageDecodeError
Bases: Exception
Raised when a raw broker message cannot be decoded/preprocessed.
MessageEncodeError
Bases: Exception
Raised when a payload cannot be encoded into a broker message.
MessageExecutionError
Bases: Exception
Raised when message processing fails after retries.
MessageSubmissionError
Bases: Exception
Raised when sending a message fails.
PollingConfig
Bases: ConsumerRuntimeConfig
Base config class for polling consumers.
prefixes() -> list[str]
staticmethod
Implementation of the prefixes config method.
Returns:
| Type | Description |
|---|---|
list[str]
|
A list of prefixes to use for env var name resolution. |
Source code in src/zenml/pub_sub/base.py
479 480 481 482 483 484 485 486 487 488 | |
PollingConsumer(config: PollingConfig, executor: Executor, event_handler: CriticalEventHandler | None = None)
Bases: ConsumerBase, ABC
Polling transport: the consumer fetches batches periodically.
Polling consumer constructor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
PollingConfig
|
Polling config instance. |
required |
executor
|
Executor
|
Executor instance. |
required |
event_handler
|
CriticalEventHandler | None
|
A CriticalEventHandler instance (optional). |
None
|
Source code in src/zenml/pub_sub/base.py
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 | |
polling_interval: float
property
Implement the 'polling_interval' property.
Returns:
| Type | Description |
|---|---|
float
|
The polling interval value. |
get_messages_batch_size() -> int
abstractmethod
Getter for the batch size.
Enables implementations that adapt batch size per iteration based on configuration, throttling or other factors.
Returns:
| Type | Description |
|---|---|
int
|
The size of the next batch of messages. |
Source code in src/zenml/pub_sub/base.py
554 555 556 557 558 559 560 561 562 563 | |
poll_once() -> None
async
Receive and handle one batch. Receive failures emit read_failed.
Source code in src/zenml/pub_sub/base.py
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 | |
receive_messages(batch_size: int | None = None) -> list[Any]
abstractmethod
async
Requests to get a batch of messages from the queue.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_size
|
int | None
|
Number of messages to get. |
None
|
Returns:
| Type | Description |
|---|---|
list[Any]
|
A list of messages (broker-specific payload). |
Source code in src/zenml/pub_sub/base.py
541 542 543 544 545 546 547 548 549 550 551 552 | |
run() -> None
async
Run polling loop until stop() is called.
Source code in src/zenml/pub_sub/base.py
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 | |
stop() -> None
Signal the run loop to stop.
Source code in src/zenml/pub_sub/base.py
528 529 530 | |
ProducerBase(config: ProducerConfig, event_handler: CriticalEventHandler | None = None)
Bases: ABC
Async producer base.
Backends implement
1) build_message(payload) -> backend-specific message object (Any) 2) send_message(message) -> submit to broker
Base provides
- publish(payload): build + send (+ optional retries) and emits CriticalEvent on failure.
Producer constructor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
ProducerConfig
|
The producer config object. |
required |
event_handler
|
CriticalEventHandler | None
|
A CriticalEventHandler instance (optional). |
None
|
Source code in src/zenml/pub_sub/base.py
105 106 107 108 109 110 111 112 113 114 115 116 117 | |
config: ProducerConfig
property
Implements the 'config' property.
Returns:
| Type | Description |
|---|---|
ProducerConfig
|
The producer config object. |
build_message(payload: MessagePayload) -> Any
abstractmethod
async
Convert the message to a broker-specific payload.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
MessagePayload
|
A MessagePayload object. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
A broker-specific message. |
Source code in src/zenml/pub_sub/base.py
128 129 130 131 132 133 134 135 136 137 | |
handle_critical_event(event: CriticalEvent) -> None
async
Critical event handler.
A critical event indicates that the application behavior has degraded. For instance, the inability to serialize the message to a broker-compatible format is extremely severe as it most probably repeats across messages meaning no messages are published for execution at all.
A complete implementation of a critical event handling process would be the following:
- Log in detailed fashion the event so that it is traceable in the logs.
- Publish the event to an event aggregator service so that it is tracked long-term and alerts can be generated based on its frequency and severity.
- Hold the message in a DLQ to enable re-execution when a bug fix is introduced in the code.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
CriticalEvent
|
A critical event instance. |
required |
Source code in src/zenml/pub_sub/base.py
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 | |
publish(payload: MessagePayload) -> str
async
Build and send message, emitting CriticalEvent on failure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
MessagePayload
|
Application payload. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Message ID generated by the broker. |
Raises:
| Type | Description |
|---|---|
MessageEncodeError
|
If encoding/building fails. |
MessageSubmissionError
|
If sending fails after retries. |
Source code in src/zenml/pub_sub/base.py
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 | |
send_message(message: Any) -> str
abstractmethod
async
Sends a message to the broker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
Any
|
A broker-specific message. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The message ID generated by the broker. |
Source code in src/zenml/pub_sub/base.py
139 140 141 142 143 144 145 146 147 148 | |
ProducerConfig
Bases: ConfigBase
Config object grouping producer configuration params.
prefixes() -> list[str]
staticmethod
Implementation of the prefixes config method.
Returns:
| Type | Description |
|---|---|
list[str]
|
A list of prefixes to use for env var name resolution. |
Source code in src/zenml/pub_sub/base.py
60 61 62 63 64 65 66 67 | |
models
Models for the pub/sub layer.
Classes
CriticalEvent
Bases: BaseModel
Class capturing a critical event basic properties.
CriticalEventType
MessageEnvelope
Bases: BaseModel
Envelope class for consumer operations (raw payload & de-serialized payload).
id: str
property
Implements the 'id' property.
Returns:
| Type | Description |
|---|---|
str
|
The ID of the message payload. |
MessagePayload
Bases: BaseModel
Class capturing a message payload basic properties.
SnapshotExecutionPayload
Bases: BaseModel
Payload class for snapshot async execution.