Skip to main content

A simple, robust RabbitMQ manager for Python applications

Project description

RabbitMQ Easy

PyPI version Python Support License: MIT

A simple, robust RabbitMQ manager for Python applications with built-in connection management, retry logic, and dead letter queue support.

🚀 Features

  • 🔄 Automatic Connection Management with retry logic
  • 🛡️ Dead Letter Queue Support with automatic setup
  • 🔧 Environment Variable Support for easy configuration
  • 📦 Context Manager Support for proper cleanup
  • ⚡ Production Ready with comprehensive error handling

📦 Installation

pip install rabbitmq-easy

🏃‍♂️ Quick Start

from rabbitmq_easy import RabbitMQManager

# Simple setup
manager = RabbitMQManager(
    host='localhost',
    queues=['orders', 'payments'],
    routing_keys=['orders.*', 'payments.*'],
    exchange='my_exchange'
)

# Publish a message
manager.publish_message('my_exchange', 'orders.new', '{"order_id": 123}')

# Use as context manager
with RabbitMQManager() as manager:
    manager.publish_message('my_exchange', 'orders.new', '{"order_id": 123}')

🔧 Environment Variables

# .env file
RABBITMQ_HOST=localhost
RABBITMQ_USERNAME=guest
RABBITMQ_PASSWORD=guest
RABBITMQ_EXCHANGE=my_exchange
RABBITMQ_QUEUES=orders,payments
RABBITMQ_ROUTING_KEYS=orders.*,payments.*
from rabbitmq_easy import create_rabbitmq_manager

# Auto-loads from environment
manager = create_rabbitmq_manager()

📖 Usage Examples

Consumer

import json

def process_message(ch, method, properties, body):
    try:
        data = json.loads(body)
        print(f"Processing: {data}")
        ch.basic_ack(delivery_tag=method.delivery_tag)
    except Exception as e:
        print(f"Error: {e}")
        ch.basic_nack(delivery_tag=method.delivery_tag, requeue=False)

manager.start_consuming('orders', process_message)

Dead Letter Handling

def handle_failed_messages(ch, method, properties, body):
    headers = properties.headers or {}
    death_info = headers.get('x-death', [])
    
    if death_info:
        print(f"Message failed {death_info[0].get('count')} times")
    
    # Reprocess or log
    ch.basic_ack(delivery_tag=method.delivery_tag)

manager.start_consuming('failed_messages', handle_failed_messages)

⚙️ Configuration

Parameter Environment Variable Default
host RABBITMQ_HOST localhost
username RABBITMQ_USERNAME guest
password RABBITMQ_PASSWORD guest
queues RABBITMQ_QUEUES []
routing_keys RABBITMQ_ROUTING_KEYS []
exchange RABBITMQ_EXCHANGE ''

🏗️ What Gets Created

When you initialize:

manager = RabbitMQManager(
    exchange='orders',
    queues=['new_orders', 'pending_orders'],
    routing_keys=['orders.new', 'orders.pending']
)

Automatically creates:

  1. orders exchange
  2. orders_dlx exchange (dead letter)
  3. new_orders queue → bound to orders
  4. pending_orders queue → bound to orders
  5. failed_messages queue → bound to orders_dlx

🛠️ Resource Management

# Queue operations
manager.delete_queue("old_queue")
manager.purge_queue("queue_name")
info = manager.get_queue_info('orders')

# Health check
health = manager.health_check()

# Cleanup
manager.delete_all_setup_resources(confirm=True)

❌ Error Handling

from rabbitmq_easy import RabbitMQConnectionError, RabbitMQConfigurationError

try:
    manager = RabbitMQManager(
        queues=['queue1', 'queue2'],
        routing_keys=['key1']  # Mismatch - will raise error
    )
except RabbitMQConfigurationError as e:
    print(f"Configuration error: {e}")

🚀 Production Example

# docker-compose.yml
version: '3.8'
services:
  app:
    build: .
    env_file: .env
    depends_on: [rabbitmq]
  
  rabbitmq:
    image: rabbitmq:3-management
    env_file: .env

📋 Best Practices

  • Use environment variables for credentials
  • Always configure dead letter queues for production
  • Use context managers for automatic cleanup
  • Monitor queue lengths and consumer lag

🤝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for changes
  4. Submit a Pull Request

📝 License & Support


Made with ❤️ for the Python community

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

rabbitmq_easy-1.0.1.tar.gz (18.6 kB view details)

Uploaded Source

Built Distribution

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

rabbitmq_easy-1.0.1-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

Details for the file rabbitmq_easy-1.0.1.tar.gz.

File metadata

  • Download URL: rabbitmq_easy-1.0.1.tar.gz
  • Upload date:
  • Size: 18.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for rabbitmq_easy-1.0.1.tar.gz
Algorithm Hash digest
SHA256 9bfbe7126f5ce344c65cb7cc0d63230c64675c8bf28f460e4bef85bfbfb38eaf
MD5 40b1ba9d78fbf6826afea34943e5b861
BLAKE2b-256 db4f1f4abaf226b74f2499437332d71ec2f741bb1d2ceb4d8a22b176067c10ee

See more details on using hashes here.

File details

Details for the file rabbitmq_easy-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: rabbitmq_easy-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 15.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for rabbitmq_easy-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6ec9d6a55b61233213e9a7b711ebc89f2a4b5799ae538702a7c30b03a24aafa8
MD5 a8b235eb7b093e4648a220f41ec17791
BLAKE2b-256 070690b8f1580479570d6b709042ad124cb3ae42b3656ce0f92913b13a2de683

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