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.
63 lines
1.9 KiB
TypeScript
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 };
|
|
});
|
|
}
|