Fix dev shutdown to stop orphaned services
This commit is contained in:
parent
4a22fcc635
commit
c8c8094594
2 changed files with 115 additions and 14 deletions
|
|
@ -12,12 +12,61 @@ type Child = {
|
|||
const children: Child[] = [];
|
||||
let shuttingDown = false;
|
||||
|
||||
const sleep = (delayMs: number): Promise<void> => {
|
||||
return new Promise((resolve) => setTimeout(resolve, delayMs));
|
||||
};
|
||||
|
||||
const waitForExit = async (proc: Bun.Subprocess, timeoutMs: number): Promise<boolean> => {
|
||||
const result = await Promise.race([
|
||||
proc.exited.then(() => true),
|
||||
sleep(timeoutMs).then(() => false)
|
||||
]);
|
||||
return result;
|
||||
};
|
||||
|
||||
const signalProcess = (pid: number, signal: NodeJS.Signals): boolean => {
|
||||
try {
|
||||
process.kill(-pid, signal);
|
||||
return true;
|
||||
} catch {
|
||||
try {
|
||||
process.kill(pid, signal);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const stopChild = async (child: Child, timeoutMs = 5000): Promise<void> => {
|
||||
const pid = child.process.pid;
|
||||
if (!pid) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!signalProcess(pid, "SIGINT")) {
|
||||
return;
|
||||
}
|
||||
|
||||
const exited = await waitForExit(child.process, timeoutMs);
|
||||
if (exited) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!signalProcess(pid, "SIGKILL")) {
|
||||
return;
|
||||
}
|
||||
|
||||
await waitForExit(child.process, 2000);
|
||||
};
|
||||
|
||||
const spawnChild = ({ name, cmd, cwd }: ChildSpec): void => {
|
||||
const proc = Bun.spawn(cmd, {
|
||||
cwd,
|
||||
stdin: "inherit",
|
||||
stdout: "inherit",
|
||||
stderr: "inherit"
|
||||
stderr: "inherit",
|
||||
detached: true
|
||||
});
|
||||
|
||||
children.push({ name, process: proc });
|
||||
|
|
@ -30,26 +79,26 @@ const spawnChild = ({ name, cmd, cwd }: ChildSpec): void => {
|
|||
const exitCode = code ?? 0;
|
||||
const statusLabel = exitCode === 0 ? "exited" : "failed";
|
||||
console.error(`[dev-services] ${name} ${statusLabel} (${exitCode})`);
|
||||
shutdown(exitCode);
|
||||
void shutdown(exitCode);
|
||||
});
|
||||
};
|
||||
|
||||
const shutdown = (code: number): void => {
|
||||
const shutdown = async (code: number): Promise<void> => {
|
||||
if (shuttingDown) {
|
||||
return;
|
||||
}
|
||||
|
||||
shuttingDown = true;
|
||||
|
||||
for (const child of children) {
|
||||
child.process.kill();
|
||||
if (children.length > 0) {
|
||||
await Promise.all(children.map((child) => stopChild(child)));
|
||||
}
|
||||
|
||||
process.exit(code);
|
||||
};
|
||||
|
||||
process.on("SIGINT", () => shutdown(0));
|
||||
process.on("SIGTERM", () => shutdown(0));
|
||||
process.on("SIGINT", () => void shutdown(0));
|
||||
process.on("SIGTERM", () => void shutdown(0));
|
||||
|
||||
const tasks: ChildSpec[] = [
|
||||
{ name: "ingest-options", cmd: ["bun", "run", "dev"], cwd: "services/ingest-options" },
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue