diff --git a/server/private/lib/alerts/events/siteEvents.ts b/server/private/lib/alerts/events/siteEvents.ts index 76939b537..afb53f25f 100644 --- a/server/private/lib/alerts/events/siteEvents.ts +++ b/server/private/lib/alerts/events/siteEvents.ts @@ -112,7 +112,8 @@ export async function fireSiteOfflineAlert( .where( and( eq(targetHealthCheck.orgId, orgId), - eq(targetHealthCheck.siteId, siteId) + eq(targetHealthCheck.siteId, siteId), + eq(targetHealthCheck.hcEnabled, true) // only effect the ones that are enabled ) ) .returning(); @@ -126,7 +127,7 @@ export async function fireSiteOfflineAlert( healthCheck.orgId, healthCheck.targetHealthCheckId, healthCheck.name, - undefined, + healthCheck.targetId, // for the resource if we have one undefined, true, trx diff --git a/server/routers/target/handleHealthcheckStatusMessage.ts b/server/routers/target/handleHealthcheckStatusMessage.ts index 0fe5caf7b..e5f286524 100644 --- a/server/routers/target/handleHealthcheckStatusMessage.ts +++ b/server/routers/target/handleHealthcheckStatusMessage.ts @@ -1,7 +1,4 @@ -import { - db, - targetHealthCheck -} from "@server/db"; +import { db, primaryDb, targetHealthCheck } from "@server/db"; import { MessageHandler } from "@server/routers/ws"; import { Newt } from "@server/db"; import { eq, and, ne } from "drizzle-orm"; @@ -11,7 +8,6 @@ import { fireHealthCheckUnhealthyAlert } from "#dynamic/lib/alerts"; - interface TargetHealthStatus { status: string; lastCheck: string; @@ -85,13 +81,14 @@ export const handleHealthcheckStatusMessage: MessageHandler = async ( continue; } - const [targetCheck] = await db + const [targetCheck] = await primaryDb // using the primary db here in case it has just been updated and we are getting the immediate status back and it has not made it out to the repliacs yet .select({ targetId: targetHealthCheck.targetId, orgId: targetHealthCheck.orgId, targetHealthCheckId: targetHealthCheck.targetHealthCheckId, name: targetHealthCheck.name, - hcHealth: targetHealthCheck.hcHealth + hcHealth: targetHealthCheck.hcHealth, + hcEnabled: targetHealthCheck.hcEnabled }) .from(targetHealthCheck) .where( @@ -103,13 +100,20 @@ export const handleHealthcheckStatusMessage: MessageHandler = async ( .limit(1); if (!targetCheck) { - logger.warn( + logger.debug( `Target ${targetId} not found or does not belong to site ${newt.siteId}` ); errorCount++; continue; } + if (!targetCheck.hcEnabled) { + logger.debug( + `Health check for target ${targetId} is not enabled, skipping update` + ); + continue; + } + // check if the status has changed if (targetCheck.hcHealth === healthStatus.status) { logger.debug( @@ -128,7 +132,12 @@ export const handleHealthcheckStatusMessage: MessageHandler = async ( | "healthy" | "unhealthy" }) - .where(eq(targetHealthCheck.targetHealthCheckId, targetCheck.targetHealthCheckId)); + .where( + eq( + targetHealthCheck.targetHealthCheckId, + targetCheck.targetHealthCheckId + ) + ); // because we are checking above if there was a change we can fire the alert here because it changed if (healthStatus.status === "unhealthy") {