Domanda

I'm using node-amqp as a queueing system in a node application. I'd like to be able to monitor the state of the queue to figure out if we have enough workers runnning, i.e. if the queue size is increasing we know we are starting to fall behind.

I know that from the command line you can use something like;

rabbitmqctl list_queues

Which gives me the exact information I need, but I was wondering if there is anyway to do this from node-amqp itself?

Thanks in advance.

EDIT

In the end, I just used the command line tool rabbitmqctl to get the information I need, its not a great solution but here is what I did;

var Logger = require('arsenic-logger');

getQueueMeta(function(info){
    Logger.info(info);
});

/**
* Returns a sparse array with the queue names as the indices
* and the number of messages as the value, e.g.;
*
* info = [ my-queue: 9, my-other-queue: 1 ]
* 
* @param callback
*/
function getQueueMeta(callback){

    var sys = require('sys')
    var exec = require('child_process').exec;

    exec("/usr/local/sbin/rabbitmqctl list_queues", function(error, stdout, stderr) {

        var info = [];

        if (!error){

            var lines = stdout.split(/\n/);

            if (lines.length > 1){

                for (var i=1; i<lines.length-2; i++){
                    var temp = lines[i].split(/\s/);
                    info[temp[0].trim()] = parseInt(temp[1]);
                }

            }

        }

        callback(info);

    });

}
È stato utile?

Soluzione

I may be a bit late for the party, but in case anyone in the future stumbles across this problem... In current version of node-amqp (0.2.4) you can check both message count and number of currently subscribed consumers when declaring a queue.

var connection = require("amqp").createConnection();
connection.exchange("exampleExchange", {/*...*/}, function(exchange) {
  connection.queue("exampleQueue", {/*...*/}, function(queue, messageCount, consumerCount){
    console.log("Message count", messageCount);
    console.log("Consumer count", consumerCount);
  });
});

Altri suggerimenti

So RabbitMQ supports AMQP up to 0.9.1. You can see the docs directly here.

A quick scan through the docs will reveal that AMQP only covers things like connections, exchanges and basic queuing / dequeuing. However many feature in rabbitmqctl are simply not accessible via AMQP spec. In that way rabbitmqctl is much closer to a "management" tool where the AMQP connection is basically a "consumer" tool.

That stated, you may be in luck. Take a look at the connection.queue() method.

var q = connection.queue('my-queue', function (queue) {
  console.log('Queue ' + queue.name + ' is open');
});

In the .NET implementation it looks like that queue variable includes not just the name, but also the Count of Consumers and Messages. I'm not clear what's available in the node.js, you may have to dig in.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top