aboutsummaryrefslogtreecommitdiff
path: root/lib/cluster.js
diff options
context:
space:
mode:
authorAndreas Madsen <amwebdk@gmail.com>2012-01-05 20:09:43 +0100
committerRyan Dahl <ry@tinyclouds.org>2012-01-20 13:09:56 -0800
commitf9a47debfca854666e45de299b0bb630db4a1548 (patch)
tree44ee8fcb77d1675d0a0eab99dd2fceaa4051abd1 /lib/cluster.js
parent6b5853794d77094f960795cacf21d37841f1a890 (diff)
downloadandroid-node-v8-f9a47debfca854666e45de299b0bb630db4a1548.tar.gz
android-node-v8-f9a47debfca854666e45de299b0bb630db4a1548.tar.bz2
android-node-v8-f9a47debfca854666e45de299b0bb630db4a1548.zip
Add cluster.setupMaster
Fixes #2470
Diffstat (limited to 'lib/cluster.js')
-rw-r--r--lib/cluster.js69
1 files changed, 39 insertions, 30 deletions
diff --git a/lib/cluster.js b/lib/cluster.js
index 6f003f5212..ce593ad01f 100644
--- a/lib/cluster.js
+++ b/lib/cluster.js
@@ -61,8 +61,6 @@ var cluster = module.exports = new cluster();
var masterStarted = false;
var ids = 0;
var serverHandlers = {};
-var workerFilename;
-var workerArgs;
// Used in the worker:
var serverLisenters = {};
@@ -78,6 +76,9 @@ cluster.worker = cluster.isWorker ? {} : null;
// The workers array is oly used in the naster
cluster.workers = cluster.isMaster ? {} : null;
+// Settings object
+var settings = cluster.settings = {};
+
// Simple function there call a function on each worker
function eachWorker(cb) {
// Go througe all workers
@@ -88,36 +89,44 @@ function eachWorker(cb) {
}
}
-// Call this from the master process. It will start child workers.
-//
-// options.workerFilename
-// Specifies the script to execute for the child processes. Default is
-// process.argv[1]
-//
-// options.args
-// Specifies program arguments for the workers. The Default is
-// process.argv.slice(2)
-//
-// options.workers
-// The number of workers to start. Defaults to os.cpus().length.
-function startMaster() {
- // This can only be called from the master.
- assert(cluster.isMaster);
+cluster.setupMaster = function(options) {
+ // This can only be called from the master.
+ assert(cluster.isMaster);
- if (masterStarted) return;
- masterStarted = true;
+ // Don't allow this function to run more that once
+ if (masterStarted) return;
+ masterStarted = true;
- workerFilename = process.argv[1];
- workerArgs = process.argv.slice(2);
+ // Get filename and arguments
+ options = options || {};
+
+ // Set settings object
+ settings = cluster.settings = {
+ exec: options.exec || process.argv[1],
+ args: options.args || process.argv.slice(2),
+ silent: options.silent || false
+ };
- process.on('uncaughtException', function(e) {
- console.error('Exception in cluster master process: ' +
- e.message + '\n' + e.stack);
+ // Kill workers when a uncaught exception is received
+ process.on('uncaughtException', function(err) {
+ // Did the user install a listener? If so, it overrides this one.
+ if (process.listeners('uncaughtException').length > 1) return;
+ // Output the error stack, and create on if non exist
+ if (!(err instanceof Error)) {
+ err = new Error(err);
+ }
+ console.error(err.stack);
+
+ // quick destroy cluster
quickDestroyCluster();
+ // when done exit process with error code: 1
process.exit(1);
- });
-}
+ });
+
+ // emit setup event
+ cluster.emit('setup');
+};
// Check if a message is internal only
var INTERNAL_PREFIX = 'NODE_CLUSTER_';
@@ -275,10 +284,10 @@ function Worker(customEnv) {
}
// fork worker
- this.process = fork(workerFilename, workerArgs, {
- 'env': envCopy
+ this.process = fork(settings.exec, settings.args, {
+ 'env': envCopy,
+ 'silent': settings.silent
});
-
} else {
this.process = process;
}
@@ -426,7 +435,7 @@ cluster.fork = function(env) {
assert(cluster.isMaster);
// Make sure that the master has been initalized
- startMaster();
+ cluster.setupMaster();
return (new cluster.Worker(env));
};