-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
processLauncher.ts
190 lines (167 loc) · 6.96 KB
/
processLauncher.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* Copyright 2017 Google Inc. All rights reserved.
* Modifications copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as childProcess from 'child_process';
import * as readline from 'readline';
import { helper } from './helper';
import * as types from './types';
import { isUnderTest, removeFolders } from '../utils/utils';
export type Env = {[key: string]: string | number | boolean | undefined};
export type LaunchProcessOptions = {
executablePath: string,
args: string[],
env?: Env,
handleSIGINT?: boolean,
handleSIGTERM?: boolean,
handleSIGHUP?: boolean,
stdio: 'pipe' | 'stdin',
tempDirectories: string[],
cwd?: string,
// Note: attemptToGracefullyClose should reject if it does not close the browser.
attemptToGracefullyClose: () => Promise<any>,
onExit: (exitCode: number | null, signal: string | null) => void,
log: (message: string) => void,
};
type LaunchResult = {
launchedProcess: childProcess.ChildProcess,
gracefullyClose: () => Promise<void>,
kill: () => Promise<void>,
};
const gracefullyCloseSet = new Set<() => Promise<void>>();
export async function gracefullyCloseAll() {
await Promise.all(Array.from(gracefullyCloseSet).map(gracefullyClose => gracefullyClose().catch(e => {})));
}
// We currently spawn a process per page when recording video in Chromium.
// This triggers "too many listeners" on the process object once you have more than 10 pages open.
const maxListeners = process.getMaxListeners();
if (maxListeners !== 0)
process.setMaxListeners(Math.max(maxListeners || 0, 100));
export async function launchProcess(options: LaunchProcessOptions): Promise<LaunchResult> {
const cleanup = () => removeFolders(options.tempDirectories);
const stdio: ('ignore' | 'pipe')[] = options.stdio === 'pipe' ? ['ignore', 'pipe', 'pipe', 'pipe', 'pipe'] : ['pipe', 'pipe', 'pipe'];
options.log(`<launching> ${options.executablePath} ${options.args.join(' ')}`);
const spawnedProcess = childProcess.spawn(
options.executablePath,
options.args,
{
// On non-windows platforms, `detached: true` makes child process a leader of a new
// process group, making it possible to kill child process tree with `.kill(-pid)` command.
// @see https://nodejs.org/api/child_process.html#child_process_options_detached
detached: process.platform !== 'win32',
env: (options.env as {[key: string]: string}),
cwd: options.cwd,
stdio,
}
);
// Prevent Unhandled 'error' event.
spawnedProcess.on('error', () => {});
if (!spawnedProcess.pid) {
let failed: (e: Error) => void;
const failedPromise = new Promise<Error>((f, r) => failed = f);
spawnedProcess.once('error', error => {
failed(new Error('Failed to launch: ' + error));
});
return cleanup().then(() => failedPromise).then(e => Promise.reject(e));
}
options.log(`<launched> pid=${spawnedProcess.pid}`);
const stdout = readline.createInterface({ input: spawnedProcess.stdout });
stdout.on('line', (data: string) => {
options.log(`[pid=${spawnedProcess.pid}][out] ` + data);
});
const stderr = readline.createInterface({ input: spawnedProcess.stderr });
stderr.on('line', (data: string) => {
options.log(`[pid=${spawnedProcess.pid}][err] ` + data);
});
let processClosed = false;
let fulfillClose = () => {};
const waitForClose = new Promise<void>(f => fulfillClose = f);
let fulfillCleanup = () => {};
const waitForCleanup = new Promise<void>(f => fulfillCleanup = f);
spawnedProcess.once('exit', (exitCode, signal) => {
options.log(`[pid=${spawnedProcess.pid}] <process did exit: exitCode=${exitCode}, signal=${signal}>`);
processClosed = true;
helper.removeEventListeners(listeners);
gracefullyCloseSet.delete(gracefullyClose);
options.onExit(exitCode, signal);
fulfillClose();
// Cleanup as process exits.
cleanup().then(fulfillCleanup);
});
const listeners = [ helper.addEventListener(process, 'exit', killProcess) ];
if (options.handleSIGINT) {
listeners.push(helper.addEventListener(process, 'SIGINT', () => {
gracefullyClose().then(() => {
// Give tests a chance to dispatch any async calls.
if (isUnderTest())
setTimeout(() => process.exit(130), 0);
else
process.exit(130);
});
}));
}
if (options.handleSIGTERM)
listeners.push(helper.addEventListener(process, 'SIGTERM', gracefullyClose));
if (options.handleSIGHUP)
listeners.push(helper.addEventListener(process, 'SIGHUP', gracefullyClose));
gracefullyCloseSet.add(gracefullyClose);
let gracefullyClosing = false;
async function gracefullyClose(): Promise<void> {
gracefullyCloseSet.delete(gracefullyClose);
// We keep listeners until we are done, to handle 'exit' and 'SIGINT' while
// asynchronously closing to prevent zombie processes. This might introduce
// reentrancy to this function, for example user sends SIGINT second time.
// In this case, let's forcefully kill the process.
if (gracefullyClosing) {
options.log(`[pid=${spawnedProcess.pid}] <forecefully close>`);
killProcess();
await waitForClose; // Ensure the process is dead and we called options.onkill.
return;
}
gracefullyClosing = true;
options.log(`[pid=${spawnedProcess.pid}] <gracefully close start>`);
await options.attemptToGracefullyClose().catch(() => killProcess());
await waitForCleanup; // Ensure the process is dead and we have cleaned up.
options.log(`[pid=${spawnedProcess.pid}] <gracefully close end>`);
}
// This method has to be sync to be used as 'exit' event handler.
function killProcess() {
options.log(`[pid=${spawnedProcess.pid}] <kill>`);
helper.removeEventListeners(listeners);
if (spawnedProcess.pid && !spawnedProcess.killed && !processClosed) {
// Force kill the browser.
try {
if (process.platform === 'win32')
childProcess.execSync(`taskkill /pid ${spawnedProcess.pid} /T /F`, { stdio: 'ignore' });
else
process.kill(-spawnedProcess.pid, 'SIGKILL');
} catch (e) {
// the process might have already stopped
}
}
cleanup();
}
function killAndWait() {
killProcess();
return waitForCleanup;
}
return { launchedProcess: spawnedProcess, gracefullyClose, kill: killAndWait };
}
export function envArrayToObject(env: types.EnvArray): Env {
const result: Env = {};
for (const { name, value } of env)
result[name] = value;
return result;
}