railyard/apps/console/lib/rail-status.ts
caelum0x c3b7edcf6d feat(console): GET /api/v1/rail/status, one aggregated health check for the rail
Consumer products (Rally, Menivor, Scribase, ...) and operators currently
have no single place to ask "is the rail actually up" -- they'd have to
probe Mint, Rago, Relay and Abacus separately before routing real traffic.

Adds lib/rail-status.ts (pure, injectable fetch per the repo's own transport
invariant) that checks every configured service in parallel with a bounded
per-service timeout, and GET /api/v1/rail/status that wires it to four new
optional env vars (MINT_HEALTH_URL, RAGO_HEALTH_URL, RELAY_HEALTH_URL,
ABACUS_HEALTH_URL -- unset services read "not_configured" and are excluded
from the verdict, so this degrades cleanly on any partial deployment).
Overall reads ok/degraded/down/unconfigured; HTTP status is 200 for
ok/degraded and 503 for down/unconfigured so a caller that only checks the
status code still gets the right answer without parsing the body.

TDD: 7 new tests in lib/rail-status.test.ts (all services healthy, one down
degrades the rest, a thrown network error reads down not an unhandled
rejection, a hung service times out instead of blocking forever, an unset
service skips the network call and is excluded from the verdict, zero
configured services reads unconfigured not a false ok, every service down
reads down). Full console suite: 166 pass (164 + the pre-existing env-only
failure in add-app-component.test.ts, unrelated -- missing root node_modules
in this sandbox, not a code regression).

Not deployed: the console Worker's deploy needs
~/.config/products-keys/railyard-console.env (not present on this machine)
and a working wrangler/pnpm toolchain (corepack's pnpm cache is broken here,
and the workspace root node_modules does not exist), so this could not be
shipped to railyard.ereboslab.com from this session -- noted in NOTES.md.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-21 18:39:35 +03:00

76 lines
3.0 KiB
TypeScript

// Railyard rail status: one aggregated health check across the money rail's
// own services (Mint, Rago, Relay, Abacus, ...) so a consumer product (or an
// operator) can ask ONE endpoint instead of probing four hosts directly
// before routing real traffic at the rail. Every service is independently
// optional -- with its URL unset it reads "not_configured" and is excluded
// from the overall verdict, so this works in any deployment that only has
// part of the rail live (invariant #5 in docs/RAILYARD_ENTERPRISE_ARCHITECTURE.md:
// all external I/O behind an injectable transport, fake in tests).
export interface RailService {
name: string;
url: string | null;
}
export type ServiceState = "ok" | "down" | "not_configured";
export interface ServiceStatus {
service: string;
state: ServiceState;
latency_ms: number | null;
http_status: number | null;
}
export interface RailStatusDeps {
services: readonly RailService[];
fetchImpl?: typeof fetch;
/** Per-service request timeout; a service that never answers reads "down", not hung. */
timeoutMs?: number;
now?: () => Date;
}
export type Overall = "ok" | "degraded" | "down" | "unconfigured";
export interface RailStatusResult {
checked_at: string;
overall: Overall;
services: ServiceStatus[];
}
const DEFAULT_TIMEOUT_MS = 3000;
async function checkOne(fetchImpl: typeof fetch, timeoutMs: number, svc: RailService): Promise<ServiceStatus> {
if (!svc.url) return { service: svc.name, state: "not_configured", latency_ms: null, http_status: null };
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), timeoutMs);
const started = Date.now();
try {
const res = await fetchImpl(svc.url, { signal: controller.signal });
return { service: svc.name, state: res.ok ? "ok" : "down", latency_ms: Date.now() - started, http_status: res.status };
} catch {
// Network error, DNS failure, connection refused or our own abort on
// timeout: all read the same to a caller deciding whether to route here.
return { service: svc.name, state: "down", latency_ms: Date.now() - started, http_status: null };
} finally {
clearTimeout(timer);
}
}
function overallOf(services: readonly ServiceStatus[]): Overall {
const configured = services.filter((s) => s.state !== "not_configured");
if (configured.length === 0) return "unconfigured";
const okCount = configured.filter((s) => s.state === "ok").length;
if (okCount === configured.length) return "ok";
if (okCount === 0) return "down";
return "degraded";
}
/** Check every service in parallel; never throws (a single bad host must not break the aggregate). */
export async function checkRailStatus(deps: RailStatusDeps): Promise<RailStatusResult> {
const fetchImpl = deps.fetchImpl ?? fetch;
const timeoutMs = deps.timeoutMs ?? DEFAULT_TIMEOUT_MS;
const now = deps.now ?? (() => new Date());
const services = await Promise.all(deps.services.map((s) => checkOne(fetchImpl, timeoutMs, s)));
return { checked_at: now().toISOString(), overall: overallOf(services), services };
}