Skip to content
Advertisement

PHP RabbitMQ consumer stops consuming events

I have a php daemon, which uses php-amqplib, that consumes messages from RabbitMQ server. Here is a gist of it (though it’s a bit more complex than that):

$callback = function (AMQPMessage $msg) {
    echo "Handling event: some event name here";
    try {
      //some custom logic here
    } catch (Throwable $e) {
      //write error in a log here
    }
    
    $msg->ack();
};

$channel = $this->connection->channel();
$channel->basic_qos(null, 1, null);

$channel->basic_consume($queue, '', false, false, false, false, $callback);

$this->wait($channel);

while ($channel->is_open()) {
   $channel->wait();
}

$channel->close();
$this->connection->close();

When I run it in the background, it handles the events, writes output and errors to various logs and I can see in RabbitMQ control panel that the queue has consumers. Then after quite some time it just stops doing that: there are no messages appearing in both error and output logs, the RabbitMQ control panel shows that the queue has 0 consumers, but the process is still running somehow.

Advertisement

Answer

If the process continue running, but doesn’t receive events from rabbitMQ, this may be caused by lost connection between consumer and rabbit server. Maybe the connection was closed by server. By default, if rabbitMQ server does not receive ack signal from consumer in some meaningful timeout (depends on configuration), it thinks that consumer is dead, closes the connection and re-queues the message. I.e. if message processing takes a long time and your consumer does not send ack within this timeout, connection is lost. To fix this you could send so-called “heartbeats” to server, to say like “hey, I’m alive, just give me more time to process a message”. RabbitMq client should do it automatically, but there is some bug in php implementation. See this article for details: https://blog.mollie.com/keeping-rabbitmq-connections-alive-in-php-b11cb657d5fb

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement