Node.js 如果队列不为空,Kue 会自动处理作业
Node.js Kue autoprocessing jobs if queue is not empty
我是 Kue 的新手,有一些问题。希望你能帮助我:)
我阅读了 Kue 的所有文档,但我只看到一种处理队列中作业的方法:手动处理呼叫。如果队列为空,我需要在他们进入队列后立即处理作业,如果队列不为空,则 运行 在完成之前的作业后休息作业。有什么办法吗?谢谢。
或者如果我换个问题:如果特定类型的作业 运行 从队列中退出会怎样?我应该重新开始处理还是一旦出现新工作就会自动调用?
实际上,我需要按时分配 API 个请求,因为服务器端限制了请求。
我写了一点js脚本:
var kue = require('kue')
, queue = kue.createQueue();
var job = queue.create('email', {
title: 'welcome email for tj'
, to: 'tj@learnboost.com'
, template: 'welcome-email'
}).save( function(err){
if( !err ) console.log( job.id );
});
queue.on('job enqueue', function(id, type){
console.log( 'Job %s got queued of type %s', id, type );
}).on('job complete', function(id, result){
kue.Job.get(id, function(err, job){
if (err) return;
job.remove(function(err){
if (err) throw err;
console.log('removed completed job #%d', job.id);
});
});
});
queue.process('email', function(job, done){
console.log("Processing email: " + job.data.title);
email(job.data.to, done);
});
function email(address, done)
{
done();
}
var job1 = queue.create('email', {
title: 'welcome email for tj 2'
, to: 'tj@learnboost.com'
, template: 'welcome-email'
}).save( function(err){
if( !err ) console.log( job1.id );
});
var job2 = queue.create('email', {
title: 'welcome email for tj 3'
, to: 'tj@learnboost.com'
, template: 'welcome-email'
}).save( function(err){
if( !err ) console.log( job2.id );
});
运行 这与 node script.js
产生了以下输出:
29
30
31
Job 29 got queued of type email
Job 30 got queued of type email
Job 31 got queued of type email
Processing email: welcome email for tj
Processing email: welcome email for tj 2
removed completed job #29
Processing email: welcome email for tj 3
removed completed job #30
removed completed job #31
我这样做了很多次(> 30)。但是有一次删除顺序不按顺序。但是删除是异步的,所以这是可能的。
Job 8 got queued of type email
Job 9 got queued of type email
Job 10 got queued of type email
Processing email
Processing email
removed completed job #10
Processing email
removed completed job #8
removed completed job #9
输出有点不同,因为我在 运行 之后将标题添加到处理消息中。不幸的是,该示例中的处理顺序不明确。而且我无法重现它。 :-(
编辑
我添加了一个 for 循环以更好地调查该行为。并解决惊喜:
它接缝作业将按照其作业 ID 的 词汇 顺序进行处理。
添加的循环:
for (var i=0; i<20; i++)
{
var job2 = queue.create('email', {
title: 'welcome email for tj ' + (i+4)
, to: 'tj@learnboost.com'
, template: 'welcome-email'
}).save();
新输出:
87
88
109
Job 87 got queued of type email
Job 88 got queued of type email
Job 89 got queued of type email
Job 90 got queued of type email
Job 91 got queued of type email
Job 92 got queued of type email
Job 93 got queued of type email
Job 94 got queued of type email
Job 95 got queued of type email
Job 96 got queued of type email
Job 97 got queued of type email
Job 98 got queued of type email
Job 99 got queued of type email
Job 100 got queued of type email
Job 101 got queued of type email
Job 102 got queued of type email
Job 103 got queued of type email
Job 104 got queued of type email
Job 105 got queued of type email
Job 106 got queued of type email
Job 107 got queued of type email
Job 108 got queued of type email
Job 109 got queued of type email
Processing email: welcome email for tj 14
Processing email: welcome email for tj 15
removed completed job #100
Processing email: welcome email for tj 16
removed completed job #101
Processing email: welcome email for tj 17
removed completed job #102
Processing email: welcome email for tj 18
removed completed job #103
Processing email: welcome email for tj 19
removed completed job #104
Processing email: welcome email for tj 20
removed completed job #105
Processing email: welcome email for tj 21
removed completed job #106
Processing email: welcome email for tj 22
removed completed job #107
Processing email: welcome email for tj 23
removed completed job #108
Processing email: welcome email for tj
removed completed job #109
Processing email: welcome email for tj 2
removed completed job #87
Processing email: welcome email for tj 3
removed completed job #88
Processing email: welcome email for tj 4
removed completed job #89
Processing email: welcome email for tj 5
removed completed job #90
Processing email: welcome email for tj 6
removed completed job #91
Processing email: welcome email for tj 7
removed completed job #92
Processing email: welcome email for tj 8
removed completed job #93
Processing email: welcome email for tj 9
removed completed job #94
Processing email: welcome email for tj 10
removed completed job #95
Processing email: welcome email for tj 11
removed completed job #96
Processing email: welcome email for tj 12
removed completed job #97
Processing email: welcome email for tj 13
removed completed job #98
removed completed job #99
因此,当作业 ID 从 9 更改为 10,从 99 更改为 100,从 999 更改为 1000 等等时,此行为肯定会重现。
我是 Kue 的新手,有一些问题。希望你能帮助我:)
我阅读了 Kue 的所有文档,但我只看到一种处理队列中作业的方法:手动处理呼叫。如果队列为空,我需要在他们进入队列后立即处理作业,如果队列不为空,则 运行 在完成之前的作业后休息作业。有什么办法吗?谢谢。
或者如果我换个问题:如果特定类型的作业 运行 从队列中退出会怎样?我应该重新开始处理还是一旦出现新工作就会自动调用?
实际上,我需要按时分配 API 个请求,因为服务器端限制了请求。
我写了一点js脚本:
var kue = require('kue')
, queue = kue.createQueue();
var job = queue.create('email', {
title: 'welcome email for tj'
, to: 'tj@learnboost.com'
, template: 'welcome-email'
}).save( function(err){
if( !err ) console.log( job.id );
});
queue.on('job enqueue', function(id, type){
console.log( 'Job %s got queued of type %s', id, type );
}).on('job complete', function(id, result){
kue.Job.get(id, function(err, job){
if (err) return;
job.remove(function(err){
if (err) throw err;
console.log('removed completed job #%d', job.id);
});
});
});
queue.process('email', function(job, done){
console.log("Processing email: " + job.data.title);
email(job.data.to, done);
});
function email(address, done)
{
done();
}
var job1 = queue.create('email', {
title: 'welcome email for tj 2'
, to: 'tj@learnboost.com'
, template: 'welcome-email'
}).save( function(err){
if( !err ) console.log( job1.id );
});
var job2 = queue.create('email', {
title: 'welcome email for tj 3'
, to: 'tj@learnboost.com'
, template: 'welcome-email'
}).save( function(err){
if( !err ) console.log( job2.id );
});
运行 这与 node script.js
产生了以下输出:
29
30
31
Job 29 got queued of type email
Job 30 got queued of type email
Job 31 got queued of type email
Processing email: welcome email for tj
Processing email: welcome email for tj 2
removed completed job #29
Processing email: welcome email for tj 3
removed completed job #30
removed completed job #31
我这样做了很多次(> 30)。但是有一次删除顺序不按顺序。但是删除是异步的,所以这是可能的。
Job 8 got queued of type email
Job 9 got queued of type email
Job 10 got queued of type email
Processing email
Processing email
removed completed job #10
Processing email
removed completed job #8
removed completed job #9
输出有点不同,因为我在 运行 之后将标题添加到处理消息中。不幸的是,该示例中的处理顺序不明确。而且我无法重现它。 :-(
编辑
我添加了一个 for 循环以更好地调查该行为。并解决惊喜:
它接缝作业将按照其作业 ID 的 词汇 顺序进行处理。
添加的循环:
for (var i=0; i<20; i++)
{
var job2 = queue.create('email', {
title: 'welcome email for tj ' + (i+4)
, to: 'tj@learnboost.com'
, template: 'welcome-email'
}).save();
新输出:
87
88
109
Job 87 got queued of type email
Job 88 got queued of type email
Job 89 got queued of type email
Job 90 got queued of type email
Job 91 got queued of type email
Job 92 got queued of type email
Job 93 got queued of type email
Job 94 got queued of type email
Job 95 got queued of type email
Job 96 got queued of type email
Job 97 got queued of type email
Job 98 got queued of type email
Job 99 got queued of type email
Job 100 got queued of type email
Job 101 got queued of type email
Job 102 got queued of type email
Job 103 got queued of type email
Job 104 got queued of type email
Job 105 got queued of type email
Job 106 got queued of type email
Job 107 got queued of type email
Job 108 got queued of type email
Job 109 got queued of type email
Processing email: welcome email for tj 14
Processing email: welcome email for tj 15
removed completed job #100
Processing email: welcome email for tj 16
removed completed job #101
Processing email: welcome email for tj 17
removed completed job #102
Processing email: welcome email for tj 18
removed completed job #103
Processing email: welcome email for tj 19
removed completed job #104
Processing email: welcome email for tj 20
removed completed job #105
Processing email: welcome email for tj 21
removed completed job #106
Processing email: welcome email for tj 22
removed completed job #107
Processing email: welcome email for tj 23
removed completed job #108
Processing email: welcome email for tj
removed completed job #109
Processing email: welcome email for tj 2
removed completed job #87
Processing email: welcome email for tj 3
removed completed job #88
Processing email: welcome email for tj 4
removed completed job #89
Processing email: welcome email for tj 5
removed completed job #90
Processing email: welcome email for tj 6
removed completed job #91
Processing email: welcome email for tj 7
removed completed job #92
Processing email: welcome email for tj 8
removed completed job #93
Processing email: welcome email for tj 9
removed completed job #94
Processing email: welcome email for tj 10
removed completed job #95
Processing email: welcome email for tj 11
removed completed job #96
Processing email: welcome email for tj 12
removed completed job #97
Processing email: welcome email for tj 13
removed completed job #98
removed completed job #99
因此,当作业 ID 从 9 更改为 10,从 99 更改为 100,从 999 更改为 1000 等等时,此行为肯定会重现。