Skip to content

Commit 6a463cf

Browse files
committed
make sure we close stuff
1 parent 01d445b commit 6a463cf

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

dev-packages/node-overhead-gh-action/lib/getOverheadMeasurements.mjs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,40 @@ async function getMeasurements(instrumentFile) {
1717

1818
log(`Getting measurements for "${cmd}"`);
1919

20+
const [appProcess, appProcessClosed] = await startAppProcess(cmd);
21+
22+
log('Example app listening, running autocannon...');
23+
24+
try {
25+
return await startAutocannonProcess();
26+
} catch (error) {
27+
log(`Error running autocannon: ${error}`);
28+
} finally {
29+
appProcess.kill('SIGKILL');
30+
await appProcessClosed;
31+
}
32+
}
33+
34+
async function startAppProcess(cmd) {
2035
const appProcess = spawn(cmd, { shell: true });
2136

2237
log('Child process started, waiting for example app...');
2338

39+
// Promise to keep track of the app process being closed
40+
let resolveAppClose, rejectAppClose;
41+
const appClosePromise = new Promise((res, rej) => {
42+
resolveAppClose = res;
43+
rejectAppClose = rej;
44+
});
45+
46+
appProcess.on('close', code => {
47+
if (code !== 0) {
48+
rejectAppClose(new Error(`App process exited with code ${code}`));
49+
} else {
50+
resolveAppClose();
51+
}
52+
});
53+
2454
await new Promise((resolve, reject) => {
2555
appProcess.stdout.on('data', data => {
2656
log(`appProcess: ${data}`);
@@ -31,13 +61,15 @@ async function getMeasurements(instrumentFile) {
3161

3262
appProcess.stderr.on('data', data => {
3363
log(`appProcess stderr: ${data}`);
34-
reject(data);
3564
appProcess.kill('SIGKILL');
65+
reject(data);
3666
});
3767
});
3868

39-
log('Example app listening, running autocannon...');
69+
return [appProcess, appClosePromise];
70+
}
4071

72+
async function startAutocannonProcess() {
4173
const autocannon = spawn('yarn test', {
4274
shell: true,
4375
cwd: packageRoot,
@@ -62,11 +94,8 @@ async function getMeasurements(instrumentFile) {
6294

6395
autocannon.on('close', code => {
6496
log(`autocannon closed with code ${code}`);
65-
6697
log(`Average requests: ${lastJson?.requests.average}`);
6798

68-
appProcess.kill('SIGKILL');
69-
7099
resolve(lastJson?.requests.average);
71100
});
72101
});

dev-packages/node-overhead-gh-action/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,3 @@
3636
"extends": "../../package.json"
3737
}
3838
}
39-
40-

0 commit comments

Comments
 (0)