Repository navigation
Expand file tree
/
Copy pathheavy-command.mjs
More file actions
103 lines (101 loc) · 3.11 KB
/
Copy pathheavy-command.mjs
File metadata and controls
103 lines (101 loc) · 3.11 KB
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
// Supervise command lifetimes without serializing independent invocations.
import { spawn } from "node:child_process";
import { constants } from "node:os";
import { fileURLToPath, pathToFileURL } from "node:url";
export async function runSupervised(command, args, options = {}) {
const {
label = "command",
stdio = "inherit",
cwd,
env = process.env,
signal,
exitWithParent = false,
log = (line) => process.stderr.write(line + "\n"),
} = options;
if (signal?.aborted) return 130;
const supervised = process.platform === "linux";
const monitorParent = !supervised && exitWithParent;
const config = { label, parentPid: process.pid };
return new Promise((resolve) => {
const child = spawn(
supervised ? "python3" : command,
supervised
? [
fileURLToPath(new URL("./heavy-supervisor.py", import.meta.url)),
JSON.stringify(config),
command,
...args,
]
: monitorParent
? [
"--import",
new URL("./exit-with-parent.mjs", import.meta.url).href,
...args,
]
: args,
{
cwd,
env,
stdio: supervised
? [stdio, stdio, stdio, "pipe"]
: monitorParent
? [stdio, stdio, stdio, "ipc"]
: stdio,
},
);
let lines = "";
child.stdio[3]?.on("data", (data) => {
lines += data;
let end;
while ((end = lines.indexOf("\n")) !== -1) {
log(lines.slice(0, end));
lines = lines.slice(end + 1);
}
});
const forward = (name) => {
if (monitorParent && process.platform === "win32") {
if (child.connected) child.disconnect();
} else child.kill(name);
};
const handlers = ["SIGINT", "SIGTERM", "SIGHUP"].map((name) => [
name,
() => forward(name),
]);
const abort = () => forward("SIGTERM");
for (const [name, handler] of handlers) process.on(name, handler);
signal?.addEventListener("abort", abort, { once: true });
if (signal?.aborted) abort();
const cleanup = () => {
for (const [name, handler] of handlers) process.off(name, handler);
signal?.removeEventListener("abort", abort);
};
child.once("error", (error) => {
cleanup();
log(
`${label}: not started: ${error.message}${supervised ? "; Linux command supervision requires Python 3.8+" : ""}`,
);
resolve(127);
});
child.once("close", (code, killedBy) => {
cleanup();
if (lines) log(lines);
resolve(code ?? 128 + (constants.signals[killedBy] ?? 1));
});
});
}
if (
process.argv[1] &&
import.meta.url === pathToFileURL(process.argv[1]).href
) {
const args = process.argv.slice(2);
const browserDispatch = args[0] === "--browser-dispatch";
if (browserDispatch) args.shift();
if (!args.length)
throw new Error(
"Usage: node scripts/heavy-command.mjs [--browser-dispatch] [node options] script [arguments]",
);
process.exitCode = await runSupervised(process.execPath, args, {
label: "browser smoke",
exitWithParent: browserDispatch,
});
}