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.2.tar.gz (18.7 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.2-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: rabbitmq_easy-1.0.2.tar.gz
  • Upload date:
  • Size: 18.7 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.2.tar.gz
Algorithm Hash digest
SHA256 c7e59ac607def901047166ed65903434f3d0b7858bc5bb6e7aa06a2e71fcf3a1
MD5 809be02c859bfe83292a0180731625ac
BLAKE2b-256 88029bd99daafe99f9a58f339fd45a2510b21432e50a78cab62ba7be4d680f04

See more details on using hashes here.

File details

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

File metadata

  • Download URL: rabbitmq_easy-1.0.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4eda6c26fa0357a7f72db1b4006104a4322b4031e6183abba952f2359123483b
MD5 d5ff80bd342c036416e8442f324307e4
BLAKE2b-256 230bce0353a040d5963d7417f106d29ef4a91db3ae84b26e42529824f976faaf

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