Files
HealthLog/src/app/api/health/route.ts
T
Marc-André Bombeck 846b0e5df0 perf(measurements): insert fresh batch samples set-based; answer health while starting
POST /api/measurements/batch ran one interactive reconciler transaction
per fresh sample: a savepoint, three advisory locks, two candidate reads
and the insert, about ten round trips. A 500-entry upload of new samples
was 500 of those, the 2 to 15 second outliers. Fresh samples now land
through INSERT ... ON CONFLICT DO NOTHING under the same per-account
advisory lock. Both measurement identities are full unique indexes, so
the statement inserts exactly what the reconciler would have inserted,
and every skipped row still goes through the reconciler for its own
verdict. Only rows whose external and natural keys each occur once in
the batch are buffered, and the buffer is flushed before any other row
is reconciled, so the order of effects stays the sequential one. The
contract and every per-entry status are unchanged; a golden test runs
each batch through both paths against the same table.

/api/health took seconds after a start because register() awaited the
whole pg-boss worker start (queues, schedules, boot discovery) before
the server took a request. The worker, or a web process's producer, now
starts in the background after the blocking boot checks; the probe
answers at once with reason "starting" until it is up, and gives the
database two seconds before calling it not answering.
2026-10-21 20:42:06 +02:00

103 lines
3.5 KiB
TypeScript

import { prisma } from "@/lib/db";
import { getWorkerStatus } from "@/lib/jobs/worker-status";
import { getGlobalBoss } from "@/lib/jobs/boss-instance";
import { shouldRunWeb, shouldRunWorker } from "@/lib/process-type";
import { getSession } from "@/lib/auth/session";
import { NextResponse } from "next/server";
import { apiHandler } from "@/lib/api-handler";
import { annotate } from "@/lib/logging/context";
import {
getKeyMismatchWarning,
isKeyMismatch,
KEY_MISMATCH_HEALTH_REASON,
} from "@/lib/boot/key-mismatch-state";
import { startupPhase } from "@/lib/boot/startup-state";
/**
* How long the probe waits for `SELECT 1`. During a boot storm the pool can
* be saturated by the worker's start-up work; a probe that waits its turn
* held a monitor for seconds. Past this the database counts as not answering
* and the probe says `degraded` at once.
*/
const DB_PROBE_TIMEOUT_MS = 2_000;
/** Reported while the background role is still coming up. */
const STARTING_HEALTH_REASON = "starting";
async function probeDatabase(): Promise<boolean> {
let timer: ReturnType<typeof setTimeout> | undefined;
try {
return await Promise.race([
prisma.$queryRaw`SELECT 1`.then(() => true),
new Promise<boolean>((resolve) => {
timer = setTimeout(() => resolve(false), DB_PROBE_TIMEOUT_MS);
}),
]);
} catch {
return false;
} finally {
if (timer) clearTimeout(timer);
}
}
export const dynamic = "force-dynamic";
export const GET = apiHandler(async () => {
annotate({ action: { name: "health.check" } });
const dbOk = await probeDatabase();
const worker = getWorkerStatus();
const producerReady = getGlobalBoss() !== null;
// A process whose encryption key cannot open the database refuses every
// other route; the probe says so in a word every caller may see, because
// it is the one place a catalog install's status page reads.
const keyMismatch = isKeyMismatch();
// Otherwise, while the background role is still starting, the 503 below
// says so: a transient state, not a fault, and not a hang.
const roleDown =
(shouldRunWeb() && !producerReady) ||
(shouldRunWorker() && !worker.running);
const reason = keyMismatch
? KEY_MISMATCH_HEALTH_REASON
: dbOk && roleDown && startupPhase() === "starting"
? STARTING_HEALTH_REASON
: undefined;
// Under ENCRYPTION_KEY_CHECK=warn the same finding does not refuse; it is
// named as a warning and leaves the status alone.
const warning =
!keyMismatch && getKeyMismatchWarning()
? KEY_MISMATCH_HEALTH_REASON
: undefined;
const status = !keyMismatch && dbOk && !roleDown ? "ok" : "degraded";
const statusCode = status === "ok" ? 200 : 503;
const cacheHeaders = {
"Cache-Control": "no-store, no-cache, must-revalidate",
};
// Only expose detailed info to authenticated admins
const session = await getSession().catch(() => null);
if (session?.user?.role === "ADMIN") {
return NextResponse.json(
{
status,
...(reason ? { reason } : {}),
...(warning ? { warning } : {}),
timestamp: new Date().toISOString(),
database: dbOk ? "connected" : "disconnected",
worker: worker.running ? "running" : "stopped",
...(worker.lastHeartbeat
? { workerLastHeartbeat: worker.lastHeartbeat }
: {}),
},
{ status: statusCode, headers: cacheHeaders },
);
}
return NextResponse.json(
{ status, ...(reason ? { reason } : {}), ...(warning ? { warning } : {}) },
{ status: statusCode, headers: cacheHeaders },
);
});