我创建了一个工作:
var kue = require('kue');
var queue = kue.createQueue();
//name of the queue is myQueue
var job = queue.create('myQueue', {
from: 'process1',
type: 'testMessage',
data: {
msg: 'Hello world!'
}
}).save(function(err) {
if (err) {
console.log('Unable to save ' + err);
} else {
console.log('Job ' + job.id + ' saved to the queue.');
}
});
有没有办法让我自己更新作业状态(即事件、失败、进行中)?例如:
消费者接受工作:
queue.process('myQueue', function(job, done){
console.log('IN HERE', job.state) // returns function
});
这是从上面返回的函数:
function ( state, fn ) {
if( 0 == arguments.length ) return this._state;
var client = this.client
, fn = fn || noop;
var oldState = this._state;
var multi = client.multi();
我想对工作状态进行硬编码,例如job.state = 'failed'
并允许自己在需要时更新作业状态?
这在 Kue 中可能吗?
最佳答案
快速回答,是的,您可以使用 job.failed() 或发送错误以完成。
queue.process('myQueue', function(job, done){
console.log('IN HERE', job.state) // returns function
job.failed();
done(new Error('bad'));
});
但是,听起来您想自己进行处理。您可以像这样设置自己的函数。
queue.on('job enqueue', function(id, type){
console.log( 'Job %s got queued of type %s', id, type );
kue.Job.get(id, function(err, job){
if (err) return;
// do your custom processing here
if( something was processed ){
job.complete();
}else{
job.failed();
}
});
});
这里还有一些您还可以使用的选项。
job.inactive();
job.active();
job.complete();
job.delayed();
此页面上有一些示例。 https://github.com/Automattic/kue
关于javascript - 在 KUE Node js 中更新作业状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43540568/