Event sourcing library for RabbitMQ (aio_pika)
Project description
RabbitMQ Module
A module for working with RabbitMQ in asynchronous mode. Provides convenient decorators for creating consumers, producers, and functions.
Main Components
1. Factory Function (__init__.py)
build_event_dependencies()- creates instances of decorators and event handler
2. Decorators (decorators.py)
EventDecorators- class with decorators for working with RabbitMQ@consumer- decorator for creating message consumers@producer- decorator for creating message producers@function- decorator for functions with message sending
3. Event Handler (queue_runtime.py)
EventHandler- main class for message processingconsume()- main message consumption loopprocess_message()- processing incoming messageshandle_single_message()- processing a single message from queuehandle_reply_data()- sending responses for RPC
4. Connection Context (context.py)
ContextConnectionEvents- managing connection context and handlers
5. Interfaces (interface.py)
IEventDecorators- interface for decoratorsIContextConnectionEvent- interface for connection contextIRabbitQueue- interface for queue
6. Data Models (model.py)
QueueDetail- queue detailsQueueEventType- event types (CONSUME)QueueResponse- response model
7. Common Components (common.py)
ConsumeChannel- consumption channel configurationQueueData- message data model
8. Connection Management (connection.py)
get_connection_rabbit()- factory for creating connections- Managing connection lifecycle
9. Queue (queue.py)
RabbitQueue- class for working with RabbitMQ queue
Main Features
- Asynchronous operation - all operations are performed asynchronously
- Automatic reconnection - automatically reconnects on connection errors
- Easy to use - convenient decorators for all types of operations
- Convenient decorators - simple creation of message handlers
- Logging - built-in logging of all operations
- Error handling - automatic error handling and logging
- Connection management - automatic management of connection lifecycle
- Message retry system - automatic retry mechanism with configurable delays and limits
Decorator Types
- Consumer - simple message consumer
- Producer - message producer
- Function - function with automatic result sending
Message Retry System
The module includes a sophisticated message retry system that automatically handles failed message processing with configurable delays and retry limits.
Retry Configuration
from app.domain.modules.rabbit import build_event_dependencies
from app.domain.modules.rabbit.common import RetryConfig
# Configure retry settings
retry_config = RetryConfig(
max_retries=3, # Maximum number of retries (-1 for infinite)
retry_delay_seconds=300 # Delay between retries in seconds (5 minutes)
)
events, handle = build_event_dependencies(
url="amqp://user:password@localhost:5672/",
idle_timeout=300,
retry_config=retry_config
)
How Retry System Works
- Message Processing: When a message fails to process, the system automatically detects the error
- Retry Count Tracking: The system tracks retry attempts using message headers (
x-retry-count) - Delay Implementation: Uses Dead Letter Exchange pattern with TTL for delayed retries
- Automatic Retry: Messages are automatically retried after the configured delay
- Final Rejection: After max retries are exceeded, messages are permanently rejected
Retry Flow
Message Processing Failed
↓
Check Retry Count < Max Retries
↓
Increment Retry Count in Headers
↓
Create Delay Queue with TTL
↓
Send Message to Delay Queue
↓
Message Returns to Main Queue After Delay
↓
Process Message Again
Configuration Options
- max_retries: Maximum number of retry attempts (-1 for infinite retries)
- retry_delay_seconds: Time to wait between retry attempts in seconds
Example Usage
@events.consumer(action="PROCESS_EMAIL")
async def process_email(user_id: str, email_data: dict):
try:
# Process email logic
result = await send_email(email_data)
return {"status": "success", "result": result}
except Exception as e:
# This will trigger automatic retry with delay
raise e # Let the retry system handle it
Configuration
The module requires URL configuration for connecting to RabbitMQ:
from app.domain.modules.rabbit import build_event_dependencies
events, handle = build_event_dependencies(
url="amqp://user:password@localhost:5672/",
idle_timeout=300 # timeout before closing connection
)
Security
- SSL connection support
- Automatic connection management
- Network error handling
- Persistent messages (delivery_mode=DeliveryMode.PERSISTENT)
- Automatic connection closing with timeout
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file eventrabbit-0.1.0.tar.gz.
File metadata
- Download URL: eventrabbit-0.1.0.tar.gz
- Upload date:
- Size: 10.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.5 CPython/3.8.10 Darwin/23.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
132aecca83b1399d1341d255320d0d27e319bcb41b9143f00926149b59ea5b60
|
|
| MD5 |
5de42939be454a3573d9995490c673ac
|
|
| BLAKE2b-256 |
a8a0d1a7b358d530d860904b04334e725cb2212e49e02dbfed93f7bba34d91fd
|
File details
Details for the file eventrabbit-0.1.0-py3-none-any.whl.
File metadata
- Download URL: eventrabbit-0.1.0-py3-none-any.whl
- Upload date:
- Size: 12.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.5 CPython/3.8.10 Darwin/23.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
986dbb678d64895aa26362c06ce1efa43261a04cea29b00b4d0691a3319d9e87
|
|
| MD5 |
3e4560b8d8d658e22d935a846a2612fd
|
|
| BLAKE2b-256 |
6379acdc629a30c1073da17d4d92746a0a968ff7e4862c8aa38c2cb46c31b95e
|