Download the PHP package mhujer/rabbit-mq-consumer-handler-bundle without Composer
On this page you can find all versions of the php package mhujer/rabbit-mq-consumer-handler-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mhujer/rabbit-mq-consumer-handler-bundle
More information about mhujer/rabbit-mq-consumer-handler-bundle
Files in mhujer/rabbit-mq-consumer-handler-bundle
Package rabbit-mq-consumer-handler-bundle
Short Description Handle messages in RabbitMQ consumers in a safe and effective way
License MIT
Informations about the package rabbit-mq-consumer-handler-bundle
RabbitMQ Consumer Handler Bundle
This package is a fork of vasek-purchart/rabbit-mq-consumer-handler-bundle
maintained for newer PHP and Symfony versions.
Handle messages in RabbitMQ consumers in a safe and effective way
Note: This bundle expects you are using RabbitMqBundle
Message queue consumers require usually long running processes, which should process many different messages, before they are terminated. In an ideal scenario, they will be running indefinitely. But since we are not living in an ideal world, errors will inevitably occur. These can be typically:
- expected application exceptions,
- unexpected application exceptions,
- other exceptions and errors like connection interruptions etc.,
- memory leaks and other unexpected behavior.
The purpose of this bundle is to encapsulate handling of these states, automate the ones, which can be automated and provide comfortable ways to handle the remaining ones.
This bundle can automatically handle:
- stopping consumer on uncaught exceptions (so that it can be safely restarted)
- logging uncaught exceptions,
- clearing Doctrine EntityManager before processing a message,
- stopping consumer when Doctrine EntityManager is closed.
Usage
In order to receive all the benefits of automated handling you need only to run the message processing through the ConsumerHandler
, so standard consumer could look something like this:
This bundle will create ConsumerHandler
instance for every one of your consumers, because it needs to access specific instance of OldSound\RabbitMqBundle\RabbitMq\DequeuerInterface
, which it needs to control consuming messages from the configured queue.
Assuming your consumer is called example
in the old_sound_rabbit_mq
configuration, vasek_purchart.rabbit_mq_consumer_handler.consumer_handler.id.example
service will be prepared, so you can just pass this instance to your consumer:
Restarting consumers
With consumers you will generaly need something to keep them running in case they fail. This is achieved usually with some kind of daemon (for example supervisord
), which will run the consumers for you, watch them if they are running and start them again if not (based on your configuration).
In order for the tool to be able to restart the consumer reliably, it first needs to be able to tell, whether the consumer has even started, so that it would just not get stuck in booting cycle. The usual configuration is to not start the program again after several retries when it cannot start properly. In suprvisord
the program is considered to be started after startsecs
number of seconds.
This bundle is making sure, that always, when the consumer is shutting down due to an uncaught exception or error, the consumer has been running at least this amount of time by sleeping for stop_consumer_sleep_seconds
, which should be configured to the same value as startsecs
. This means that the behavior can be handled correctly by supervisord
- restarting the consumer when it fails due to processing a message, but not restarting it indefinitely if the application cannot even start.
Handling exceptions
In the example below, there are some examples of the most common situations you will encounter:
Configuration
Configuration structure with listed default values:
Installation
Install package mhujer/rabbit-mq-consumer-handler-bundle
with Composer:
Register the bundle in your application kernel:
All versions of rabbit-mq-consumer-handler-bundle with dependencies
consistence-community/consistence Version ~2.1
doctrine/orm Version ~2.6
php-amqplib/rabbitmq-bundle Version ~2.2
psr/log Version ~1.0
symfony/config Version ~4.4|^5.4|^6.0
symfony/dependency-injection Version ~4.4|^5.4|^6.0
symfony/http-kernel Version ~4.4|^5.4|^6.0
symfony/yaml Version ~4.4|^5.4|^6.0