Wrap in transactions

This commit is contained in:
Owen
2026-04-27 13:46:01 -07:00
parent 512ba2150b
commit 61aaa5a832
7 changed files with 337 additions and 272 deletions

View File

@@ -17,6 +17,7 @@ export async function addTargets(
}:${target.port}`; }:${target.port}`;
}); });
if (payloadTargets.length > 0) {
await sendToClient( await sendToClient(
newtId, newtId,
{ {
@@ -25,8 +26,12 @@ export async function addTargets(
targets: payloadTargets targets: payloadTargets
} }
}, },
{ incrementConfigVersion: true, compress: canCompress(version, "newt") } {
incrementConfigVersion: true,
compress: canCompress(version, "newt")
}
); );
}
const healthCheckTargets = healthCheckData.map((hc) => { const healthCheckTargets = healthCheckData.map((hc) => {
// Ensure all necessary fields are present // Ensure all necessary fields are present
@@ -206,6 +211,7 @@ export async function removeTargets(
}:${target.port}`; }:${target.port}`;
}); });
if (payloadTargets.length > 0) {
await sendToClient( await sendToClient(
newtId, newtId,
{ {
@@ -216,6 +222,7 @@ export async function removeTargets(
}, },
{ incrementConfigVersion: true } { incrementConfigVersion: true }
); );
}
const healthCheckTargets = healthCheckData.map((hc) => { const healthCheckTargets = healthCheckData.map((hc) => {
return hc.targetHealthCheckId; return hc.targetHealthCheckId;

View File

@@ -98,7 +98,8 @@ export async function deleteResource(
await removeTargets( await removeTargets(
newt.newtId, newt.newtId,
[target], // [target],
[], // deleting the target from newt causes issues because we cant unbind the port. this needs to be fixed in newt before we can do this
[healthCheck], [healthCheck],
deletedResource.protocol, deletedResource.protocol,
newt.version newt.version

View File

@@ -19,7 +19,11 @@ import { eq } from "drizzle-orm";
import { pickPort } from "./helpers"; import { pickPort } from "./helpers";
import { isTargetValid } from "@server/lib/validators"; import { isTargetValid } from "@server/lib/validators";
import { OpenAPITags, registry } from "@server/openApi"; import { OpenAPITags, registry } from "@server/openApi";
import { fireHealthCheckHealthyAlert, fireHealthCheckUnhealthyAlert, fireHealthCheckUnknownAlert } from "#dynamic/lib/alerts"; import {
fireHealthCheckHealthyAlert,
fireHealthCheckUnhealthyAlert,
fireHealthCheckUnknownAlert
} from "#dynamic/lib/alerts";
const createTargetParamsSchema = z.strictObject({ const createTargetParamsSchema = z.strictObject({
resourceId: z.string().transform(Number).pipe(z.int().positive()) resourceId: z.string().transform(Number).pipe(z.int().positive())
@@ -142,7 +146,11 @@ export async function createTarget(
); );
} }
const existingTargets = await db let newTarget: Target[] = [];
let targetIps: string[] = [];
let healthCheck: TargetHealthCheck[] = [];
await db.transaction(async (trx) => {
const existingTargets = await trx
.select() .select()
.from(targets) .from(targets)
.where(eq(targets.resourceId, resourceId)); .where(eq(targets.resourceId, resourceId));
@@ -162,11 +170,8 @@ export async function createTarget(
); );
} }
let newTarget: Target[] = [];
let healthCheck: TargetHealthCheck[] = [];
let targetIps: string[] = [];
if (site.type == "local") { if (site.type == "local") {
newTarget = await db newTarget = await trx
.insert(targets) .insert(targets)
.values({ .values({
resourceId, resourceId,
@@ -188,10 +193,8 @@ export async function createTarget(
); );
} }
const { internalPort, targetIps: newTargetIps } = await pickPort( const { internalPort, targetIps: newTargetIps } =
site.siteId!, await pickPort(site.siteId!, trx);
db
);
if (!internalPort) { if (!internalPort) {
return next( return next(
@@ -202,7 +205,7 @@ export async function createTarget(
); );
} }
newTarget = await db newTarget = await trx
.insert(targets) .insert(targets)
.values({ .values({
resourceId, resourceId,
@@ -231,7 +234,7 @@ export async function createTarget(
hcHeaders = JSON.stringify(targetData.hcHeaders); hcHeaders = JSON.stringify(targetData.hcHeaders);
} }
healthCheck = await db healthCheck = await trx
.insert(targetHealthCheck) .insert(targetHealthCheck)
.values({ .values({
orgId: resource.orgId, orgId: resource.orgId,
@@ -254,7 +257,8 @@ export async function createTarget(
hcHealth: targetData.hcEnabled ? "unhealthy" : "unknown", hcHealth: targetData.hcEnabled ? "unhealthy" : "unknown",
hcTlsServerName: targetData.hcTlsServerName ?? null, hcTlsServerName: targetData.hcTlsServerName ?? null,
hcHealthyThreshold: targetData.hcHealthyThreshold ?? null, hcHealthyThreshold: targetData.hcHealthyThreshold ?? null,
hcUnhealthyThreshold: targetData.hcUnhealthyThreshold ?? null hcUnhealthyThreshold:
targetData.hcUnhealthyThreshold ?? null
}) })
.returning(); .returning();
@@ -265,7 +269,8 @@ export async function createTarget(
healthCheck[0].name, healthCheck[0].name,
undefined, undefined,
undefined, undefined,
false // dont send the alert because we just want to create the alert, not notify users yet false, // dont send the alert because we just want to create the alert, not notify users yet
trx
); );
} else if (healthCheck[0].hcHealth === "unknown") { } else if (healthCheck[0].hcHealth === "unknown") {
// if the health is unknown, we want to fire an alert to notify users to enable health checks // if the health is unknown, we want to fire an alert to notify users to enable health checks
@@ -275,7 +280,8 @@ export async function createTarget(
healthCheck[0].name, healthCheck[0].name,
undefined, undefined,
undefined, undefined,
false // dont send the alert because we just want to create the alert, not notify users yet false, // dont send the alert because we just want to create the alert, not notify users yet
trx
); );
} else if (healthCheck[0].hcHealth === "healthy") { } else if (healthCheck[0].hcHealth === "healthy") {
await fireHealthCheckHealthyAlert( await fireHealthCheckHealthyAlert(
@@ -284,9 +290,11 @@ export async function createTarget(
healthCheck[0].name, healthCheck[0].name,
undefined, undefined,
undefined, undefined,
false // dont send the alert because we just want to create the alert, not notify users yet false, // dont send the alert because we just want to create the alert, not notify users yet
trx
); );
} }
});
if (site.pubKey) { if (site.pubKey) {
if (site.type == "wireguard") { if (site.type == "wireguard") {

View File

@@ -2,15 +2,13 @@ import { Request, Response, NextFunction } from "express";
import { z } from "zod"; import { z } from "zod";
import { db } from "@server/db"; import { db } from "@server/db";
import { newts, resources, sites, targets } from "@server/db"; import { newts, resources, sites, targets } from "@server/db";
import { eq } from "drizzle-orm"; import { eq, ne, and } from "drizzle-orm";
import response from "@server/lib/response"; import response from "@server/lib/response";
import HttpCode from "@server/types/HttpCode"; import HttpCode from "@server/types/HttpCode";
import createHttpError from "http-errors"; import createHttpError from "http-errors";
import logger from "@server/logger"; import logger from "@server/logger";
import { addPeer } from "../gerbil/peers";
import { fromError } from "zod-validation-error"; import { fromError } from "zod-validation-error";
import { removeTargets } from "../newt/targets"; import { removeTargets } from "../newt/targets";
import { getAllowedIps } from "./helpers";
import { OpenAPITags, registry } from "@server/openApi"; import { OpenAPITags, registry } from "@server/openApi";
import { targetHealthCheck } from "@server/db/pg"; import { targetHealthCheck } from "@server/db/pg";
@@ -80,10 +78,29 @@ export async function deleteTarget(
); );
} }
// check if there are other targets on the resource
const otherTargets = await db
.select()
.from(targets)
.where(
and(
eq(targets.resourceId, resource.resourceId),
ne(targets.targetId, targetId)
)
);
if (otherTargets.length == 0) {
// set the resource status
await db
.update(resources)
.set({ health: "unknown" })
.where(eq(resources.resourceId, resource.resourceId));
}
const [site] = await db const [site] = await db
.select() .select()
.from(sites) .from(sites)
.where(eq(sites.siteId, targets.siteId)) .where(eq(sites.siteId, deletedTarget.siteId))
.limit(1); .limit(1);
if (!site) { if (!site) {
@@ -106,7 +123,8 @@ export async function deleteTarget(
await removeTargets( await removeTargets(
newt.newtId, newt.newtId,
[deletedTarget], // [deletedTarget],
[], // deleting the target from newt causes issues because we cant unbind the port. this needs to be fixed in newt before we can do this
[deletedHealthCheck], [deletedHealthCheck],
resource.protocol, resource.protocol,
newt.version newt.version

View File

@@ -10,12 +10,10 @@ import logger from "@server/logger";
import { fromError } from "zod-validation-error"; import { fromError } from "zod-validation-error";
import { addPeer } from "../gerbil/peers"; import { addPeer } from "../gerbil/peers";
import { addTargets } from "../newt/targets"; import { addTargets } from "../newt/targets";
import { fireHealthCheckHealthyAlert, fireHealthCheckUnknownAlert } from "#dynamic/lib/alerts"; import { fireHealthCheckHealthyAlert, fireHealthCheckUnknownAlert, fireHealthCheckUnhealthyAlert } from "#dynamic/lib/alerts";
import { pickPort } from "./helpers"; import { pickPort } from "./helpers";
import { isTargetValid } from "@server/lib/validators"; import { isTargetValid } from "@server/lib/validators";
import { OpenAPITags, registry } from "@server/openApi"; import { OpenAPITags, registry } from "@server/openApi";
import { fireHealthCheckUnhealthyAlert } from "@server/lib/alerts";
const updateTargetParamsSchema = z.strictObject({ const updateTargetParamsSchema = z.strictObject({
targetId: z.string().transform(Number).pipe(z.int().positive()) targetId: z.string().transform(Number).pipe(z.int().positive())
@@ -168,7 +166,10 @@ export async function updateTarget(
const pathMatchTypeRemoved = parsedBody.data.pathMatchType === null; const pathMatchTypeRemoved = parsedBody.data.pathMatchType === null;
const [updatedTarget] = await db let updatedTarget: any;
let updatedHc: any;
await db.transaction(async (trx) => {
[updatedTarget] = await trx
.update(targets) .update(targets)
.set({ .set({
siteId: parsedBody.data.siteId, siteId: parsedBody.data.siteId,
@@ -186,7 +187,7 @@ export async function updateTarget(
.where(eq(targets.targetId, targetId)) .where(eq(targets.targetId, targetId))
.returning(); .returning();
const [existingHc] = await db const [existingHc] = await trx
.select() .select()
.from(targetHealthCheck) .from(targetHealthCheck)
.where(eq(targetHealthCheck.targetId, targetId)) .where(eq(targetHealthCheck.targetId, targetId))
@@ -232,7 +233,7 @@ export async function updateTarget(
parsedBody.data.hcEnabled === null) && parsedBody.data.hcEnabled === null) &&
existingHc.hcEnabled === true; existingHc.hcEnabled === true;
const [updatedHc] = await db const [updatedHc] = await trx
.update(targetHealthCheck) .update(targetHealthCheck)
.set({ .set({
siteId: parsedBody.data.siteId, siteId: parsedBody.data.siteId,
@@ -264,7 +265,8 @@ export async function updateTarget(
updatedHc.name || "", updatedHc.name || "",
undefined, undefined,
undefined, undefined,
false // dont send the alert because we just want to create the alert, not notify users yet false, // dont send the alert because we just want to create the alert, not notify users yet
trx
); );
} else if (updatedHc.hcHealth === "unknown" && existingHc.hcHealth !== "unknown") { } else if (updatedHc.hcHealth === "unknown" && existingHc.hcHealth !== "unknown") {
// if the health is unknown, we want to fire an alert to notify users to enable health checks // if the health is unknown, we want to fire an alert to notify users to enable health checks
@@ -274,7 +276,8 @@ export async function updateTarget(
updatedHc.name, updatedHc.name,
undefined, undefined,
undefined, undefined,
false // dont send the alert because we just want to create the alert, not notify users yet false, // dont send the alert because we just want to create the alert, not notify users yet
trx
); );
} else if (updatedHc.hcHealth === "healthy" && existingHc.hcHealth !== "healthy") { } else if (updatedHc.hcHealth === "healthy" && existingHc.hcHealth !== "healthy") {
await fireHealthCheckHealthyAlert( await fireHealthCheckHealthyAlert(
@@ -283,9 +286,11 @@ export async function updateTarget(
updatedHc.name, updatedHc.name,
undefined, undefined,
undefined, undefined,
false // dont send the alert because we just want to create the alert, not notify users yet false, // dont send the alert because we just want to create the alert, not notify users yet
trx
); );
} }
});
if (site.pubKey) { if (site.pubKey) {
if (site.type == "wireguard") { if (site.type == "wireguard") {
@@ -310,6 +315,7 @@ export async function updateTarget(
); );
} }
} }
return response(res, { return response(res, {
data: { data: {
...updatedTarget, ...updatedTarget,

View File

@@ -62,6 +62,7 @@ import { formatAxiosError } from "@app/lib/api/formatAxiosError";
import { DockerManager, DockerState } from "@app/lib/docker"; import { DockerManager, DockerState } from "@app/lib/docker";
import { orgQueries, resourceQueries } from "@app/lib/queries"; import { orgQueries, resourceQueries } from "@app/lib/queries";
import { zodResolver } from "@hookform/resolvers/zod"; import { zodResolver } from "@hookform/resolvers/zod";
import { build } from "@server/build";
import { tlsNameSchema } from "@server/lib/schemas"; import { tlsNameSchema } from "@server/lib/schemas";
import { type GetResourceResponse } from "@server/routers/resource"; import { type GetResourceResponse } from "@server/routers/resource";
import type { ListSitesResponse } from "@server/routers/site"; import type { ListSitesResponse } from "@server/routers/site";
@@ -953,6 +954,18 @@ function ProxyResourceTargetsForm({
</Button> </Button>
</div> </div>
)} )}
{build === "saas" &&
targets.length > 1 &&
new Set(targets.map((t) => t.siteId)).size > 1 && (
<p className="text-sm text-muted-foreground mt-3 flex items-start gap-1.5">
<AlertTriangle className="h-4 w-4 shrink-0 mt-0.5" />
<span>
Round robin routing will not work between
sites that are not connected to the same
node, but failover will work.
</span>
</p>
)}
</SettingsSectionBody> </SettingsSectionBody>
<form className="self-end mt-4" action={formAction}> <form className="self-end mt-4" action={formAction}>

View File

@@ -1419,6 +1419,18 @@ export default function Page() {
</Button> </Button>
</div> </div>
)} )}
{build === "enterprise" &&
targets.length > 1 &&
new Set(targets.map((t) => t.siteId)).size > 1 && (
<p className="text-sm text-muted-foreground mt-3 flex items-start gap-1.5">
<InfoIcon className="h-4 w-4 shrink-0 mt-0.5" />
<span>
Round robin routing will not work between
sites that are not connected to the same
node, but failover will work.
</span>
</p>
)}
</SettingsSectionBody> </SettingsSectionBody>
</SettingsSection> </SettingsSection>