Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP (Symfony 4) rabbitmq consumer command as daemon

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.

like image 773
el.luke Avatar asked Mar 31 '26 17:03

el.luke


1 Answers

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:

  1. 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.

  2. 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:

  • Handling beanstalk and RabbitMQ workers of symfony applications with supervisor in capistrano deployment process
  • Using supervisor within docker containers
  • Monitoring processes with Supervisor
like image 67
BentCoder Avatar answered Apr 02 '26 13:04

BentCoder