How to retreieve jobs with specific status in kue?

陌路散爱 提交于 2020-01-01 08:57:18

问题


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?


回答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


来源:https://stackoverflow.com/questions/14669306/how-to-retreieve-jobs-with-specific-status-in-kue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!