Skip to content

Commit a511f4d

Browse files
committed
refactor(deployments): unify old deployment clearing logic for applications and composes
- Renamed and consolidated the functions for clearing old deployments to a single method, `clearOldDeployments`, which now accepts an ID and type (application or compose). - Updated the logic to filter deployments based on status and type, improving code maintainability and reducing redundancy.
1 parent 95a944c commit a511f4d

3 files changed

Lines changed: 14 additions & 102 deletions

File tree

apps/dokploy/server/api/routers/application.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
addNewService,
33
checkServiceAccess,
4-
clearOldDeploymentsByApplicationId,
4+
clearOldDeployments,
55
createApplication,
66
deleteAllMiddlewares,
77
findApplicationById,
@@ -761,9 +761,7 @@ export const applicationRouter = createTRPCRouter({
761761
"You are not authorized to clear deployments for this application",
762762
});
763763
}
764-
const result = await clearOldDeploymentsByApplicationId(
765-
input.applicationId,
766-
);
764+
const result = await clearOldDeployments(input.applicationId);
767765
return {
768766
success: true,
769767
message: `${result.deletedCount} old deployments cleared successfully`,

apps/dokploy/server/api/routers/compose.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
addDomainToCompose,
33
addNewService,
44
checkServiceAccess,
5-
clearOldDeploymentsByComposeId,
5+
clearOldDeployments,
66
cloneCompose,
77
createCommand,
88
createCompose,
@@ -278,7 +278,7 @@ export const composeRouter = createTRPCRouter({
278278
"You are not authorized to clear deployments for this compose",
279279
});
280280
}
281-
const result = await clearOldDeploymentsByComposeId(input.composeId);
281+
const result = await clearOldDeployments(input.composeId, "compose");
282282
return {
283283
success: true,
284284
message: `${result.deletedCount} old deployments cleared successfully`,

packages/server/src/services/deployment.ts

Lines changed: 10 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
} from "@dokploy/server/utils/process/execAsync";
2020
import { TRPCError } from "@trpc/server";
2121
import { format } from "date-fns";
22-
import { desc, eq } from "drizzle-orm";
22+
import { and, desc, eq, or } from "drizzle-orm";
2323
import {
2424
type Application,
2525
findApplicationById,
@@ -852,110 +852,24 @@ export const findAllDeploymentsByServerId = async (serverId: string) => {
852852
return deploymentsList;
853853
};
854854

855-
export const clearOldDeploymentsByApplicationId = async (
856-
applicationId: string,
855+
export const clearOldDeployments = async (
856+
id: string,
857+
type: "application" | "compose" = "application",
857858
) => {
858859
// Get all deployments ordered by creation date (newest first)
859860
const deploymentsList = await db.query.deployments.findMany({
860-
where: eq(deployments.applicationId, applicationId),
861-
orderBy: desc(deployments.createdAt),
862-
});
863-
864-
// Find the most recent successful deployment (status "done")
865-
const activeDeployment = deploymentsList.find(
866-
(deployment) => deployment.status === "done",
867-
);
868-
869-
// If there's an active deployment, keep it and remove all others
870-
// If there's no active deployment, keep the most recent one and remove the rest
871-
const deploymentsToKeep: string[] = [];
872-
873-
if (activeDeployment) {
874-
deploymentsToKeep.push(activeDeployment.deploymentId);
875-
} else if (deploymentsList.length > 0) {
876-
// Keep the most recent deployment even if it's not "done"
877-
deploymentsToKeep.push(deploymentsList[0]!.deploymentId);
878-
}
879-
880-
const deploymentsToDelete = deploymentsList.filter(
881-
(deployment) => !deploymentsToKeep.includes(deployment.deploymentId),
882-
);
883-
884-
// Delete old deployments and their log files
885-
for (const deployment of deploymentsToDelete) {
886-
if (deployment.rollbackId) {
887-
await removeRollbackById(deployment.rollbackId);
888-
}
889-
890-
// Remove log file if it exists
891-
const logPath = deployment.logPath;
892-
if (logPath && logPath !== "." && existsSync(logPath)) {
893-
try {
894-
await fsPromises.unlink(logPath);
895-
} catch (error) {
896-
console.error(`Error removing log file ${logPath}:`, error);
897-
}
898-
}
899-
900-
// Delete deployment from database
901-
await removeDeployment(deployment.deploymentId);
902-
}
903-
904-
return {
905-
deletedCount: deploymentsToDelete.length,
906-
keptDeployment: deploymentsToKeep[0] || null,
907-
};
908-
};
909-
910-
export const clearOldDeploymentsByComposeId = async (composeId: string) => {
911-
// Get all deployments ordered by creation date (newest first)
912-
const deploymentsList = await db.query.deployments.findMany({
913-
where: eq(deployments.composeId, composeId),
861+
where: and(
862+
eq(deployments[`${type}Id`], id),
863+
or(eq(deployments.status, "done"), eq(deployments.status, "error")),
864+
),
914865
orderBy: desc(deployments.createdAt),
915866
});
916867

917-
// Find the most recent successful deployment (status "done")
918-
const activeDeployment = deploymentsList.find(
919-
(deployment) => deployment.status === "done",
920-
);
921-
922-
// If there's an active deployment, keep it and remove all others
923-
// If there's no active deployment, keep the most recent one and remove the rest
924-
const deploymentsToKeep: string[] = [];
925-
926-
if (activeDeployment) {
927-
deploymentsToKeep.push(activeDeployment.deploymentId);
928-
} else if (deploymentsList.length > 0) {
929-
// Keep the most recent deployment even if it's not "done"
930-
deploymentsToKeep.push(deploymentsList[0]!.deploymentId);
931-
}
932-
933-
const deploymentsToDelete = deploymentsList.filter(
934-
(deployment) => !deploymentsToKeep.includes(deployment.deploymentId),
935-
);
936-
937-
// Delete old deployments and their log files
938-
for (const deployment of deploymentsToDelete) {
939-
if (deployment.rollbackId) {
940-
await removeRollbackById(deployment.rollbackId);
941-
}
942-
943-
// Remove log file if it exists
944-
const logPath = deployment.logPath;
945-
if (logPath && logPath !== "." && existsSync(logPath)) {
946-
try {
947-
await fsPromises.unlink(logPath);
948-
} catch (error) {
949-
console.error(`Error removing log file ${logPath}:`, error);
950-
}
951-
}
952-
953-
// Delete deployment from database
868+
for (const deployment of deploymentsList) {
954869
await removeDeployment(deployment.deploymentId);
955870
}
956871

957872
return {
958-
deletedCount: deploymentsToDelete.length,
959-
keptDeployment: deploymentsToKeep[0] || null,
873+
deletedCount: deploymentsList.length,
960874
};
961875
};

0 commit comments

Comments
 (0)