I've have just dived into NodeJS and I'm reading on taking advantage of all CPU's cores and spawning the process among all the available resources.
It seems like a pretty interesting topic, and I'm testing it right now. But I have a question concerning the argv object key in the cluster settings.
I'm starting the server by using this script :
var path = require("path"),
colors = require("colors");
var cluster = require('cluster'),
os = require('os'),
cores = os.cpus();
/*
var config = require(path.join(__dirname, 'config.json'));
*/
/*
* Start Server
*/
cluster.setupMaster({
exec : path.join(__dirname, "application.js"),
args : ["--environment", "dev"]
});
if (cluster.isMaster) {
for (var i = cores.length - 1; i >= 0; i--) {
cluster.fork();
};
cluster.on("fork", function(worker) {
console.log("Worker : [ %d ][ Status : Forking ]".cyan, worker.process.pid);
});
cluster.on("online", function(worker) {
console.log("Worker : [ %d ][ Status : Online ]".green, worker.process.pid);
});
cluster.on("listening", function(worker, address) {
console.log("Worker : [ %d ][ Status : Listening ][ Address : %s ][ Port : %d ]".yellow, worker.process.pid, address.address, address.port);
});
cluster.on("disconnect", function(worker) {
console.log("Worker : [ %d ][ Status : Disconnected ]".white, worker.process.pid);
});
/*
* Restart Dead Workers
*/
cluster.on("exit", function(worker, code, signal) {
console.log("Worker : [ %d ][ Status : Exit ][ Signal : %s ][ Code : %s ]".red, worker.process.pid, signal, code);
cluster.fork();
});
} else {
};
The above would be my server.js, which when run, as stated in the exec, starts off the application :
var express = require("express"),
http = require('http'),
path = require("path"),
optimist = require("optimist"),
colors = require("colors"),
recess = require("recess");
var application = module.exports = express(),
config = optimist.argv;
console.log(config);
var routes = require(path.join(__dirname, "routes")),
api = require(path.join(__dirname, "routes", "api"));
application.set('view engine', 'jade');
application.set('views', path.join(__dirname, 'views'));
application.configure(function(){
application.use(express.bodyParser());
application.use(express.methodOverride());
application.use("/assets", express.static(path.join(__dirname, "public")));
application.use(express.logger('dev'));
application.use(application.router);
});
application.configure('development', function(){
application.use(express.errorHandler({
dumpExceptions: true,
showStack: true
}));
});
application.configure('production', function(){
application.use(express.errorHandler());
});
/*
* JSON API
*/
application.get('/api/test', api.test);
/*
* Routing
*/
application.get('/', routes.index);
application.get('/partials/:name', routes.partials);
application.get('*', routes.index);
/*
* Start Server
*/
var server = http.createServer(application).listen(process.env.PORT);
So firstly, can I use argv to send an object as options to the application.js script ? If so how ? If not I guess I can only send simple options as I'm doing right now.
Secondly, and lastly, am I doing the syntax right ? Is it necessary to have something else in the if(cluster.isMaster) {} else {} ( I'm most interested in if there's anything needed in the else ) ? I'm sorry if the question seems a bit stupid, but I've just started a few days ago and I'm pretty eager to learn more about Node :)
There are 2 ways you can send objects to the child:
online event on Master, and then sending an object using cluster.worker[<id>].send(<object>) which will automatically take care of (de)serialization.You master looks fine. About the only improvement I can suggest is wrapping your workers in domain for better error handling.