How to retreieve jobs with specific status in kue?
Asked Answered
P

2

9

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?

Powwow answered 3/2, 2013 at 3:47 Comment(0)
P
17

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
Powwow answered 27/2, 2013 at 9:1 Comment(4)
this works good on localhost. But by using internal functions, rangeByType will actually create a new RedisClient (bound to localhost, default) and not use the one provided to kue. This makes it hard to do above with redis on an other host. My solution is to query redis directly (with the code provided in rangeByType)Mcclure
@Mcclure I know its been awhile but did you get this too work?Monochord
@JamesMorris I would recommend not to use kue but to use something like bull. Kue is no longer maintained and has different limitations and many outstanding bugs if you want to go beyond a basic task queueMcclure
@Mcclure Thanks. I will look into bullMonochord
B
0

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
Bobble answered 10/9, 2021 at 11:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.