fix: compile TypeScript in Docker build — dist/ was never built in CI, connection resilience code was missing from images
This commit is contained in:
parent
95ca10175f
commit
e611609580
6 changed files with 183 additions and 50 deletions
41
dist/routes/health.js
vendored
41
dist/routes/health.js
vendored
|
|
@ -5,28 +5,37 @@ import { pool } from "../services/db.js";
|
|||
const require = createRequire(import.meta.url);
|
||||
const { version: APP_VERSION } = require("../../package.json");
|
||||
export const healthRouter = Router();
|
||||
const HEALTH_CHECK_TIMEOUT_MS = 3000;
|
||||
healthRouter.get("/", async (_req, res) => {
|
||||
const poolStats = getPoolStats();
|
||||
let databaseStatus;
|
||||
let overallStatus = "ok";
|
||||
let httpStatus = 200;
|
||||
// Check database connectivity
|
||||
// Check database connectivity with a real query and timeout
|
||||
try {
|
||||
const client = await pool.connect();
|
||||
try {
|
||||
const result = await client.query('SELECT version()');
|
||||
const version = result.rows[0]?.version || 'Unknown';
|
||||
// Extract just the PostgreSQL version number (e.g., "PostgreSQL 15.4")
|
||||
const versionMatch = version.match(/PostgreSQL ([\d.]+)/);
|
||||
const shortVersion = versionMatch ? `PostgreSQL ${versionMatch[1]}` : 'PostgreSQL';
|
||||
databaseStatus = {
|
||||
status: "ok",
|
||||
version: shortVersion
|
||||
};
|
||||
}
|
||||
finally {
|
||||
client.release();
|
||||
}
|
||||
const dbCheck = async () => {
|
||||
const client = await pool.connect();
|
||||
try {
|
||||
// Use SELECT 1 as a lightweight liveness probe
|
||||
await client.query('SELECT 1');
|
||||
const result = await client.query('SELECT version()');
|
||||
const version = result.rows[0]?.version || 'Unknown';
|
||||
const versionMatch = version.match(/PostgreSQL ([\d.]+)/);
|
||||
const shortVersion = versionMatch ? `PostgreSQL ${versionMatch[1]}` : 'PostgreSQL';
|
||||
client.release();
|
||||
return { status: "ok", version: shortVersion };
|
||||
}
|
||||
catch (queryErr) {
|
||||
// Destroy the bad connection so it doesn't go back to the pool
|
||||
try {
|
||||
client.release(true);
|
||||
}
|
||||
catch (_) { }
|
||||
throw queryErr;
|
||||
}
|
||||
};
|
||||
const timeout = new Promise((_resolve, reject) => setTimeout(() => reject(new Error("Database health check timed out")), HEALTH_CHECK_TIMEOUT_MS));
|
||||
databaseStatus = await Promise.race([dbCheck(), timeout]);
|
||||
}
|
||||
catch (error) {
|
||||
databaseStatus = {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue