@specs-feup/lara
Version:
A js port of the popular framework for building source-to-source compilers
47 lines • 1.49 kB
JavaScript
import Debug from "debug";
const debug = Debug("ChildProcessHandling");
const activeChildProcesses = {};
export function getActiveChildProcesses() {
return activeChildProcesses;
}
export function addActiveChildProcess(child) {
if (child.pid === undefined) {
throw new Error("Child process doesn't have a pid");
}
const pid = child.pid;
activeChildProcesses[pid] = child;
// Listen for the 'exit' event of the child process
child.once("exit", () => {
delete activeChildProcesses[pid];
});
}
export function handleExit(exitProcess = true) {
const closingChildren = [];
for (const child of Object.values(activeChildProcesses)) {
const promise = new Promise((resolve) => {
child.once("exit", (code) => {
resolve(code);
});
});
closingChildren.push(promise);
child.kill();
}
Promise.all(closingChildren)
.then(() => {
debug("Closed all child processes");
if (exitProcess) {
process.exit();
}
})
.catch((err) => {
debug(`Error closing child processes: ${err.toString()}`);
});
}
export function listenForTerminationSignals() {
process.on("SIGINT", handleExit);
process.on("SIGTERM", handleExit);
process.on("SIGQUIT", handleExit);
process.on("uncaughtException", handleExit);
process.on("unhandledRejection", handleExit);
}
//# sourceMappingURL=ChildProcessHandling.js.map