I've a php application (laravel 5.8) that implements a rabbitmq consumer (bschmitt/laravel-amqp).
When the application starts, to keep under control the consumer process, a supervisor process starts as well .
My supervisor conf file:
[program:message-consume]
process_name=%(program_name)s_%(process_num)02d
command=php artisan message:consume
directory=/var/www
autostart=true
autorestart=true
numprocs=1
redirect_stderr=true
stdout_logfile=/var/www/storage/logs/supervisor.log
stopwaitsecs=3600
startretries=20
The consume command
public function consume($routingKey)
{
Log::info("[MessagingService][consume] start consuming key: " . $ROUTING_KEY . ".*");
Amqp::consume($QUEUE_NAME, function ($message, $resolver) {
# my code...
$resolver->acknowledge($message);
}, [
'routing' => $ROUTING_KEY . '.*',
'queue' => $QUEUE_NAME,
'exchange' => $EXCHANGE,
'exchange_type' => 'topic',
'exchange_durable' => false,
'queue_force_declare' => true,
'queue_exclusive' => false,
'persistent' => true, // set true if consume forever
]);
}
Everything seems to be alright but I realized that, if there are no messages for a while, the application stop receiving messages!
The processes is still running and supervisor doesn't do anything.
What's going on? What I can see from rabbit dashboard is that there are no consumers connected to the channel.
The idea was to create a cron script that every minutes call rabbitmq api to check if there are consumers in the queue and, in case, restart supervisor.
I'm working with docker so I changed my startup.sh in order to add the cron
#!/bin/bash
supervisord
crontab /etc/cron.d/check-consumer-cron
cron -f
php-fpm
But I start asking me questions. Am I doing well? Am I missing something? Do I need supervisor?
If I change the startup.sh in the following way, what changes?
#!/bin/bash
php artisan message:consume
crontab /etc/cron.d/check-consumer-cron
cron -f
php-fpm