diff --git a/web/lib/inngest/functions/rollout-helpers.ts b/web/lib/inngest/functions/rollout-helpers.ts index e67b32e..8d1440d 100644 --- a/web/lib/inngest/functions/rollout-helpers.ts +++ b/web/lib/inngest/functions/rollout-helpers.ts @@ -1,5 +1,5 @@ import { randomUUID } from "node:crypto"; -import { eq, inArray } from "drizzle-orm"; +import { and, eq, inArray } from "drizzle-orm"; import { db } from "@/db"; import { deploymentPorts, @@ -214,6 +214,27 @@ export async function prepareRollingUpdate( return { deploymentIds: runningDeployments.map((d) => d.id) }; } +export async function cleanupTerminalDeployments( + serviceId: string, +): Promise { + const terminalDeployments = await db + .select() + .from(deployments) + .where( + and( + eq(deployments.serviceId, serviceId), + inArray(deployments.status, ["rolled_back", "stopped", "failed"]), + ), + ); + + for (const dep of terminalDeployments) { + await db + .delete(deploymentPorts) + .where(eq(deploymentPorts.deploymentId, dep.id)); + await db.delete(deployments).where(eq(deployments.id, dep.id)); + } +} + export async function cleanupExistingDeployments( serviceId: string, ): Promise { diff --git a/web/lib/inngest/functions/rollout-workflow.ts b/web/lib/inngest/functions/rollout-workflow.ts index ae17eb2..14ea729 100644 --- a/web/lib/inngest/functions/rollout-workflow.ts +++ b/web/lib/inngest/functions/rollout-workflow.ts @@ -10,6 +10,7 @@ import { validateServers, prepareRollingUpdate, cleanupExistingDeployments, + cleanupTerminalDeployments, issueCertificatesForService, createDeploymentRecords, saveDeployedConfig, @@ -76,6 +77,10 @@ export const rolloutWorkflow = inngest.createFunction( return ids; }); + await step.run("cleanup-terminal-deployments", async () => { + await cleanupTerminalDeployments(serviceId); + }); + const isRollingUpdate = await step.run("check-rolling-update", async () => { return checkForRollingUpdate(serviceId); });