Implement handle_failed/3
Learn to handle failed messages through the handle_message callback function.
The handle_failed/3
callback function
Right now, when we return a message from handle_message/3
, Broadway
automatically acknowledges it since it reaches the end of the pipeline.
However, if an unhandled exception happens in our business logic, the message is marked as failed. We can also manually mark a message as failed using Broadway.Message.failed/2
. This could be useful when we want to discard messages we don’t want to process.
Remember that in @producer_config
we configure the :on_failure
setting with the :reject_and_requeue
value. This means that failed messages will be sent back to RabbitMQ and redelivered. By default, messages will be redelivered indefinitely. We can change this setting to :reject_and_requeue_once
or simply :reject
to completely discard all failed messages.
We can also configure acknowledgments on a per-message basis, and the ideal place for that is handle_failed/3
. The optional handle_failed/3
callback allows us to check which messages have failed and why. We can then decide if we want to take further action.
Update tickets_available?
Let’s update the tickets_available?/1
function and pattern match for cinema events. We return false
to all of them for being unavailable:
Get hands-on with 1400+ tech skills courses.