I have RabbitMQ producer and consumer written in PHP (Symfony 4). Consumer is working as custom symfony 4 command along with bundle php-amqplib/rabbitmq-bundle
Here is issue. I want to be able to set consumer to listen and consume tasks instantly after they appear in queue.
I tried to run it as one-shot systemd service but it's not working wery well.
Systemd:
[Unit]
Description=consumer for rabbitmq
[Service]
Type=oneshot
ExecStart=/bin/sh /var/www/public/rabbit.sh
ExecStop=/usr/bin/pkill -f "rabbitmq:consumer"
RemainAfterExit=yes
StandardOutput=journal
[Install]
WantedBy=multi-user.target
If there is better solution than writing daemon please let me know. I just want to be able to set consumer to listen and consume task instantly after it apear in queue.
The solution to your "issue" is simple. All you are going to do is, make use of supervisord which will watch your command behind the scene and bring it back up if it goes down.
PHP is not good at long running processes so you want to keep your consumer/worker to consume reasonably enough (not many) messages. e.g. 100 to 200 is good enough.
This is what you are going to do:
Create a supervisor config file for your command - check example below. If you wish go to the doc and read what exactly the properties below do.
Enable this config within the supervisor.
That's all!
[program:name-of-your-command]
command=php bin/console rabbitmq:consumer -m 100 your_queue --env=prod -DFOREGROUND # Your consumer command
directory=/path/to/your/app
autostart=true
autorestart=true
startretries=5
startsecs=0
user=deployer # Your user
numprocs=1 # This tells supervisor to run only one consumer
process_name=%(program_name)s_%(process_num)02d
stderr_logfile=/path/to/your/app/var/logs/%(program_name)s_stderr.log
stderr_logfile_maxbytes=10MB
stdout_logfile=/path/to/your/app/var/logs/%(program_name)s_stdout.log
stdout_logfile_maxbytes=10MB
Examples:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With