A reusable RabbitMQ pipeline handler for publishing and consuming messages.
Project description
MQPipeline
MQPipeline is a Python library for processing messages from RabbitMQ queues. It simplifies consuming, processing, and publishing messages with a custom handler, supporting durable queues and optional error handling. Configuration is managed via environment variables using Pydantic.
Features
- Connects to RabbitMQ with configurable exchanges, queues, and routing keys.
- Processes messages asynchronously with a user-defined handler.
- Supports an optional error queue for failed messages.
- Ensures graceful shutdown on SIGTERM/SIGINT.
- Includes a sample app and test setup with Podman Compose.
Installation
Prerequisites
- Python 3.12+
- RabbitMQ server (e.g., via Docker)
- Podman and Podman Compose (for testing)
- Tox
Setup
Clone the repository and install dependencies:
git clone <repository-url>
cd mqpipeline
tox -e dist
It produces a wheel file in the dist/ directory.
Usage
Configure Environment Variables
Set up a .env file in tests/ or export variables:
export MQ_HOST=rabbitmq
export MQ_USER=guest
export MQ_PASSWORD=guest
export MQ_VHOST= # empty for default vhost /
export MQ_APPLICATION=my_app
export MQ_PUBLISHER_EXCHANGE=pub_ex
export MQ_PUBLISHER_QUEUE=pub_queue
export MQ_PUBLISHER_ROUTING_KEY=pub_key
export MQ_SUBSCRIBER_EXCHANGE=sub_ex
export MQ_SUBSCRIBER_QUEUE=sub_queue
export MQ_SUBSCRIBER_ROUTING_KEY=sub_key
export MQ_HAS_ERROR_QUEUE=true
export MQ_ERROR_EXCHANGE=err_ex
export MQ_ERROR_QUEUE=err_queue
export MQ_ERROR_ROUTING_KEY=err_key
If MQ_HAS_ERROR_QUEUE is set to true, the error queue will be used for failed messages. Otherwise MQ_ERROR_EXCHANGE, MQ_ERROR_QUEUE, and MQ_ERROR_ROUTING_KEY are ignored.
Sample Application
Please refer to the sample app in tests/sample_app/app.py for an example of how to use the library. It demonstrates how to set up a message handler, configure RabbitMQ connections, and start consuming messages.
##Testing
For local testing, run:
./test.sh
This runs linting (pylint), security checks (bandit), builds the wheel, and starts the sample app with Podman Compose.
Send Test Messages
Use the RabbitMQ UI (http://localhost:15672) or curl:
curl -X POST http://guest:guest@localhost:15672/api/exchanges/%2F/sub_ex/publish \
-H "Content-Type: application/json" \
-d '{
"routing_key": "sub_key",
"payload": "{\"sleep_time\": 2, \"produce_error\": false}",
"payload_encoding": "string",
"properties": {"delivery_mode": 2}
}'
Documentation
To build documentation locally for ReadTheDocs:
tox -e docs
View the output in docs/_build/html/index.html. The documentation covers MQPipelineConfig, MQPipeline and Sample Application.
Contributing
Submit pull requests or issues to the repository.
License
MIT License. See LICENSE for details.
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 Distributions
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 mqpipeline-0.1.0-py3-none-any.whl.
File metadata
- Download URL: mqpipeline-0.1.0-py3-none-any.whl
- Upload date:
- Size: 13.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2dc73345e0d91f62737fcf473925d2d57c4fb324afd8de2b8e05ddc4c14a286d
|
|
| MD5 |
a4124b0f61a6fe41bba8c7dfea09611d
|
|
| BLAKE2b-256 |
2f92f7703bebda1586cf41da6c343a575562fab8d04237dd2efec803d88f4c04
|