Skip to content

Alex RabbitMQ broker is a wrapper with a few out-of-the-box features. Use at will. Just, please, mention the author ;)

License

Notifications You must be signed in to change notification settings

diPaoli/alex-rabbitMQ-broker

Repository files navigation

RabbitMQ Handler for Python - by Alex diPaoli

Alex RabbitMQ broker is a wrapper with a few out-of-the-box features. Use at will. Just, please, mention the author using the gitHub user link: https://github.com/diPaoli

;)

Instalation

Add this requirement to your project:

alex-rabbitMQ-broker @ git+https://<user+password or token>@github.com/<your_organization>/alex-rabbitMQ-broker.git

Then use pip install -r requirements.txt to install all requirements.


Basic Usage

To use, simply create a Consumer and call the Consumer.consume() method. You must create a method to handle received messages, like this:

Example:

from rabbit_broker.consumer import RabbitConsumer

def handle_message(body):
    print(body)

consumer = RabbitConsumer(queue="basic_queue", task=handle_message)
consumer.consume()

You can publish messages into this queue using the Publisher class, like this:

from rabbit_broker.publisher import RabbitPublisher


publisher = RabbitPublisher(queue="basic_queue", message="Test message using Erik!")
publisher.publish()
print('Message published!')


Retry Usage

To use the Retry workflow you must:

  • Declare use_retry=True to Enable retry behavior.
  • Declare auto_acknowledge=False to allow RetryPublisher class to handle the acknowledgements.
  • In this mode, the handle_message method must receive an instance of consumer.BasicMessage, which will carry content and handlers of the rabbit message.

Example:

from rabbit_broker.consumer import BasicMessage, RabbitConsumer
from rabbit_broker.retry_publisher import RetryPublisher


def handle_message(message: BasicMessage):
    print("MESSAGE RECEIVED: ", message.body)
    print(f"RETRY_COUNT: {message.retry_count}")

    if <condition to retry>:
        RetryPublisher(consumer, message)
    else: message.channel.basic_nack(delivery_tag=message.method.delivery_tag,
        requeue=False)


consumer = RabbitConsumer(
    queue="retry_example_queue",
    task=handle_message,
    use_retry = True,
    auto_acknowledge=False
    )
consumer.consume()

  • The RetryPublisher works by publishing a new message to the same queue on each retry, then it nacks (consumer.basic_nack) for the actual message, which will be descarded. When the retry_count is exceded, the message will be published in the dead_letter_queue declared on the consumer call, and also nacks the actual message. Therefore, every retry is simply a new message with retry_count + 1.
  • As you see in this example, you can handle basic_ack or basic_nack whenever you see fit, by using the channel passed in the message object. However, keep in mind the RetryPublisher already nacks all messages, assuming that if they are being retryed, the process reach some failure. If you ever need an to confirm the acknowledge, you must call it manually (except on the basic mode, when the auto_acknowledge is True by defaut).
  • Extra parameters can be customized using kwargs. See the Consumer and Publisher classes docstrings for details.


Retry Workflow Chart



MIT License

Copyright (c) 2024 Alexandre di Paoli

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Alex RabbitMQ broker is a wrapper with a few out-of-the-box features. Use at will. Just, please, mention the author ;)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages