feat: correlate server errors and monitor application health

This commit is contained in:
syntaxbullet
2026-09-04 18:30:34 +02:00
parent 661bc6c346
commit 9c8a96ba03
10 changed files with 143 additions and 6 deletions

21
scripts/monitor.ts Normal file
View File

@@ -0,0 +1,21 @@
import { probeHealth, healthObserver } from "../src/ops/monitor";
const target = new URL(process.env.MONITOR_URL || `http://127.0.0.1:${process.env.PORT ?? 3000}/api/health`);
if (!["http:", "https:"].includes(target.protocol) || target.username || target.password) throw new Error("MONITOR_URL must be an HTTP(S) URL without credentials");
const emit = (event: Record<string, unknown>) => console.log(JSON.stringify(event));
if (process.argv.includes("--once")) {
const result = await probeHealth(target.href);
emit({ event: "health_probe", ...result });
process.exitCode = result.healthy ? 0 : 1;
} else {
const observe = healthObserver(emit);
let running = false;
const check = async () => {
if (running) return;
running = true;
try { observe(await probeHealth(target.href)); } finally { running = false; }
};
await check();
const timer = setInterval(() => void check(), 60000);
const stop = () => { clearInterval(timer); process.exit(0); };
process.on("SIGTERM", stop); process.on("SIGINT", stop);
}