如果队列不为空,则Node.js Kue自动处理作业

时间:2015-10-27 11:58:09

标签: javascript node.js kue

我是Kue的新手并且有一些问题。希望你能帮助我:)。

我阅读了所有Kue的文档,但我看到只有一种方法来处理队列中的作业:手动调用处理。如果队列为空,我需要在排队时立即处理作业,如果队列不为空则在前一个完成后运行休息作业。有什么办法吗?谢谢。

或者,如果我重新提问:如果具有特定类型的作业将从队列中用完,会发生什么?我是否应该再次开始处理,否则一旦新工作出现就会自动停止处理?

实际上,由于服务器端限制请求,我需要及时传播API请求。

1 个答案:

答案 0 :(得分:3)

我'写了一个小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,依此类推。