A simple, robust RabbitMQ manager for Python applications
Project description
RabbitMQ Easy
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:
ordersexchangeorders_dlxexchange (dead letter)new_ordersqueue → bound toorderspending_ordersqueue → bound toordersfailed_messagesqueue → bound toorders_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
- Fork the repository
- Create a feature branch
- Add tests for changes
- Submit a Pull Request
📝 License & Support
- License: MIT - see LICENSE
- Issues: GitHub Issues
- Docs: GitHub README
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)
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 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9bfbe7126f5ce344c65cb7cc0d63230c64675c8bf28f460e4bef85bfbfb38eaf
|
|
| MD5 |
40b1ba9d78fbf6826afea34943e5b861
|
|
| BLAKE2b-256 |
db4f1f4abaf226b74f2499437332d71ec2f741bb1d2ceb4d8a22b176067c10ee
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6ec9d6a55b61233213e9a7b711ebc89f2a4b5799ae538702a7c30b03a24aafa8
|
|
| MD5 |
a8b235eb7b093e4648a220f41ec17791
|
|
| BLAKE2b-256 |
070690b8f1580479570d6b709042ad124cb3ae42b3656ce0f92913b13a2de683
|