Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: setup cluster workers before preloading #1314

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@
} else {
// There is user code to be run

// If this is a worker in cluster mode, start up the communication
// channel. This needs to be done before any user code gets executed
// (including preload modules).
if (process.argv[1] && process.env.NODE_UNIQUE_ID) {
var cluster = NativeModule.require('cluster');
cluster._setupWorker();

// Make sure it's not accidentally inherited by child processes.
delete process.env.NODE_UNIQUE_ID;
}

// Load any preload modules
if (process._preload_modules) {
var Module = NativeModule.require('module');
Expand All @@ -87,16 +98,6 @@
var path = NativeModule.require('path');
process.argv[1] = path.resolve(process.argv[1]);

// If this is a worker in cluster mode, start up the communication
// channel.
if (process.env.NODE_UNIQUE_ID) {
var cluster = NativeModule.require('cluster');
cluster._setupWorker();

// Make sure it's not accidentally inherited by child processes.
delete process.env.NODE_UNIQUE_ID;
}

var Module = NativeModule.require('module');

if (global.v8debug &&
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/cluster-preload-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var cluster = require('cluster');
if (cluster.isMaster) {
cluster.fork(); // one child
cluster.on('exit', function(worker, code, signal) {
console.log('worker terminated with code ' + code);
});
}
3 changes: 3 additions & 0 deletions test/fixtures/cluster-preload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var cluster = require('cluster');
cluster.isMaster || process.exit(42 + cluster.worker.id); // +42 to distinguish
// from exit(1) for other random reasons
8 changes: 8 additions & 0 deletions test/parallel/test-preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,11 @@ child_process.exec(nodeBinary + ' '
if (err) throw err;
assert.equal(stdout, 'A\nB\nhello\n');
});

child_process.exec(nodeBinary + ' '
+ '--require ' + fixture('cluster-preload.js') + ' '
+ fixture('cluster-preload-test.js'),
function(err, stdout, stderr) {
if (err) throw err;
assert.ok(/worker terminated with code 43/.test(stdout));
});