fix restarting subprocs
This commit is contained in:
parent
6bc692c558
commit
06b73f90eb
|
|
@ -14,7 +14,13 @@ import { apps, isApp, appDir, isStaticApp, webappLog } from "./utils"
|
|||
export type Handler = (r: Context) => string | Child | Response | Promise<Response>
|
||||
export type App = Hono | Handler
|
||||
|
||||
const processes = new Map<string, { port: string, proc: ReturnType<typeof Bun.spawn> }>()
|
||||
type ProcInfo = {
|
||||
port: string,
|
||||
proc: ReturnType<typeof Bun.spawn>,
|
||||
killed?: boolean
|
||||
}
|
||||
|
||||
const processes = new Map<string, ProcInfo>()
|
||||
const restarting = new Set<string>()
|
||||
const STARTING_PORT = 10000
|
||||
let nextPort = STARTING_PORT
|
||||
|
|
@ -67,8 +73,9 @@ async function startApp(name: string): Promise<string | undefined> {
|
|||
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
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user