Filter out unhealthy targets

This commit is contained in:
Owen
2026-08-13 16:31:50 -04:00
parent f9ff3a5715
commit 3f0be4a39d
+14 -1
View File
@@ -6,6 +6,7 @@ import {
db, db,
exitNodes, exitNodes,
sites, sites,
targetHealthCheck,
targets targets
} from "@server/db"; } from "@server/db";
import config from "@server/lib/config"; import config from "@server/lib/config";
@@ -97,11 +98,16 @@ async function fetchProviderTargets(
port: targets.port, port: targets.port,
method: targets.method, method: targets.method,
exitNodeSubnet: sites.exitNodeSubnet, exitNodeSubnet: sites.exitNodeSubnet,
reachableAt: exitNodes.reachableAt reachableAt: exitNodes.reachableAt,
hcHealth: targetHealthCheck.hcHealth
}) })
.from(targets) .from(targets)
.innerJoin(sites, eq(targets.siteId, sites.siteId)) .innerJoin(sites, eq(targets.siteId, sites.siteId))
.innerJoin(exitNodes, eq(sites.exitNodeId, exitNodes.exitNodeId)) .innerJoin(exitNodes, eq(sites.exitNodeId, exitNodes.exitNodeId))
.leftJoin(
targetHealthCheck,
eq(targetHealthCheck.targetId, targets.targetId)
)
.where( .where(
and(eq(targets.providerId, providerId), eq(targets.enabled, true)) and(eq(targets.providerId, providerId), eq(targets.enabled, true))
); );
@@ -113,6 +119,13 @@ async function fetchProviderTargets(
if (!row.exitNodeSubnet || !row.reachableAt) { if (!row.exitNodeSubnet || !row.reachableAt) {
continue; continue;
} }
// A target with an active health check that's currently failing is
// taken out of rotation. No health check (null) or "unknown" (check
// hasn't run yet / hcEnabled is off) still routes normally, matching
// the convention in getTraefikConfig.ts's target selection.
if (row.hcHealth === "unhealthy") {
continue;
}
const host = row.exitNodeSubnet.split("/")[0]; const host = row.exitNodeSubnet.split("/")[0];
const port = row.internalPort ?? row.port; const port = row.internalPort ?? row.port;
const scheme = row.method?.toLowerCase() ?? "https"; const scheme = row.method?.toLowerCase() ?? "https";