Message Bus
- class MessageBus(initial_message: Command | Event | ReturnMessage, *, log_level: Literal['debug', 'info', 'disabled'] = 'info', error_log_level: Literal['info', 'warning', 'error', 'disabled'] = 'error')[source]
This is a basic implementation of Message Bus. Create a subclass of this if you need to change logging and/or exception handling.
Usage:
results = MessageBus(my_message)()
If you’re doing CQRS and/or don’t use
ReturnMessageobjects.MessageBus(my_message)()
Alternatively, you can use
handle_message(). It has the same arguments and return values. (It won’t work if you have subclassedMessageBus).- handle_exception(exc: Exception, message: Command | Event | ReturnMessage) None[source]
Override this in a subclass to customize exception handling.
A note from the book to keep in mind in your code:
Note
“Since we don’t know who’s handling an event, senders should not care whether the receivers succeeded or failed.”
By default, exceptions raised will be logged and raised.
- log_activity(*args)[source]
Handles logging of actions to a standard Python logger. Override in a subclass for custom handling.
Top-level package for MessageBus.
- handle_message(message: Command | Event | ReturnMessage, *, log_level: Literal['debug', 'info', 'disabled'] = 'disabled', error_log_level: Literal['warning', 'error', 'disabled'] = 'disabled')[source]
Convenience function to process a message using
MessageBus.Usage:
return_messages = handle_message(my_message)
If you’re doing CQRS and/or don’t use
ReturnMessageobjects.handle_message(my_message)