Use transactions

This commit is contained in:
Owen
2026-04-22 18:13:02 -07:00
parent dcbd22b4ad
commit 245755a140
10 changed files with 147 additions and 107 deletions

View File

@@ -18,7 +18,8 @@ import {
statusHistory,
targetHealthCheck,
targets,
resources
resources,
Transaction
} from "@server/db";
import { eq } from "drizzle-orm";
import {
@@ -46,10 +47,11 @@ export async function fireHealthCheckHealthyAlert(
healthCheckId: number,
healthCheckName?: string | null,
healthCheckTargetId?: number | null,
extra?: Record<string, unknown>
extra?: Record<string, unknown>,
trx: Transaction | typeof db = db
): Promise<void> {
try {
await db.insert(statusHistory).values({
await trx.insert(statusHistory).values({
entityType: "health_check",
entityId: healthCheckId,
orgId: orgId,
@@ -57,7 +59,7 @@ export async function fireHealthCheckHealthyAlert(
timestamp: Math.floor(Date.now() / 1000)
});
await handleResource(orgId, healthCheckTargetId);
await handleResource(orgId, healthCheckTargetId, trx);
await processAlerts({
eventType: "health_check_healthy",
@@ -102,10 +104,11 @@ export async function fireHealthCheckUnhealthyAlert(
healthCheckId: number,
healthCheckName?: string | null,
healthCheckTargetId?: number | null,
extra?: Record<string, unknown>
extra?: Record<string, unknown>,
trx: Transaction | typeof db = db
): Promise<void> {
try {
await db.insert(statusHistory).values({
await trx.insert(statusHistory).values({
entityType: "health_check",
entityId: healthCheckId,
orgId: orgId,
@@ -113,7 +116,7 @@ export async function fireHealthCheckUnhealthyAlert(
timestamp: Math.floor(Date.now() / 1000)
});
await handleResource(orgId, healthCheckTargetId);
await handleResource(orgId, healthCheckTargetId, trx);
await processAlerts({
eventType: "health_check_unhealthy",
@@ -142,12 +145,12 @@ export async function fireHealthCheckUnhealthyAlert(
}
}
async function handleResource(orgId: string, healthCheckTargetId?: number | null) {
async function handleResource(orgId: string, healthCheckTargetId?: number | null, trx: Transaction | typeof db = db) {
if (!healthCheckTargetId) {
return;
}
// we have resources lets get them
const [target] = await db
const [target] = await trx
.select()
.from(targets)
.where(eq(targets.targetId, healthCheckTargetId))
@@ -156,7 +159,7 @@ async function handleResource(orgId: string, healthCheckTargetId?: number | null
if (!target) {
return;
}
const [resource] = await db
const [resource] = await trx
.select()
.from(resources)
.where(eq(resources.resourceId, target.resourceId))
@@ -165,7 +168,7 @@ async function handleResource(orgId: string, healthCheckTargetId?: number | null
if (!resource) {
return;
}
const otherTargets = await db
const otherTargets = await trx
.select({ hcHealth: targetHealthCheck.hcHealth })
.from(targets)
.where(eq(targets.resourceId, resource.resourceId));
@@ -181,7 +184,7 @@ async function handleResource(orgId: string, healthCheckTargetId?: number | null
if (health != resource.health) {
// it changed
await db
await trx
.update(resources)
.set({ health })
.where(eq(resources.resourceId, resource.resourceId));
@@ -190,13 +193,17 @@ async function handleResource(orgId: string, healthCheckTargetId?: number | null
await fireResourceUnhealthyAlert(
orgId,
resource.resourceId,
resource.name
resource.name,
undefined,
trx
);
} else if (health === "healthy") {
await fireResourceHealthyAlert(
orgId,
resource.resourceId,
resource.name
resource.name,
undefined,
trx
);
}
}

View File

@@ -13,7 +13,7 @@
import logger from "@server/logger";
import { processAlerts } from "../processAlerts";
import { db, statusHistory } from "@server/db";
import { db, statusHistory, Transaction } from "@server/db";
// ---------------------------------------------------------------------------
// Public API
@@ -34,10 +34,11 @@ export async function fireResourceHealthyAlert(
orgId: string,
resourceId: number,
resourceName?: string | null,
extra?: Record<string, unknown>
extra?: Record<string, unknown>,
trx: Transaction | typeof db = db
): Promise<void> {
try {
await db.insert(statusHistory).values({
await trx.insert(statusHistory).values({
entityType: "resource",
entityId: resourceId,
orgId: orgId,
@@ -87,10 +88,11 @@ export async function fireResourceUnhealthyAlert(
orgId: string,
resourceId: number,
resourceName?: string | null,
extra?: Record<string, unknown>
extra?: Record<string, unknown>,
trx: Transaction | typeof db = db
): Promise<void> {
try {
await db.insert(statusHistory).values({
await trx.insert(statusHistory).values({
entityType: "resource",
entityId: resourceId,
orgId: orgId,
@@ -140,7 +142,8 @@ export async function fireResourceToggleAlert(
orgId: string,
resourceId: number,
resourceName?: string | null,
extra?: Record<string, unknown>
extra?: Record<string, unknown>,
trx: Transaction | typeof db = db
): Promise<void> {
try {
await processAlerts({

View File

@@ -13,7 +13,7 @@
import logger from "@server/logger";
import { processAlerts } from "../processAlerts";
import { db, sites, statusHistory, targetHealthCheck } from "@server/db";
import { db, sites, statusHistory, targetHealthCheck, Transaction } from "@server/db";
import { and, eq, inArray } from "drizzle-orm";
import { fireHealthCheckUnhealthyAlert } from "./healthCheckEvents";
@@ -36,10 +36,11 @@ export async function fireSiteOnlineAlert(
orgId: string,
siteId: number,
siteName?: string,
extra?: Record<string, unknown>
extra?: Record<string, unknown>,
trx: Transaction | typeof db = db
): Promise<void> {
try {
await db.insert(statusHistory).values({
await trx.insert(statusHistory).values({
entityType: "site",
entityId: siteId,
orgId: orgId,
@@ -89,10 +90,11 @@ export async function fireSiteOfflineAlert(
orgId: string,
siteId: number,
siteName?: string,
extra?: Record<string, unknown>
extra?: Record<string, unknown>,
trx: Transaction | typeof db = db
): Promise<void> {
try {
await db.insert(statusHistory).values({
await trx.insert(statusHistory).values({
entityType: "site",
entityId: siteId,
orgId: orgId,
@@ -100,7 +102,7 @@ export async function fireSiteOfflineAlert(
timestamp: Math.floor(Date.now() / 1000)
});
const unhealthyHealthChecks = await db
const unhealthyHealthChecks = await trx
.update(targetHealthCheck)
.set({ hcHealth: "unhealthy" })
.where(
@@ -119,7 +121,10 @@ export async function fireSiteOfflineAlert(
await fireHealthCheckUnhealthyAlert(
healthCheck.orgId,
healthCheck.targetHealthCheckId,
healthCheck.name
healthCheck.name,
undefined,
undefined,
trx
);
}