简体   繁体   English

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

[英]Node.js Kue autoprocessing jobs if queue is not empty

I'm new to Kue and have some questions. 我是Kue的新手并且有一些问题。 Hope you can help me :) 希望你能帮我 :)

I read all Kue's docs, but I saw only one way to process jobs in queue: call processing manually. 我阅读了所有Kue的文档,但我看到只有一种方法来处理队列中的作业:手动调用处理。 I need to process jobs as soon as they get to queue if queue was empty and run rest jobs after previous was done if queue is not empty. 如果队列为空,我需要在排队时立即处理作业,如果队列不为空则在前一个完成后运行休息作业。 Is there any way to do it? 有什么办法吗? Thank you. 谢谢。

Or if I rephrase question: What will happen if jobs with specific type will run out from queue? 或者,如果我重新提问:如果具有特定类型的作业将从队列中耗尽,会发生什么? Should I start processing again or it will be autocalled as soon as new job appear? 我是否应该再次开始处理,或者在新工作出现后立即进行自动停止?

Actually, I need to spread API requests due time, since server-side limits requests. 实际上,由于服务器端限制请求,我需要及时传播API请求。

I' ve written a little js script: 我写了一个小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 );
});

Running this with node script.js produced the following output: 使用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

I did this many times (> 30). 我做了很多次(> 30)。 But one time the remove order was not in sequence. 但有一次删除顺序不是按顺序。 But removing is asynchronous, so that is possible. 但删除是异步的,所以这是可能的。

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

The output is a bit different because I added the title to the processing message after that run. 输出有点不同,因为我在运行后将标题添加到处理消息中。 Unfortunatelly the processing order is not clear in that example. 不幸的是,在该示例中,处理顺序并不清楚。 And I can not reproduce it. 我无法重现它。 :-( :-(

EDIT 编辑

I added a for loop to better investigate that behaviour. 我添加了一个for循环来更好地调查这种行为。 And quel surprise: 令人惊讶的是:

It seams the jobs will be processes in lexical order of their job id. 它接缝作业将按其作业ID的词汇顺序进行处理。

The added loop: 添加的循环:

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();

The new output: 新输出:

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

So this behaviour will surely be reproducible when the job id changes from 9 to 10 from 99 to 100 from 999 to 1000 and so on. 因此,当作业ID从9变为10,从99变为100,从999变为1000时,此行为肯定是可重现的,依此类推。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM