diff --git a/src/index.ts b/src/index.ts index 6c0c930..e08a2ff 100644 --- a/src/index.ts +++ b/src/index.ts @@ -292,14 +292,34 @@ api.route("/search", searchRouter); api.route("/buffer-rules", bufferRulesRouter); api.route("/routes", routesRouter); +// Start the HTTP server first so /health and public routes are available immediately. +// Auth initialization runs afterward with retry — a transient DB ECONNRESET at boot +// must not crash the process (GRO-2652). Auth routes return 503 until initAuth succeeds. const port = Number(process.env.PORT ?? 3000); -await initAuth(); -console.log(`API server listening on port ${port}`); const server = serve({ fetch: app.fetch, port }); +console.log(`API server listening on port ${port}`); // Start background reminder scheduler (runs every minute to check for upcoming appointments) startReminderScheduler(); +let initAttempt = 0; +while (true) { + try { + await initAuth(); + break; + } catch (err) { + initAttempt++; + const delay = Math.min(2 ** initAttempt * 500, 30_000); + console.error(`[auth] initAuth attempt ${initAttempt} failed: ${err}`); + if (initAttempt >= 10) { + console.error("[auth] auth init permanently failed — auth endpoints will serve 503"); + break; + } + console.error(`[auth] retrying in ${delay}ms`); + await new Promise((r) => setTimeout(r, delay)); + } +} + function shutdown() { console.log("Shutting down gracefully..."); // SIGTERM/SIGINT → server.close() → callback → process.exit(0)