Last change
on this file since 1ad8e64 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
812 bytes
|
Line | |
---|
1 | const runningProcs = new Set()
|
---|
2 | let handlersInstalled = false
|
---|
3 |
|
---|
4 | const forwardedSignals = [
|
---|
5 | 'SIGINT',
|
---|
6 | 'SIGTERM'
|
---|
7 | ]
|
---|
8 |
|
---|
9 | const handleSignal = signal => {
|
---|
10 | for (const proc of runningProcs) {
|
---|
11 | proc.kill(signal)
|
---|
12 | }
|
---|
13 | }
|
---|
14 |
|
---|
15 | const setupListeners = () => {
|
---|
16 | for (const signal of forwardedSignals) {
|
---|
17 | process.on(signal, handleSignal)
|
---|
18 | }
|
---|
19 | handlersInstalled = true
|
---|
20 | }
|
---|
21 |
|
---|
22 | const cleanupListeners = () => {
|
---|
23 | if (runningProcs.size === 0) {
|
---|
24 | for (const signal of forwardedSignals) {
|
---|
25 | process.removeListener(signal, handleSignal)
|
---|
26 | }
|
---|
27 | handlersInstalled = false
|
---|
28 | }
|
---|
29 | }
|
---|
30 |
|
---|
31 | const add = proc => {
|
---|
32 | runningProcs.add(proc)
|
---|
33 | if (!handlersInstalled)
|
---|
34 | setupListeners()
|
---|
35 |
|
---|
36 | proc.once('exit', () => {
|
---|
37 | runningProcs.delete(proc)
|
---|
38 | cleanupListeners()
|
---|
39 | })
|
---|
40 | }
|
---|
41 |
|
---|
42 | module.exports = {
|
---|
43 | add,
|
---|
44 | handleSignal,
|
---|
45 | forwardedSignals
|
---|
46 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.