Just use the targetHealthCheckId as the id

This commit is contained in:
Owen
2026-04-21 14:56:25 -07:00
parent dc299a740b
commit ff1ca7eafb
4 changed files with 31 additions and 96 deletions

View File

@@ -141,6 +141,7 @@ export async function updateProxyResources(
.insert(targetHealthCheck) .insert(targetHealthCheck)
.values({ .values({
name: `${targetData.hostname}:${targetData.port}`, name: `${targetData.hostname}:${targetData.port}`,
siteId: site.siteId,
targetId: newTarget.targetId, targetId: newTarget.targetId,
orgId: orgId, orgId: orgId,
hcEnabled: healthcheckData?.enabled || false, hcEnabled: healthcheckData?.enabled || false,

View File

@@ -21,7 +21,6 @@ import {
generateSubnetProxyTargetV2, generateSubnetProxyTargetV2,
SubnetProxyTargetV2 SubnetProxyTargetV2
} from "@server/lib/ip"; } from "@server/lib/ip";
import { supportsTargetHealthChecksV2 } from "./targets";
export async function buildClientConfigurationForNewtClient( export async function buildClientConfigurationForNewtClient(
site: Site, site: Site,
@@ -232,12 +231,7 @@ export async function buildTargetConfigurationForNewtClient(
hcUnhealthyThreshold: targetHealthCheck.hcUnhealthyThreshold hcUnhealthyThreshold: targetHealthCheck.hcUnhealthyThreshold
}) })
.from(targetHealthCheck) .from(targetHealthCheck)
.where( .where(eq(targetHealthCheck.siteId, siteId));
and(
eq(targetHealthCheck.siteId, siteId),
eq(targetHealthCheck.hcEnabled, true)
)
);
const { tcpTargets, udpTargets } = allTargets.reduce( const { tcpTargets, udpTargets } = allTargets.reduce(
(acc, target) => { (acc, target) => {
@@ -285,9 +279,7 @@ export async function buildTargetConfigurationForNewtClient(
} }
return { return {
id: supportsTargetHealthChecksV2(version) id: target.targetHealthCheckId,
? target.targetId
: target.targetHealthCheckId,
hcEnabled: target.hcEnabled, hcEnabled: target.hcEnabled,
hcPath: target.hcPath, hcPath: target.hcPath,
hcScheme: target.hcScheme, hcScheme: target.hcScheme,

View File

@@ -2,15 +2,6 @@ import { Target, TargetHealthCheck } from "@server/db";
import { sendToClient } from "#dynamic/routers/ws"; import { sendToClient } from "#dynamic/routers/ws";
import logger from "@server/logger"; import logger from "@server/logger";
import { canCompress } from "@server/lib/clientVersionChecks"; import { canCompress } from "@server/lib/clientVersionChecks";
import semver from "semver";
const NEWT_V2_TARGET_HEALTH_CHECK_VERSION = ">=1.12.0";
export function supportsTargetHealthChecksV2(version?: string | null) {
return version
? semver.satisfies(version, NEWT_V2_TARGET_HEALTH_CHECK_VERSION)
: false;
}
export async function addTargets( export async function addTargets(
newtId: string, newtId: string,
@@ -92,9 +83,7 @@ export async function addTargets(
} }
return { return {
id: supportsTargetHealthChecksV2(version) id: hc.targetHealthCheckId,
? target.targetId
: hc.targetHealthCheckId,
hcEnabled: hc.hcEnabled, hcEnabled: hc.hcEnabled,
hcPath: hc.hcPath, hcPath: hc.hcPath,
hcScheme: hc.hcScheme, hcScheme: hc.hcScheme,

View File

@@ -14,7 +14,6 @@ import {
fireHealthCheckHealthyAlert, fireHealthCheckHealthyAlert,
fireHealthCheckNotHealthyAlert fireHealthCheckNotHealthyAlert
} from "#dynamic/lib/alerts"; } from "#dynamic/lib/alerts";
import { supportsTargetHealthChecksV2 } from "@server/routers/newt/targets";
interface TargetHealthStatus { interface TargetHealthStatus {
status: string; status: string;
@@ -74,8 +73,6 @@ export const handleHealthcheckStatusMessage: MessageHandler = async (
let successCount = 0; let successCount = 0;
let errorCount = 0; let errorCount = 0;
const isV2 = supportsTargetHealthChecksV2(newt.version);
// Process each target status update // Process each target status update
for (const [targetId, healthStatus] of Object.entries(data.targets)) { for (const [targetId, healthStatus] of Object.entries(data.targets)) {
logger.debug( logger.debug(
@@ -91,78 +88,34 @@ export const handleHealthcheckStatusMessage: MessageHandler = async (
continue; continue;
} }
let targetCheck: { const [targetCheck] = await db
targetId: number; .select({
siteId: number | null; targetId: targets.targetId,
orgId: string | null; siteId: targets.siteId,
targetHealthCheckId: number; orgId: targetHealthCheck.orgId,
resourceOrgId: string | null; targetHealthCheckId: targetHealthCheck.targetHealthCheckId,
resourceId: number | null; resourceOrgId: resources.orgId,
name: string | null; resourceId: resources.resourceId,
hcStatus: string | null; name: targetHealthCheck.name,
} | undefined; hcStatus: targetHealthCheck.hcHealth
})
if (isV2) { .from(targetHealthCheck)
// New newt (>= 1.12.0): the key is the targetId .innerJoin(
[targetCheck] = await db targets,
.select({ eq(targetHealthCheck.targetId, targets.targetId)
targetId: targets.targetId, )
siteId: targets.siteId, .innerJoin(
orgId: targetHealthCheck.orgId, resources,
targetHealthCheckId: targetHealthCheck.targetHealthCheckId, eq(targets.resourceId, resources.resourceId)
resourceOrgId: resources.orgId, )
resourceId: resources.resourceId, .innerJoin(sites, eq(targets.siteId, sites.siteId))
name: targetHealthCheck.name, .where(
hcStatus: targetHealthCheck.hcHealth and(
}) eq(targetHealthCheck.targetHealthCheckId, targetIdNum),
.from(targets) eq(sites.siteId, newt.siteId)
.innerJoin(
resources,
eq(targets.resourceId, resources.resourceId)
) )
.innerJoin(sites, eq(targets.siteId, sites.siteId)) )
.innerJoin( .limit(1);
targetHealthCheck,
eq(targets.targetId, targetHealthCheck.targetId)
)
.where(
and(
eq(targets.targetId, targetIdNum),
eq(sites.siteId, newt.siteId)
)
)
.limit(1);
} else {
// Old newt (< 1.12.0): the key is the targetHealthCheckId
[targetCheck] = await db
.select({
targetId: targets.targetId,
siteId: targets.siteId,
orgId: targetHealthCheck.orgId,
targetHealthCheckId: targetHealthCheck.targetHealthCheckId,
resourceOrgId: resources.orgId,
resourceId: resources.resourceId,
name: targetHealthCheck.name,
hcStatus: targetHealthCheck.hcHealth
})
.from(targetHealthCheck)
.innerJoin(
targets,
eq(targetHealthCheck.targetId, targets.targetId)
)
.innerJoin(
resources,
eq(targets.resourceId, resources.resourceId)
)
.innerJoin(sites, eq(targets.siteId, sites.siteId))
.where(
and(
eq(targetHealthCheck.targetHealthCheckId, targetIdNum),
eq(sites.siteId, newt.siteId)
)
)
.limit(1);
}
if (!targetCheck) { if (!targetCheck) {
logger.warn( logger.warn(