Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit 07b1997

Browse files
AndreasMadsenry
authored andcommitted
Add env argument to cluster.fork
Fixes 2378
1 parent 8085876 commit 07b1997

File tree

3 files changed

+94
-10
lines changed

3 files changed

+94
-10
lines changed

Diff for: doc/api/cluster.markdown

+4-2
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,11 @@ in the master process via message passing:
7272

7373

7474

75-
### cluster.fork()
75+
### cluster.fork([env])
7676

7777
Spawn a new worker process. This can only be called from the master process.
78+
The function takes an optional `env` object. The propertyies in this object
79+
will be added to the process environment in the worker.
7880

7981
### cluster.isMaster
8082
### cluster.isWorker
@@ -92,6 +94,6 @@ This can be used to restart the worker by calling `fork()` again.
9294
console.log('worker ' + worker.pid + ' died. restart...');
9395
cluster.fork();
9496
});
95-
97+
9698
Different techniques can be used to restart the worker depending on the
9799
application.

Diff for: lib/cluster.js

+24-8
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,21 @@ var fork = require('child_process').fork;
2424
var net = require('net');
2525
var EventEmitter = require('events').EventEmitter;
2626

27-
var cluster = module.exports = new EventEmitter();
27+
function isObject(o) {
28+
return (typeof o === 'object' && o !== null);
29+
}
2830

31+
function extendObject(origin, add) {
32+
console.log('object: ', add);
33+
for (var name in add) {
34+
if (Object.hasOwnProperty.call(add, name)) {
35+
origin[name] = add[name];
36+
}
37+
}
38+
return origin;
39+
}
40+
41+
var cluster = module.exports = new EventEmitter();
2942

3043
var debug;
3144
if (process.env.NODE_DEBUG && /cluster/.test(process.env.NODE_DEBUG)) {
@@ -139,23 +152,26 @@ function eachWorker(cb) {
139152
}
140153

141154

142-
cluster.fork = function() {
155+
cluster.fork = function(env) {
143156
// This can only be called from the master.
144157
assert(cluster.isMaster);
145158

146159
// Lazily start the master process stuff.
147160
startMaster();
148161

149162
var id = ++ids;
150-
var envCopy = {};
151-
152-
for (var x in process.env) {
153-
envCopy[x] = process.env[x];
154-
}
155163

164+
//Create env object
165+
var envCopy = extendObject({}, process.env);
156166
envCopy['NODE_WORKER_ID'] = id;
167+
if (isObject(env)) {
168+
envCopy = extendObject(envCopy, env);
169+
}
157170

158-
var worker = fork(workerFilename, workerArgs, { env: envCopy });
171+
//fork worker
172+
var worker = fork(workerFilename, workerArgs, {
173+
'env': envCopy
174+
});
159175

160176
workers[id] = worker;
161177

Diff for: test/simple/test-cluster-fork-env.js

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
23+
var common = require('../common');
24+
var assert = require('assert');
25+
var cluster = require('cluster');
26+
27+
if (cluster.isWorker) {
28+
process.send({
29+
testcase: true,
30+
prop: process.env['cluster_test_prop'],
31+
overwrite: process.env['cluster_test_overwrite']
32+
});
33+
34+
} else if (cluster.isMaster) {
35+
36+
var checks = {
37+
using: false,
38+
overwrite: false
39+
};
40+
41+
//To check that the cluster extend on the process.env we will overwrite a
42+
//property
43+
process.env['cluster_test_overwrite'] = 'old';
44+
45+
//Fork worker
46+
var worker = cluster.fork({
47+
'cluster_test_prop': 'custom',
48+
'cluster_test_overwrite': 'new'
49+
});
50+
51+
//Checks worker env
52+
worker.on('message', function(data) {
53+
if (data.testcase) {
54+
checks.using = (data.prop === 'custom');
55+
checks.overwrite = (data.overwrite === 'new');
56+
process.exit(0);
57+
}
58+
});
59+
60+
process.once('exit', function() {
61+
assert.ok(checks.using, 'The worker did not receive the correct env.');
62+
assert.ok(checks.overwrite, 'The custom environment did not overwrite ' +
63+
'the existing environment.');
64+
});
65+
66+
}

0 commit comments

Comments
 (0)