Rudra
Rudra

Reputation: 1688

Kue: event to tell once the queue is empty

I am using kue in one of my project to remove stale locks from the database. I am able to process individual jobs, like updating individual documents in mongoDB.

var kue = require('kue')
 , queue = kue.createQueue();

queue.process('staleLocks', function(job, done){
  removeLock(job.data.id, done);
});

function removeLock(id, done) {    
  // mongoDB call
  done();
}

But, I want to update all the documents at once by collecting all the ids aftet the queue has been processed. I am able to even store all the ids. The only problem is that am not able to track, when the queue has been processed, some sort of event to synchronize the whole process.

Upvotes: 3

Views: 636

Answers (1)

tgo
tgo

Reputation: 1545

If I understand your problem correctly, I think there is queue maintenance function queue.inactiveCount() described here that might help.

Wouldn't you be able to just run periodically a check for the length of the queue and do your lock removal whenever the count is 0 and the number of collected locks is > 0 ? Something like

// assume locksid is an array with the collected locks id
var intervalHandle = setInterval( function() {

  queue.inactiveCount(function( err, total ){
    if (!err && total === 0 && locksid.length > 0 ){
      locksid.forEach( function(lock){
        removeLock(lock);
      })
    }
  })
}, 2000 );

// somewhere else in your code, you might want
// to stop the interval check
clearInterval( intervalHandle );

Since I don't know how your worker is built, I left the clearInterval() call out of the callback, but if the removeLock() is a one time task, you could even call it once the forEach() loop is done.

I hope this helps.

Upvotes: 2

Related Questions