Metadata-Version: 2.1
Name: aio-bunny
Version: 0.0.2
Summary: Asyncio compatible wraper around the aio-pika library for consumining messages.
Author: Mehdi Kamani <mkamani@sheypoor.com>
Author-email: 
License: MIT License
Project-URL: Documentation, https://aio-bunny.readthedocs.org/
Project-URL: Source, https://github.com/itsmehdi97/aio-bunny
Platform: all
Classifier: License :: OSI Approved :: MIT License
Classifier: Topic :: Internet
Classifier: Topic :: Software Development
Classifier: Topic :: Software Development :: Libraries
Classifier: Intended Audience :: Developers
Classifier: Natural Language :: English
Classifier: Operating System :: MacOS
Classifier: Operating System :: POSIX
Classifier: Operating System :: Microsoft
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: Implementation :: PyPy
Classifier: Programming Language :: Python :: Implementation :: CPython
Requires-Python: >=3.10, <4
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: aio-pika<10,>=6.8.0
Provides-Extra: develop
Requires-Dist: aio-pika; extra == "develop"
Requires-Dist: async_generator; extra == "develop"
Requires-Dist: coverage!=4.3; extra == "develop"
Requires-Dist: coveralls; extra == "develop"
Requires-Dist: pylama; extra == "develop"
Requires-Dist: pytest; extra == "develop"
Requires-Dist: pytest-asyncio; extra == "develop"
Requires-Dist: pytest-cov; extra == "develop"
Requires-Dist: shortuuid; extra == "develop"
Requires-Dist: nox; extra == "develop"
Requires-Dist: sphinx; extra == "develop"
Requires-Dist: sphinx-autobuild; extra == "develop"
Requires-Dist: timeout-decorator; extra == "develop"
Requires-Dist: tox>=2.4; extra == "develop"

## aio-message-handler

An asyncio compatible wrapper around aio-pika that brings the follwoing advantages:
- [x] **Simplified API**: Abstracts the complexity of defining queues and exchanges, making it easy to set up consumer/producers.
- [x] **Graceful Consumer Shutdown**: Provides an API to stop consumers without disrupting unacknowledged tasks, ensuring a smooth and safe shutdown process.
- [ ] **Idempotent Consumers**: Offers a straightforward API for integrating with external databases (e.g., Redis, MySQL) to ensure idempotency. As recommended in the [RabbitMQ documentation](https://www.rabbitmq.com/docs/confirms#automatic-requeueing), consumers configured with manual acknowledgment and concerned about avoiding duplicate message processing should implement idempotency.
- [ ] **Prometheus Integration**: Integrates with Prometheus to collect useful metrics for monitoring the state of consumers and producers. You can easily choose whether to enable metric collection based on your needs.


### Installation
```
pip install aio-bunny
```
### Usage examples
In the simplest form, you can do the following to spin up a consumer:
``` python
import asyncio

from aio_bunny import Bunny


async def main():
    bunny = Bunny("amqp://guest:guest@127.0.0.1/")

    @bunny.consumer(
        queue="myqueue",
        exchange="myexchange",
        routing_key="key")
    async def on_message(msg: bytes):
        await asyncio.sleep(1)
        print('received:', msg.decode())


    await bunny.start()


if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.create_task(main())
    loop.run_forever()
```
\
To shutdown the consumers gracefully, all you need to do is to call the `stop` method on the `Bunny` instance. \
This method will stop the consumer from accepting new messages from the broker, waits for all inflight tasks to finish and exits cleanly.\

So In the following example, we want to stop the application whenever `SIGINT` OR `SIGTERM` is received:
``` python
import asyncio
import signal

from aio_bunny import Bunny


async def main():
    bunny = Bunny("amqp://guest:guest@127.0.0.1/")

    async def shutdown(signal, loop):
        print(f"Received exit signal: {signal.name}")
        await bunny.stop()  # Stopping the consumers here
        loop.stop()

    loop = asyncio.get_event_loop()
    # Call the shutdown funciton on any of these signals.
    signals = (signal.SIGTERM, signal.SIGINT)
    for s in signals:
        loop.add_signal_handler(s, lambda s=s: asyncio.create_task(shutdown(s, loop)))

    @bunny.consumer(
        queue="myqueue",
        exchange="myexchange",
        routing_key="key")
    async def on_message(msg: bytes):
        await asyncio.sleep(1)
        print('received:', msg.decode())


    await bunny.start()


if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.create_task(main())
    loop.run_forever()
```
## Versioning
This software follows [Semantic Versioning](https://semver.org/)
### Development

#### Setting up development environment
Clone the project:
```
git clone repo_url
cd aio-bunny
```
Create a virtualenv for `aio-bunny`:
```
python3 -m venv venv
source venv/bin/activate
```
Install the requirements:
```
pip install -e '.[develop]'
```
