Permalink
Browse files

Added delayed forking and other stuff

  • Loading branch information...
1 parent 3bdb038 commit aff7f7ac2eea631f6119b29d0679a6cbf8505180 @mekwall committed Jan 5, 2013
Showing with 53 additions and 7 deletions.
  1. +31 −7 src/overseer.js
  2. +22 −0 src/worker.js
View
@@ -21,7 +21,7 @@ var overseer = Object.create(events.EventEmitter.prototype);
var APPFILE = "./app.js";
var PIDFILE = "./overseer.pid";
var ENV = process.env["NODE_ENV"] || "development";
-var FORKS = require("os").cpus().length;
+var FORKS = 0 + require("os").cpus().length;
var WATCH = false;
var CMD = false;
@@ -53,13 +53,14 @@ overseer.fork = function () {
switch (msg.type) {
case "uncaughtException":
+ overseer.halt = true;
var err = msg.data;
err.stack = err.stack.replace(/\n/g, "\n\t");
console.error(clc.red.bold("W-%s: Uncaught Exception: %s\n\n\t"), pid, err.message, clc.red.bold(err.stack), "\n");
worker.destroy();
- setTimeout(function(){
- overseer.forkWorker();
- }, 5000);
+ overseer.delayedFork(5000, function(){
+ overseer.halt = false;
+ });
break;
case "console":
@@ -70,6 +71,10 @@ overseer.fork = function () {
}
console[msg.method].apply(console, msg.data);
break;
+
+ case "restart":
+ overseer.restartWorkers();
+ break;
}
}
});
@@ -198,14 +203,33 @@ overseer.run = function() {
overseer.workers.splice(overseer.workers.indexOf(worker), 1);
});
- console.info("Master: %s", "Setting up cluster...");
+ console.info("Master: Setting up cluster.");
+ console.info("Master: Forking %d workers...", FORKS);
// Fork workers
for (var i = 0; i < FORKS; i++) {
- overseer.fork();
+ if (i === 0) {
+ overseer.fork();
+ } else {
+ // Fork the rest after 5s
+ overseer.delayedFork(5000);
+ }
}
};
+overseer.delayedFork = function (delay, cb) {
+ setTimeout(function(){
+ if (overseer.halt) {
+ overseer.delayedFork();
+ } else {
+ var fork = overseer.fork();
+ if (typeof cb === "function") {
+ cb(fork);
+ }
+ }
+ }, delay);
+}
+
overseer._restartingWorkers = false;
overseer.on("restartWorkers", function(cb) {
console.info("Master: Restarting %s workers...", overseer.workers.length);
@@ -264,7 +288,7 @@ module.exports = function () {
PIDFILE = options.pidfile;
}
if (options.forks) {
- FORKS = options.forks;
+ FORKS = 0+options.forks;
}
if (options.watch) {
WATCH = options.watch;
View
@@ -32,5 +32,27 @@ if (!cluster.isWorker) {
process.exit(0);
}
+process.on("message", function(msg){
+ if (msg.cmd) {
+ switch (msg.cmd) {
+ case "reload":
+ delete require.cache[msg.file];
+ break;
+
+ case "stat":
+ process.send("stat", {
+ uptime: process.uptime(),
+ memoryUsage: process.memoryUsage()
+ })
+ break;
+ }
+ }
+});
+
+// enable workers to ask for a cluster restart
+cluster.on("restart", function () {
+ process.send("restart");
+});
+
var APPFILE = process.argv[2];
module.exports = require(APPFILE);

0 comments on commit aff7f7a

Please sign in to comment.