The decorator organizes the basic error handling of the message queuing protocol and the message parsing into a separate layer from the callback function.
The message parsing uses Pydantic.
Derive from the abstract decorator implementing the abstract methods:
>>> from callback_decorator import Callback
>>>
>>>
>>> class MyCallback(Callback):
... @staticmethod
... def reject_message(channel, method_frame, header_frame, body):
... channel.basic_reject(method_frame.delivery_tag)
... @staticmethod
... def resend_message_later(channel, method_frame, header_frame, body):
... pass
... @staticmethod
... def acknowledge_message(channel, method_frame, header_frame, body):
... channel.basic_ack(method_frame.delivery_tag)
Create your custom data model:
>>> from pydantic import BaseModel
>>>
>>>
>>> class MyModel(BaseModel):
... my_field: int
Decorate your callback function:
>>> @MyCallback(MyModel)
... def do_my_callback(my_object, headers):
... ...
On error, raise one of the following exceptions:
Callback.FatalError
, if the message can never be processed;Callback.TryAgainError
, if it is worth trying to process the message later.