Skip to main content

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 processing
  • consume() - main message consumption loop
  • process_message() - processing incoming messages
  • handle_single_message() - processing a single message from queue
  • handle_reply_data() - sending responses for RPC

4. Connection Context (context.py)

  • ContextConnectionEvents - managing connection context and handlers

5. Interfaces (interface.py)

  • IEventDecorators - interface for decorators
  • IContextConnectionEvent - interface for connection context
  • IRabbitQueue - interface for queue

6. Data Models (model.py)

  • QueueDetail - queue details
  • QueueEventType - event types (CONSUME)
  • QueueResponse - response model

7. Common Components (common.py)

  • ConsumeChannel - consumption channel configuration
  • QueueData - 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

  1. Consumer - simple message consumer
  2. Producer - message producer
  3. 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

  1. Message Processing: When a message fails to process, the system automatically detects the error
  2. Retry Count Tracking: The system tracks retry attempts using message headers (x-retry-count)
  3. Delay Implementation: Uses Dead Letter Exchange pattern with TTL for delayed retries
  4. Automatic Retry: Messages are automatically retried after the configured delay
  5. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

eventrabbit-0.1.0.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

eventrabbit-0.1.0-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

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

Hashes for eventrabbit-0.1.0.tar.gz
Algorithm Hash digest
SHA256 132aecca83b1399d1341d255320d0d27e319bcb41b9143f00926149b59ea5b60
MD5 5de42939be454a3573d9995490c673ac
BLAKE2b-256 a8a0d1a7b358d530d860904b04334e725cb2212e49e02dbfed93f7bba34d91fd

See more details on using hashes here.

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

Hashes for eventrabbit-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 986dbb678d64895aa26362c06ce1efa43261a04cea29b00b4d0691a3319d9e87
MD5 3e4560b8d8d658e22d935a846a2612fd
BLAKE2b-256 6379acdc629a30c1073da17d4d92746a0a968ff7e4862c8aa38c2cb46c31b95e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page