How to reuse connections in node -resque?

I use the API node-resqueas described in the documentation. And every time I execute the queue, a new client connection is created, confirmed with CLIENT LIST. Why is it impossible to do only one connection?

// Initialize in the beginning and making queue globally accessible
var queue = new NR.queue({connection: connectionDetails}, jobs);
queue.on('error', function(error){ console.log(error); });

// calling this multiple times
queue.connect(function(){
  queue.enqueue('math', "add", [1,2]);
  queue.enqueue('math', "add", [1,2]);
  queue.enqueue('math', "add", [2,3]);
  queue.enqueueIn(3000, 'math', "subtract", [2,1]);
});

Am I abusing the API or is it a bug in the API? You expect connectit to close the connection again at the end of the block .

+4
source share
1 answer

"connect" , . , . , , , . Redis.

var connected = false;
queue.connect(function() {
    connected = true;
});

function addJobToQueue(job) {
    if (connected) {
        queue.enqueue(job[0], job[1], job[2]);
    } else {
        setTimeout(addJobToQueue.bind(null, job), 100);
    }
}

addJobToQueue(['math', "add", [1, 2]]);
addJobToQueue(['math', "add", [2, 2]]);
addJobToQueue(['math', "add", [3, 2]]);
//..etc
+1

Source: https://habr.com/ru/post/1609473/


All Articles