我写了一个小 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 等等时,这种行为肯定是可重现的。