-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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
cluster: support windowsHide option for workers #17412
Closed
Closed
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
'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(); | ||
}); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can get rid of the monkey-patch now because ultimately it doesn't how matter how the option is passed down, as long as the observable effect is the same.
Presumably the
child_process.spawn()
call can go too? I admit I don't quite understand why you run the tests in a child process.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I keep the monkey-patch just in case some one run the tests on other platforms or in a windows service session. Not sure if this is necessary or practically useful.
It seems Windows only allocate a new console window for an attaching process spawned by a detached process.
If process D is spawned by process C with
detached: true
, and process W is spawned by process D withdetached: false
, W will get a new black console window popped up.if D is spawned by C with
detached: false
or W is spawned by D withdetached: true
, no console window will pop up for W.C = Command, D = Daemon, W = worker, practically.
That's why the test need to be run in a (detached) child process.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bnoordhuis Is this OK?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neither, I'd say. :-)
Can you add a comment to the test that explains why it spawns a child process first? I get it now from your explanation but it's not obvious from just looking at the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, i'll add the comment, and remove the monkey-patch thing.