diff --git a/src/webapp/server.ts b/src/webapp/server.ts index 26ec1cf..2a371da 100644 --- a/src/webapp/server.ts +++ b/src/webapp/server.ts @@ -14,7 +14,13 @@ import { apps, isApp, appDir, isStaticApp, webappLog } from "./utils" export type Handler = (r: Context) => string | Child | Response | Promise export type App = Hono | Handler -const processes = new Map }>() +type ProcInfo = { + port: string, + proc: ReturnType, + killed?: boolean +} + +const processes = new Map() const restarting = new Set() const STARTING_PORT = 10000 let nextPort = STARTING_PORT @@ -67,8 +73,9 @@ async function startApp(name: string): Promise { stderr: "inherit", }) - processes.set(name, { port, proc }) - proc.exited.then(() => restartApp(name)) + const info: ProcInfo = { port, proc } + processes.set(name, info) + proc.exited.then(code => !info.killed && code !== 0 ? restartApp(name) : {}) await Bun.sleep(100) // give it time before first request @@ -89,13 +96,15 @@ export async function shutdownWebapps() { wwwWatcher?.close() nextPort = STARTING_PORT - for (const [name, { port, proc }] of processes) { + for (const [name, proc] of processes) { webappLog(name, "Shutting down") - try { proc.kill() } catch { } + try { + proc.killed = true + proc.proc.kill() + } catch { } } processes.clear() - process.exit(0) } async function restartApp(name: string) { @@ -107,7 +116,10 @@ async function restartApp(name: string) { webappLog(name, "restarting") const existing = processes.get(name) if (existing) { - try { existing.proc.kill() } catch { } + try { + existing.killed = true + existing.proc.kill() + } catch { } processes.delete(name) } @@ -121,7 +133,6 @@ async function startSubprocs() { await Promise.all(list.map(app => startApp(app))) } - let wwwWatcher: any function startWatcher() { if (!expectDir(NOSE_DIR)) return