Wait for asynchronous task in NodeJS while consuming RabbitMQ Queue

Viewed 2349

Using amqp-lib in node.js (7) I am having trouble waiting for asynchronous tasks while consuming from a RabbitMQ queue.

For example,

    channel.consume(exchange_name, doWork, {
        noAck: false
    });

    function doWork(msg) {
        log.info(JSON.parse(msg.content.toString()));
        setTimeout(function () {
            log.info("   Done");
            channel.ack(msg);
        }, 2000);
    }

This should, AFAIK retrieve a message from the queue, process it, then acknowledge the process is complete & successful.

However, when I run it vs a queue with 1000 messages, it will grab all 1000 messages & start the process. Then 2 seconds later, acknowledge everything is done.

Is it possible to wait for the process to finish & then get the next one? (a worker should not consume 10's of thousands of messages while it is waiting on another process).

Thanks in advance

0 Answers
Related