From 6c2a37349eb5ead8e32e466c61e7f813d052694d Mon Sep 17 00:00:00 2001 From: Jeff Barczewski Date: Tue, 15 Jan 2013 11:23:53 -0600 Subject: [PATCH] Allow timeouts to be configurable add additional options to clusterMaster config * `stopTimeout` - Time in milliseconds to wait for worker to stop before forcefully killing the process during restart or resize, default 5000 (5 seconds) * `skepticTimeout` - Time in milliseconds to wait for worker to live before shutting previous worker down during restart, default 2000 (2 seconds) --- README.md | 6 ++++++ cluster-master.js | 9 +++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5a3fb6a..5402200 100644 --- a/README.md +++ b/README.md @@ -88,4 +88,10 @@ The `exec`, `env`, `argv`, and `silent` configs are passed to the * `onMessage` - Method that gets called when workers send a message to the parent. Called in the context of the worker, so you can reply by looking at `this`. +* `stopTimeout` - Time in milliseconds to wait for worker to stop before + forcefully killing the process during restart or resize, default 5000 + (5 seconds) +* `skepticTimeout` - Time in milliseconds to wait for worker to live + before shutting previous worker down during restart, default 2000 + (2 seconds) diff --git a/cluster-master.js b/cluster-master.js index 555d18c..afe5dca 100644 --- a/cluster-master.js +++ b/cluster-master.js @@ -12,6 +12,8 @@ var cluster = require("cluster") , net = require('net') , fs = require('fs') , util = require('util') +, stopTimeout = 5000 // default time (ms) to wait for stop before kill +, skepticTimeout = 2000 // default time (ms) to live before stopping old exports = module.exports = clusterMaster exports.restart = restart @@ -56,6 +58,9 @@ function clusterMaster (config) { onmessage = config.onMessage || config.onmessage + if (config.stopTimeout) stopTimeout = config.stopTimeout + if (config.skepticTimeout) skepticTimeout = config.skepticTimeout + clusterSize = config.size || os.cpus().length env = config.env @@ -246,7 +251,7 @@ function forkListener () { disconnectTimer = setTimeout(function () { debug("Worker %j, forcefully killing", id) worker.process.kill("SIGKILL") - }, 5000) + }, stopTimeout) }) }) } @@ -312,7 +317,7 @@ function restart (cb) { worker.disconnect() } graceful() - }, 2000) + }, skepticTimeout) newbie.on('exit', skeptic) function skeptic () { debug('New worker died quickly. Aborting restart.')