PHP Rabbitmq how to check if consumer is running?
According Rabbitmq documentation I wrote a script for consuming queue. But its pretty basic example. The script will run as cron because I need to be sure it will recover consuming process if previous consumer fails. Script should check if there is some other consumer already running and if there is another consumer, then return and does not start new consumer.
The only way I found is to check result of queue_declare() call which returns array with numbers. The script looks like:
ini_set('max_execution_time', 0);
$queue_info = $this->channel->queue_declare($queue_name, false, true, false, false);
// This is the problem. Is there a better solution?
if( $queue_info[2] > 0 ) {
Debugger::log('There is another consumer already running.');
$this->connection->close();
$this->channel->close();
return;
}
$callback = function (AMQPMessage $msg) use ($data) {
Debugger::log($msg->getBody());
};
$this->channel->basic_consume($queue_name, '', false, true, false, false, $callback);
while ($this->channel->is_consuming()) {
$this->channel->wait();
}
$this->connection->close();
$this->channel->close();
So the question is how can I check if another consumer is already running?
Read more here: Source link
