Reputation: 637
I'm new to Kue and have some questions. 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. 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.
Upvotes: 2
Views: 3256
Reputation: 2124
I' ve written a little js script:
var kue = require('kue')
, queue = kue.createQueue();
var job = queue.create('email', {
title: 'welcome email for tj'
, to: '[email protected]'
, 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: '[email protected]'
, template: 'welcome-email'
}).save( function(err){
if( !err ) console.log( job1.id );
});
var job2 = queue.create('email', {
title: 'welcome email for tj 3'
, to: '[email protected]'
, template: 'welcome-email'
}).save( function(err){
if( !err ) console.log( job2.id );
});
Running this with node script.js
produced the following output:
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). 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. And quel surprise:
It seams the jobs will be processes in lexical order of their job id.
The added loop:
for (var i=0; i<20; i++)
{
var job2 = queue.create('email', {
title: 'welcome email for tj ' + (i+4)
, to: '[email protected]'
, 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.
Upvotes: 4