Update files
This commit is contained in:
@@ -1,63 +1,36 @@
|
||||
import { fork, type ChildProcess } from "child_process";
|
||||
import { fileURLToPath } from "url";
|
||||
import path from "path";
|
||||
import { existsSync } from "node:fs";
|
||||
import { runSupervisor } from "./supervisor.js";
|
||||
|
||||
const serverEntry = fileURLToPath(
|
||||
new URL("../src/server/index.ts", import.meta.url)
|
||||
);
|
||||
function resolveWorkerEntry(): string {
|
||||
const candidates = [
|
||||
fileURLToPath(new URL("../server/server/index.js", import.meta.url)),
|
||||
fileURLToPath(new URL("../dist/server/server/index.js", import.meta.url)),
|
||||
fileURLToPath(new URL("../src/server/index.ts", import.meta.url)),
|
||||
fileURLToPath(new URL("../../src/server/index.ts", import.meta.url)),
|
||||
];
|
||||
|
||||
let child: ChildProcess | null = null;
|
||||
let restarting = false;
|
||||
|
||||
function spawnServer() {
|
||||
child = fork(serverEntry, process.argv.slice(2), {
|
||||
stdio: "inherit",
|
||||
env: process.env,
|
||||
execArgv: ["--import", "tsx"],
|
||||
});
|
||||
|
||||
child.on("message", (msg: any) => {
|
||||
if (msg?.type === "paseo:restart") {
|
||||
restartServer();
|
||||
for (const candidate of candidates) {
|
||||
if (existsSync(candidate)) {
|
||||
return candidate;
|
||||
}
|
||||
});
|
||||
|
||||
child.on("exit", (code, signal) => {
|
||||
const exitDescriptor =
|
||||
signal ?? (typeof code === "number" ? `code ${code}` : "unknown");
|
||||
|
||||
// Restart on: explicit restart request, or any non-zero exit (crash)
|
||||
if (restarting || (code !== 0 && code !== null)) {
|
||||
restarting = false;
|
||||
process.stderr.write(`[DevRunner] Server exited (${exitDescriptor}). Restarting...\n`);
|
||||
spawnServer();
|
||||
return;
|
||||
}
|
||||
|
||||
process.stderr.write(`[DevRunner] Server exited (${exitDescriptor}). Shutting down.\n`);
|
||||
process.exit(0);
|
||||
});
|
||||
}
|
||||
|
||||
function restartServer() {
|
||||
if (!child || restarting) {
|
||||
return;
|
||||
}
|
||||
|
||||
restarting = true;
|
||||
process.stderr.write("[DevRunner] Restart requested. Stopping current server...\n");
|
||||
child.kill("SIGTERM");
|
||||
return candidates[0];
|
||||
}
|
||||
|
||||
function forwardSignal(signal: NodeJS.Signals) {
|
||||
if (!child) {
|
||||
process.exit(0);
|
||||
}
|
||||
child.kill(signal);
|
||||
function resolveWorkerExecArgv(): string[] {
|
||||
const workerEntry = resolveWorkerEntry();
|
||||
return workerEntry.endsWith(".ts") ? ["--import", "tsx"] : [];
|
||||
}
|
||||
|
||||
process.on("SIGINT", () => forwardSignal("SIGINT"));
|
||||
process.on("SIGTERM", () => forwardSignal("SIGTERM"));
|
||||
|
||||
process.stdout.write("[DevRunner] Starting server with tsx (explicit restarts only)\n");
|
||||
spawnServer();
|
||||
runSupervisor({
|
||||
name: "DevRunner",
|
||||
startupMessage: "Starting server worker (crash restarts enabled)",
|
||||
resolveWorkerEntry,
|
||||
workerArgs: process.argv.slice(2),
|
||||
workerEnv: process.env,
|
||||
workerExecArgv: resolveWorkerExecArgv(),
|
||||
restartOnCrash: true,
|
||||
shutdownReasons: ["cli_shutdown"],
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user