|
| 1 | +import { name as APP_NAME, version as APP_VERSION } from "../../package.json"; |
| 2 | + |
| 3 | +import Logger from "./logger"; |
| 4 | + |
| 5 | +interface HealthState { |
| 6 | + ready: boolean; |
| 7 | +} |
| 8 | + |
| 9 | +interface HealthServerHandle { |
| 10 | + /** Flip ready=true once Discord has emitted ClientReady and crons are mounted. */ |
| 11 | + markReady(): void; |
| 12 | + /** ISO 8601 timestamp captured when the server started. The editor uses this to detect a fresh process across pm2 reloads. */ |
| 13 | + readonly startedAt: string; |
| 14 | +} |
| 15 | + |
| 16 | +const DEFAULT_HEALTH_PORT = 7475; |
| 17 | +const DEFAULT_HEALTH_HOST = "127.0.0.1"; |
| 18 | + |
| 19 | +function resolvePort(override?: number): number { |
| 20 | + if (typeof override === "number") return override; |
| 21 | + const fromEnv = Number.parseInt(process.env.HEALTH_PORT ?? "", 10); |
| 22 | + return Number.isFinite(fromEnv) ? fromEnv : DEFAULT_HEALTH_PORT; |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Tiny localhost-only health endpoint consumed by the sibling `azalea-editor` |
| 27 | + * service to verify a `pm2 reload` actually produced a healthy bot process. |
| 28 | + * |
| 29 | + * Returns 200 with `{ ready, pid, startedAt, name, version }`. `ready` flips |
| 30 | + * to true once {@link HealthServerHandle.markReady} is called (from the Ready |
| 31 | + * event, after crons are mounted). `startedAt` strictly advances across |
| 32 | + * process restarts, which is the editor's signal that a reload succeeded — |
| 33 | + * `pm2`'s own `restart_time` and `online` status both lie about reload |
| 34 | + * outcome. |
| 35 | + * |
| 36 | + * Bound to 127.0.0.1 by default; never expose this directly to the internet. |
| 37 | + */ |
| 38 | +export function startHealthServer(options: { |
| 39 | + port?: number; |
| 40 | + host?: string; |
| 41 | +} = {}): HealthServerHandle { |
| 42 | + const port = resolvePort(options.port); |
| 43 | + const host = options.host ?? process.env.HEALTH_HOST ?? DEFAULT_HEALTH_HOST; |
| 44 | + const startedAt = new Date().toISOString(); |
| 45 | + const state: HealthState = { ready: false }; |
| 46 | + |
| 47 | + Bun.serve({ |
| 48 | + port, |
| 49 | + hostname: host, |
| 50 | + fetch(request): Response { |
| 51 | + const url = new URL(request.url); |
| 52 | + if (url.pathname !== "/healthz") { |
| 53 | + return new Response("Not Found", { status: 404 }); |
| 54 | + } |
| 55 | + |
| 56 | + const body = JSON.stringify({ |
| 57 | + ready: state.ready, |
| 58 | + pid: process.pid, |
| 59 | + startedAt, |
| 60 | + name: APP_NAME, |
| 61 | + version: APP_VERSION |
| 62 | + }); |
| 63 | + |
| 64 | + const headers = new Headers(); |
| 65 | + headers.set("content-type", "application/json"); |
| 66 | + return new Response(body, { headers }); |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + Logger.info(`Health endpoint listening on http://${host}:${port}/healthz`); |
| 71 | + |
| 72 | + return { |
| 73 | + markReady(): void { |
| 74 | + state.ready = true; |
| 75 | + }, |
| 76 | + startedAt |
| 77 | + }; |
| 78 | +} |
0 commit comments