Hi i am working with kue , cluster , redis , node.js . here is a very simple example , later i will call the test function from outside.
My purpose is to add some incoming jobs to a queue and process it using workers .
In my case when a user completed an order using my app , he will get an email . i will send the email details to the queue and process it using workers .
var cluster = require('cluster');
var kue = require('kue');
var jobs = kue.createQueue();
var max_workers = 3;
function test() {
jobs.create('email', {
title: 'welcome email for tj'
, to: 'tj@learnboost.com'
, template: 'welcome-email'
}).save( function(err){
if( !err){} console.log( "no error");
});
if( cluster.isMaster ) {
for (var i = 0; i < max_workers; i++) {
cluster.fork();
console.log("forked -> "+i);
}
} else {
jobs.process('email', function(jobs, done){
console.log('Job', jobs.id, 'is done');
done && done();
});
}
}
setInterval(test,3000);
when i run the script node test.js
i get this output
forked -> 0
forked -> 1
forked -> 2
no error
forked -> 0
forked -> 1
forked -> 2
no error
no error
no error
no error
forked -> 0
forked -> 1
forked -> 2
no error
no error
no error
no error
no error
no error
no error
forked -> 0
forked -> 1
forked -> 2
no error
no error
no error
no error
no error
no error
no error
no error
no error
no error
forked -> 0
forked -> 1
forked -> 2
no error
no error
no error
no error
no error
no error
no error
no error
no error
no error
no error
no error
no error
forked -> 0
forked -> 1
forked -> 2
no error
no error
no error
no error
Job 176 is done
no error
Job 177 is done
no error
no error
no error
no error
no error
Job 178 is done
no error
no error
no error
Job 179 is done
no error
no error
no error
and goes on ....
So as i can see , master process is running time to time and fork() the child processes again , and also jobs are getting done .
My Questions are
- Is this the correct model ? ( My purpose is to add some incoming jobs to a queue and process it using workers )
- Every time i create a job using jobs.create() , is my job queue is recreated or just append new jobs to the existing queue ?
- When the master process forks child processes again , what happened if a worker is doing some work , will he finish the the job or that job will be stopped uncompleted ?
I have tried to understand by myself few days but still have doubts , If anyone can explain the logic behind these it will be really appreciated :)
Thanks in advance .