Skip to content

Commit

Permalink
cluster: support windowsHide option for workers
Browse files Browse the repository at this point in the history
  • Loading branch information
toddwong committed Dec 2, 2017
1 parent e9e9863 commit 45134fb
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
2 changes: 2 additions & 0 deletions doc/api/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,8 @@ changes:
This can be a number, or a function that takes no arguments and returns a
number. By default each worker gets its own port, incremented from the
master's `process.debugPort`.
* `windowsHide` {boolean} Hide the forked processes console window that would
normally be created on Windows systems. **Default:** `false`

After calling `.setupMaster()` (or `.fork()`) this settings object will contain
the settings, including the default values.
Expand Down
1 change: 1 addition & 0 deletions lib/internal/cluster/master.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ function createWorkerProcess(id, env) {
return fork(cluster.settings.exec, cluster.settings.args, {
env: workerEnv,
silent: cluster.settings.silent,
windowsHide: cluster.settings.windowsHide,
execArgv: execArgv,
stdio: cluster.settings.stdio,
gid: cluster.settings.gid,
Expand Down
77 changes: 77 additions & 0 deletions test/parallel/test-cluster-fork-windowsHide.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const child_process = require('child_process');

let patchedFork = child_process.fork;
child_process.fork = (...args) => patchedFork.apply(this, args);
const cluster = require('cluster');

if (!process.argv[2]) {
const master = child_process.spawn(
process.argv[0],
[process.argv[1], '--cluster'],
{ detached: true, stdio: ['ignore', 'ignore', 'ignore', 'ipc'] });

const messageHandlers = {
windowsHide: common.mustCall((msg) => {
assert.strictEqual(msg.value, true);
}),
workerOnline: common.mustCall((msg) => {
}),
mainWindowHandle: common.mustCall((msg) => {
assert.ok(/0\s*/.test(msg.value));
}),
workerExit: common.mustCall((msg) => {
assert.strictEqual(msg.code, 0);
assert.strictEqual(msg.signal, null);
})
};

master.on('message', (msg) => {
const handler = messageHandlers[msg.type];
assert.ok(handler);
handler(msg);
});

master.on('exit', common.mustCall((code, signal) => {
assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
}));

} else if (cluster.isMaster) {
const originalFork = patchedFork;
patchedFork = common.mustCall((...args) => {
process.send({ type: 'windowsHide', value: args[2].windowsHide });
return originalFork.apply(this, args);
});

cluster.setupMaster({
silient: true,
windowsHide: true
});

const worker = cluster.fork();
worker.on('exit', (code, signal) => {
process.send({ type: 'workerExit', code: code, signal: signal });
});

worker.on('online', (msg) => {
process.send({ type: 'workerOnline' });

let output = '0';
if (process.platform === 'win32')
output = child_process.execSync(
'powershell -NoProfile -c ' +
`"(Get-Process -Id ${worker.process.pid}).MainWindowHandle"`,
{ windowsHide: true, encoding: 'utf8' });

process.send({ type: 'mainWindowHandle', value: output });
worker.send('shutdown');
});

} else {
cluster.worker.on('message', (msg) => {
cluster.worker.disconnect();
});
}

0 comments on commit 45134fb

Please sign in to comment.