[object Object]

← back to Timeout Agent

fix: catch DB init failure so server boots in degraded mode; add /api/health alias

f16f517f137336b688c2f370c4b1daeba6998936 · 2026-05-30 21:20:34 -0700 · Steve Abrams

- initDB() error no longer crashes the process before app.listen() fires;
  watchdog still works, logs fall back to in-memory until DB reconnects
- Added /api/health alias alongside existing /health for consistent probing

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

Files touched

Diff

commit f16f517f137336b688c2f370c4b1daeba6998936
Author: Steve Abrams <steve@designerwallcoverings.com>
Date:   Sat May 30 21:20:34 2026 -0700

    fix: catch DB init failure so server boots in degraded mode; add /api/health alias
    
    - initDB() error no longer crashes the process before app.listen() fires;
      watchdog still works, logs fall back to in-memory until DB reconnects
    - Added /api/health alias alongside existing /health for consistent probing
    
    Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
---
 server.js | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/server.js b/server.js
index 1827d55..692279e 100644
--- a/server.js
+++ b/server.js
@@ -917,9 +917,11 @@ app.get('/api/stats', async (req, res) => {
 });
 
 // ── Health Check ──
-app.get('/health', (req, res) => {
+function healthHandler(req, res) {
   res.json({ status: 'ok', agent: 'The Warden', port: PORT, uptime: process.uptime() });
-});
+}
+app.get('/health', healthHandler);
+app.get('/api/health', healthHandler);
 
 // ── Dashboard ──
 app.get('/', (req, res) => {
@@ -1159,7 +1161,15 @@ setInterval(refresh,15000);
 
 // ── Startup ──
 async function start() {
-  await initDB();
+  // DB init is non-fatal: the watchdog can still restart PM2 processes even
+  // if Postgres is temporarily unavailable. Log actions will fall back to
+  // in-memory recentActions until the DB comes back.
+  try {
+    await initDB();
+  } catch (dbErr) {
+    console.error('[Timeout] DB init failed (non-fatal, running without persistent logs):', dbErr.message);
+    broadcast('WARNING: DB unavailable — running in degraded mode (in-memory logs only)');
+  }
 
   // Seed prevRestartCounts so first check doesn't false-positive on historical restarts
   try {

← 2f254cd security: strip hardcoded dw_admin DSN password -> env-first  ·  back to Timeout Agent  ·  (newest)