Files
cloudflare-domain-manager/apps/api/src/services/health/state-machine.ts
T
Denozordec 3f6f402872
quality / commitlint (push) Skipped
CD / update-wiki (push) Failing after 8s
quality / changes (push) Successful in 5s
quality / docker-check (push) Skipped
quality / web (push) Successful in 50s
quality / api (push) Successful in 41s
CD / quality (push) Successful in 1m40s
CD / publish (push) Successful in 1m33s
feat(health-checks): enhance health check functionality and add new routes
- Introduced origin health check routes and integrated them into the application.
- Updated health check configuration to include success recovery thresholds.
- Expanded error handling with new error codes for health check failures.
- Added new service routes for managing health checks, including creation and listing.
- Improved health check service logic to track consecutive successes and failures.

This commit enhances the health check capabilities, providing better monitoring and management of service health.
2026-08-19 12:26:12 +07:00

76 lines
2.0 KiB
TypeScript

import type { IpHealthState, NodeHealthState } from "@cfdm/shared";
export interface HealthThresholds {
degradedFailures: number;
downFailures: number;
successRecoveries: number;
latencyWarnMs: number;
}
export interface HealthCounters {
status: string;
consecutive_failures: number;
consecutive_successes?: number;
}
export interface NextHealth {
legacy: IpHealthState;
node: NodeHealthState;
failures: number;
successes: number;
}
function wasHealthy(status: string | undefined): boolean {
return status === "up" || status === "healthy";
}
function wasUnhealthy(status: string | undefined): boolean {
return (
status === "down" ||
status === "unhealthy" ||
status === "checking" ||
status === "degraded"
);
}
export function nextHealthState(
ok: boolean,
latencyMs: number,
prev: HealthCounters | null,
thresholds: HealthThresholds,
): NextHealth {
if (!ok) {
const failures = (prev?.consecutive_failures ?? 0) + 1;
if (failures >= thresholds.downFailures) {
return { legacy: "down", node: "unhealthy", failures, successes: 0 };
}
return { legacy: "degraded", node: "degraded", failures, successes: 0 };
}
if (latencyMs > thresholds.latencyWarnMs) {
return { legacy: "degraded", node: "degraded", failures: 0, successes: 0 };
}
if (!prev || wasHealthy(prev.status) || !wasUnhealthy(prev.status)) {
return {
legacy: "up",
node: "healthy",
failures: 0,
successes: (prev?.consecutive_successes ?? 0) + 1,
};
}
const successes = (prev.consecutive_successes ?? 0) + 1;
if (successes >= thresholds.successRecoveries) {
return { legacy: "up", node: "healthy", failures: 0, successes };
}
return { legacy: "unknown", node: "checking", failures: 0, successes };
}
export function toLegacyHealth(status: NodeHealthState | IpHealthState): IpHealthState {
if (status === "healthy" || status === "up") return "up";
if (status === "unhealthy" || status === "down") return "down";
if (status === "degraded") return "degraded";
return "unknown";
}