Files
cloudflare-domain-manager/apps/api/src/routes/health-check.ts
T
Denozordec 9783974949
Build and Push CFDM Docker Image / build-and-push (push) Successful in 2m0s
Build and Push CFDM Docker Image / create-release (push) Skipped
Build and Push CFDM Docker Image / update-wiki (push) Successful in 6s
feat(health-check): introduce health probe gap configuration and enhance health check logic
Added `healthProbeGapMs` configuration to control the minimum pause between probes to different physical targets. Updated health check service to utilize this configuration, ensuring efficient probing without overwhelming the targets. Enhanced the `runAllChecks` function to group probes by physical IP and implement the new gap logic. Updated related tests to validate the new functionality.
2026-07-20 03:57:37 +07:00

63 lines
1.9 KiB
TypeScript

import type { FastifyInstance } from "fastify";
import { healthStatusQuerySchema } from "@cfdm/shared";
import { repos } from "@cfdm/db";
import * as healthCheckService from "../services/health-check-service.js";
import * as serviceConfigService from "../services/service-config-service.js";
export async function healthCheckRoutes(app: FastifyInstance) {
app.get("/health-status", async (request) => {
const query = healthStatusQuerySchema.parse(request.query);
return healthCheckService.listStatus(
request.server.db,
query.scope,
query.ref_id,
);
});
app.post("/health-check/run", async (request) => {
const config = request.server.config;
const thresholds = {
degradedFailures: config.healthDegradedFailures,
downFailures: config.healthDownFailures,
latencyWarnMs: config.healthLatencyWarnMs,
};
const checked = await healthCheckService.runAllChecks(request.server.db, {
thresholds,
probeGapMs: config.healthProbeGapMs,
onStatusChange: async (target, prev, next) => {
try {
const label =
next === "up"
? "OK"
: next === "degraded"
? "Slow"
: next === "down"
? "Down"
: "—";
repos.insertNotificationLog(
request.server.db,
"ip_health",
target.scope,
target.ref_id,
`${target.hostname || target.ip}: ${label}`,
`IP ${target.ip}: ${prev ?? "—"} → ${label}`,
);
await serviceConfigService.reconcileDnsForTarget(
request.server.db,
request.server.cf,
target.scope,
target.ref_id,
);
} catch {
// best-effort
}
},
});
const monitors = await healthCheckService.runDomainMonitors(
request.server.db,
thresholds,
);
return { checked, monitors };
});
}