我通过浏览 kue 源代码找到了解决方案。以下代码实现了我所需要的:
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();
});
});
作为参考,这里是相关的 kue 源代码:
/队列/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源代码表明:
type
是工作类型
from
,to
是索引的作业范围(例如,您可以指定从索引 0 到 10 的加载作业,总共 11 个作业。)
order
指定获取作业的顺序。默认为asc
。您也可以按以下方式排序desc