I want to log working status from workers' callbacks and include a number of messages in the queue left.
The only solution I found so far is getting the second member of queue_declare
result array, but this should be called once per worker launch, and I need info to be updated every new message.
UPD: Solution based on IMSoP's answer:
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();
$channel->queue_declare('test1');
echo "[*] Waiting for messages. To exit press CTRL+C\n";
$callback = function ($msg) use ($channel) {
list (, $cn) = $channel->queue_declare('test1', true);
echo ' [x] Received ', $msg->body, " $cn left";
for ($i = 0; $i < $msg->body; ++$i) {
sleep(1);
echo '.';
}
echo "\n";
};
$channel->basic_qos(null, 1, null);
$channel->basic_consume('test1', '', false, true, false, false, $callback);
while (count($channel->callbacks)) {
$channel->wait();
}
For some reason always gives 0 as message count.
The queue_declare
method has a parameter called "passive" which can be used for this purpose: it checks if the queue exists, by name only, and ignores any other parameters.
According to the AMQP documentation:
If set, the server will reply with Declare-Ok if the queue already exists with the same name, and raise an error if not. The client can use this to check whether a queue exists without modifying the server state. When set, all other method fields except name and no-wait are ignored. A declare with both passive and no-wait has no effect. Arguments are compared for semantic equivalence.
Note that Declare-Ok
is not just a status, but the name of the full response structure, with fields queue
, message-count
, and consumer-count
.
In PHP-AMQPLib, you can use this to log the status of a set of queues something like this:
foreach ( $this->registeredQueues as $queueName ) {
// The second parameter to queue_declare is $passive
// When set to true, everything else is ignored, so need not be passed
list($queueName, $messageCount, $consumerCount)
= $this->rabbitChannel->queue_declare($queueName, true);
$this->logger->info(
"Queue $queueName has $messageCount messages and $consumerCount active consumers."
);
}