Skip to content

Commit 76805f0

Browse files
toddwongrvagg
authored andcommitted
cluster: support windowsHide option for workers
Fixes: #17370 PR-URL: #17412Fixes: #17370 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 7c43099 commit 76805f0

3 files changed

Lines changed: 79 additions & 0 deletions

File tree

‎doc/api/cluster.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,8 @@ changes:
756756
This can be a number, or a function that takes no arguments and returns a
757757
number. By default each worker gets its own port, incremented from the
758758
master's `process.debugPort`.
759+
*`windowsHide` {boolean} Hide the forked processes console window that would
760+
normally be created on Windows systems. **Default:**`false`
759761

760762
After calling `.setupMaster()` (or `.fork()`) this settings object will contain
761763
the settings, including the default values.

‎lib/internal/cluster/master.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ function createWorkerProcess(id, env) {
131131
returnfork(cluster.settings.exec,cluster.settings.args,{
132132
env: workerEnv,
133133
silent: cluster.settings.silent,
134+
windowsHide: cluster.settings.windowsHide,
134135
execArgv: execArgv,
135136
stdio: cluster.settings.stdio,
136137
gid: cluster.settings.gid,
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
'use strict';
2+
constcommon=require('../common');
3+
constassert=require('assert');
4+
constchild_process=require('child_process');
5+
constcluster=require('cluster');
6+
7+
if(!process.argv[2]){
8+
/* It seems Windows only allocate new console window for
9+
* attaching processes spawned by detached processes. i.e.
10+
* - If process D is spawned by process C with `detached: true`,
11+
* and process W is spawned by process D with `detached: false`,
12+
* W will get a new black console window popped up.
13+
* - If D is spawned by C with `detached: false` or W is spawned
14+
* by D with `detached: true`, no console window will pop up for W.
15+
*
16+
* So, we have to spawn a detached process first to run the actual test.
17+
*/
18+
constmaster=child_process.spawn(
19+
process.argv[0],
20+
[process.argv[1],'--cluster'],
21+
{detached: true,stdio: ['ignore','ignore','ignore','ipc']});
22+
23+
constmessageHandlers={
24+
workerOnline: common.mustCall((msg)=>{
25+
}),
26+
mainWindowHandle: common.mustCall((msg)=>{
27+
assert.ok(/0\s*/.test(msg.value));
28+
}),
29+
workerExit: common.mustCall((msg)=>{
30+
assert.strictEqual(msg.code,0);
31+
assert.strictEqual(msg.signal,null);
32+
})
33+
};
34+
35+
master.on('message',(msg)=>{
36+
consthandler=messageHandlers[msg.type];
37+
assert.ok(handler);
38+
handler(msg);
39+
});
40+
41+
master.on('exit',common.mustCall((code,signal)=>{
42+
assert.strictEqual(code,0);
43+
assert.strictEqual(signal,null);
44+
}));
45+
46+
}elseif(cluster.isMaster){
47+
cluster.setupMaster({
48+
silient: true,
49+
windowsHide: true
50+
});
51+
52+
constworker=cluster.fork();
53+
worker.on('exit',(code,signal)=>{
54+
process.send({type: 'workerExit',code: code,signal: signal});
55+
});
56+
57+
worker.on('online',(msg)=>{
58+
process.send({type: 'workerOnline'});
59+
60+
letoutput='0';
61+
if(process.platform==='win32'){
62+
output=child_process.execSync(
63+
'powershell -NoProfile -c '+
64+
`"(Get-Process -Id ${worker.process.pid}).MainWindowHandle"`,
65+
{windowsHide: true,encoding: 'utf8'});
66+
}
67+
68+
process.send({type: 'mainWindowHandle',value: output});
69+
worker.send('shutdown');
70+
});
71+
72+
}else{
73+
cluster.worker.on('message',(msg)=>{
74+
cluster.worker.disconnect();
75+
});
76+
}

0 commit comments

Comments
 (0)