summaryrefslogtreecommitdiff
path: root/lib/cluster.js
diff options
context:
space:
mode:
authorcjihrig <cjihrig@gmail.com>2014-07-30 22:20:52 -0400
committerFedor Indutny <fedor@indutny.com>2014-08-01 00:34:40 +0400
commitd287b8e58adb7c3b09ca5c0aa8e9420322d40d10 (patch)
treedf22a8b819744b77c98ca214976ce11bc2881424 /lib/cluster.js
parent4b59db008cec1bfcca2783f4b27c630c9c3fdd73 (diff)
downloadandroid-node-v8-d287b8e58adb7c3b09ca5c0aa8e9420322d40d10.tar.gz
android-node-v8-d287b8e58adb7c3b09ca5c0aa8e9420322d40d10.tar.bz2
android-node-v8-d287b8e58adb7c3b09ca5c0aa8e9420322d40d10.zip
cluster: support options in Worker constructor
This commit moves some common Worker code into the constructor via support for an options argument. Reviewed-By: Fedor Indutny <fedor@indutny.com>
Diffstat (limited to 'lib/cluster.js')
-rw-r--r--lib/cluster.js94
1 files changed, 54 insertions, 40 deletions
diff --git a/lib/cluster.js b/lib/cluster.js
index 47c07ded43..3e853bcdbc 100644
--- a/lib/cluster.js
+++ b/lib/cluster.js
@@ -35,12 +35,24 @@ cluster.isWorker = ('NODE_UNIQUE_ID' in process.env);
cluster.isMaster = (cluster.isWorker === false);
-function Worker() {
- if (!(this instanceof Worker)) return new Worker;
+function Worker(options) {
+ if (!(this instanceof Worker))
+ return new Worker(options);
+
EventEmitter.call(this);
+
+ if (!util.isObject(options))
+ options = {};
+
this.suicide = undefined;
- this.state = 'none';
- this.id = 0;
+ this.state = options.state || 'none';
+ this.id = options.id | 0;
+
+ if (options.process) {
+ this.process = options.process;
+ this.process.on('error', this.emit.bind(this, 'error'));
+ this.process.on('message', this.emit.bind(this, 'message'));
+ }
}
util.inherits(Worker, EventEmitter);
@@ -190,26 +202,6 @@ if (cluster.isMaster)
else
workerInit();
-
-function createWorkerExecArgv(masterExecArgv, worker) {
- var args = masterExecArgv.slice();
- var debugPort = process.debugPort + worker.id;
- var hasDebugArg = false;
-
- for (var i = 0; i < args.length; i++) {
- var match = args[i].match(/^(--debug|--debug-brk)(=\d+)?$/);
- if (!match) continue;
- args[i] = match[1] + '=' + debugPort;
- hasDebugArg = true;
- }
-
- if (!hasDebugArg)
- args = ['--debug-port=' + debugPort].concat(args);
-
- return args;
-}
-
-
function masterInit() {
cluster.workers = {};
@@ -278,21 +270,46 @@ function masterInit() {
});
};
- var ids = 0;
- cluster.fork = function(env) {
- cluster.setupMaster();
- var worker = new Worker;
- worker.id = ++ids;
+ function createWorkerProcess(id, env) {
var workerEnv = util._extend({}, process.env);
+ var execArgv = cluster.settings.execArgv.slice();
+ var debugPort = process.debugPort + id;
+ var hasDebugArg = false;
+
workerEnv = util._extend(workerEnv, env);
- workerEnv.NODE_UNIQUE_ID = '' + worker.id;
- worker.process = fork(cluster.settings.exec, cluster.settings.args, {
+ workerEnv.NODE_UNIQUE_ID = '' + id;
+
+ for (var i = 0; i < execArgv.length; i++) {
+ var match = execArgv[i].match(/^(--debug|--debug-brk)(=\d+)?$/);
+
+ if (match) {
+ execArgv[i] = match[1] + '=' + debugPort;
+ hasDebugArg = true;
+ }
+ }
+
+ if (!hasDebugArg)
+ execArgv = ['--debug-port=' + debugPort].concat(execArgv);
+
+ return fork(cluster.settings.exec, cluster.settings.args, {
env: workerEnv,
silent: cluster.settings.silent,
- execArgv: createWorkerExecArgv(cluster.settings.execArgv, worker),
+ execArgv: execArgv,
gid: cluster.settings.gid,
uid: cluster.settings.uid
});
+ }
+
+ var ids = 0;
+
+ cluster.fork = function(env) {
+ cluster.setupMaster();
+ var id = ++ids;
+ var workerProcess = createWorkerProcess(id, env);
+ var worker = new Worker({
+ id: id,
+ process: workerProcess
+ });
worker.process.once('exit', function(exitCode, signalCode) {
worker.suicide = !!worker.suicide;
worker.state = 'dead';
@@ -307,8 +324,6 @@ function masterInit() {
cluster.emit('disconnect', worker);
delete cluster.workers[worker.id];
});
- worker.process.on('error', worker.emit.bind(worker, 'error'));
- worker.process.on('message', worker.emit.bind(worker, 'message'));
worker.process.on('internalMessage', internal(worker, onmessage));
process.nextTick(function() {
cluster.emit('fork', worker);
@@ -447,11 +462,12 @@ function workerInit() {
// Called from src/node.js
cluster._setupWorker = function() {
- var worker = new Worker;
+ var worker = new Worker({
+ id: +process.env.NODE_UNIQUE_ID | 0,
+ process: process,
+ state: 'online'
+ });
cluster.worker = worker;
- worker.id = +process.env.NODE_UNIQUE_ID | 0;
- worker.state = 'online';
- worker.process = process;
process.once('disconnect', function() {
if (!worker.suicide) {
// Unexpected disconnect, master exited, or some such nastiness, so
@@ -459,8 +475,6 @@ function workerInit() {
process.exit(0);
}
});
- worker.process.on('error', worker.emit.bind(worker, 'error'));
- worker.process.on('message', worker.emit.bind(worker, 'message'));
process.on('internalMessage', internal(worker, onmessage));
send({ act: 'online' });
function onmessage(message, handle) {