How to retreieve jobs with specific status in kue?

I am using kue for my job queue, and I’d like to know without using the GUI how many jobs are still left, how many have failed, etc. How can I retrieve this kind of information?

For example, after a few minutes of starting the processing of the job queue, I’d like to o update the status of all jobs that failed so far to ‘inactive’, in order to restart them.

The only related question I could find on stackoverflow was this, however, it deals with one job at a time, after it fires a certain event as it is being processed. My concern is different, as I am interested in retrieving all jobs in the database with a certain status.

The answer to this question mentions the function .complete of the kue library, which retrieves all the completed jobs in the database. Are there similar functions for other possible job statuses?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

I found a solution by browsing the kue source code. The following code achieves what I need:

var redis = require ('redis'),
    kue = require ('kue'),
    redisClient = redis.createClient(6379, "127.0.0.1");

kue.redis.createClient = function () {
    return redisClient;
};

kue.app.listen(3000); 


kue.Job.rangeByType ('job', 'failed', 0, 10, 'asc', function (err, selectedJobs) {
    selectedJobs.forEach(function (job) {
        job.state('inactive').save();
    });
});

For reference, here is the relevant kue source code:

/queue/job.js:123:

/**
 * Get jobs of `type` and `state`, with the range `from`..`to`
 * and invoke callback `fn(err, ids)`.
 *
 * @param {String} type
 * @param {String} state
 * @param {Number} from
 * @param {Number} to
 * @param {String} order
 * @param {Function} fn
 * @api public
 */

exports.rangeByType = function(type, state, from, to, order, fn){
  redis.client().zrange('q:jobs:' + type + ':' + state, from, to, get(fn, order));
};

Kue source code indicating that:

  • type is the job type
  • from, to is the job ranges by index (for example, you can specify load jobs from index 0 to 10, 11 jobs in total.)
  • order specifies the order of fetched jobs. Default is asc. You can also sort it by desc

Solution 2

The following works, uses the pre-existing queue object and hence, no double Redis connection issue as mentioned by Japrescott in the comments of the accepted answer.

queue.cardByType("notifications", "complete", function( err, count ) {
  console.log(count);
});

Feel free to replace with a valid state, the following is a list of valid states.

inactive
complete
active
failed
delayed

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply