mirror of
https://github.com/nodejs/node.git
synced 2025-05-13 18:07:48 +00:00

PR-URL: https://github.com/nodejs/node/pull/42122 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Mestery <mestery@protonmail.com> Reviewed-By: Danielle Adams <adamzdanielle@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
48 lines
820 B
JavaScript
48 lines
820 B
JavaScript
const runningProcs = new Set()
|
|
let handlersInstalled = false
|
|
|
|
const forwardedSignals = [
|
|
'SIGINT',
|
|
'SIGTERM',
|
|
]
|
|
|
|
const handleSignal = signal => {
|
|
for (const proc of runningProcs) {
|
|
proc.kill(signal)
|
|
}
|
|
}
|
|
|
|
const setupListeners = () => {
|
|
for (const signal of forwardedSignals) {
|
|
process.on(signal, handleSignal)
|
|
}
|
|
handlersInstalled = true
|
|
}
|
|
|
|
const cleanupListeners = () => {
|
|
if (runningProcs.size === 0) {
|
|
for (const signal of forwardedSignals) {
|
|
process.removeListener(signal, handleSignal)
|
|
}
|
|
handlersInstalled = false
|
|
}
|
|
}
|
|
|
|
const add = proc => {
|
|
runningProcs.add(proc)
|
|
if (!handlersInstalled) {
|
|
setupListeners()
|
|
}
|
|
|
|
proc.once('exit', () => {
|
|
runningProcs.delete(proc)
|
|
cleanupListeners()
|
|
})
|
|
}
|
|
|
|
module.exports = {
|
|
add,
|
|
handleSignal,
|
|
forwardedSignals,
|
|
}
|