feat: correlate server errors and monitor application health
This commit is contained in:
21
scripts/monitor.ts
Normal file
21
scripts/monitor.ts
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user