diff --git a/src/server.ts b/src/server.ts index dfa15cf..d658ec8 100644 --- a/src/server.ts +++ b/src/server.ts @@ -7,6 +7,7 @@ const PORT = Number(process.env.PORT) || 3000 const SOCKET_PATH = DATA_DIR ? join(DATA_DIR, 'tronbyt.sock') : '' const BIN_DIR = join(import.meta.dir, '..', 'bin') +let goHealthy = false let goProcess: Subprocess | undefined interface WsData { @@ -22,6 +23,7 @@ function proxyFetch(req: Request): Promise | Response { const url = new URL(req.url) if (url.pathname === '/ok') { + if (!goHealthy) return new Response('starting', { status: goProcess ? 200 : 503 }) return fetch('http://localhost/health', { unix: SOCKET_PATH }) .then((r) => (r.ok ? new Response('ok') : new Response('unhealthy', { status: 503 }))) .catch(() => new Response('unhealthy', { status: 503 })) @@ -176,10 +178,12 @@ async function spawnGoServer() { goProcess.exited.then((code) => { console.log(`Tronbyt server exited with code ${code}`) + goHealthy = false goProcess = undefined }) if (await waitForHealthy()) { + goHealthy = true console.log('Tronbyt server is healthy') } else { console.error('Tronbyt server failed to become healthy')