Merge pull request #3917 from Dokploy/3752-an-error-have-occured-deployment-not-found

refactor: streamline deployment cleanup by consolidating removeLastTe…
This commit is contained in:
Mauricio Siu
2026-03-07 00:53:28 -06:00
committed by GitHub
+34 -21
View File
@@ -117,12 +117,12 @@ export const createDeployment = async (
>, >,
) => { ) => {
const application = await findApplicationById(deployment.applicationId); const application = await findApplicationById(deployment.applicationId);
try {
await removeLastTenDeployments( await removeLastTenDeployments(
deployment.applicationId, deployment.applicationId,
"application", "application",
application.serverId, application.serverId,
); );
try {
const serverId = application.buildServerId || application.serverId; const serverId = application.buildServerId || application.serverId;
const { LOGS_PATH } = paths(!!serverId); const { LOGS_PATH } = paths(!!serverId);
@@ -200,13 +200,12 @@ export const createDeploymentPreview = async (
const previewDeployment = await findPreviewDeploymentById( const previewDeployment = await findPreviewDeploymentById(
deployment.previewDeploymentId, deployment.previewDeploymentId,
); );
try {
await removeLastTenDeployments( await removeLastTenDeployments(
deployment.previewDeploymentId, deployment.previewDeploymentId,
"previewDeployment", "previewDeployment",
previewDeployment?.application?.serverId, previewDeployment?.application?.serverId,
); );
try {
const appName = `${previewDeployment.appName}`; const appName = `${previewDeployment.appName}`;
const { LOGS_PATH } = paths(!!previewDeployment?.application?.serverId); const { LOGS_PATH } = paths(!!previewDeployment?.application?.serverId);
const formattedDateTime = format(new Date(), "yyyy-MM-dd:HH:mm:ss"); const formattedDateTime = format(new Date(), "yyyy-MM-dd:HH:mm:ss");
@@ -281,12 +280,12 @@ export const createDeploymentCompose = async (
>, >,
) => { ) => {
const compose = await findComposeById(deployment.composeId); const compose = await findComposeById(deployment.composeId);
try {
await removeLastTenDeployments( await removeLastTenDeployments(
deployment.composeId, deployment.composeId,
"compose", "compose",
compose.serverId, compose.serverId,
); );
try {
const { LOGS_PATH } = paths(!!compose.serverId); const { LOGS_PATH } = paths(!!compose.serverId);
const formattedDateTime = format(new Date(), "yyyy-MM-dd:HH:mm:ss"); const formattedDateTime = format(new Date(), "yyyy-MM-dd:HH:mm:ss");
const fileName = `${compose.appName}-${formattedDateTime}.log`; const fileName = `${compose.appName}-${formattedDateTime}.log`;
@@ -369,8 +368,8 @@ export const createDeploymentBackup = async (
} else if (backup.backupType === "compose") { } else if (backup.backupType === "compose") {
serverId = backup.compose?.serverId; serverId = backup.compose?.serverId;
} }
try {
await removeLastTenDeployments(deployment.backupId, "backup", serverId); await removeLastTenDeployments(deployment.backupId, "backup", serverId);
try {
const { LOGS_PATH } = paths(!!serverId); const { LOGS_PATH } = paths(!!serverId);
const formattedDateTime = format(new Date(), "yyyy-MM-dd:HH:mm:ss"); const formattedDateTime = format(new Date(), "yyyy-MM-dd:HH:mm:ss");
const fileName = `${backup.appName}-${formattedDateTime}.log`; const fileName = `${backup.appName}-${formattedDateTime}.log`;
@@ -439,12 +438,12 @@ export const createDeploymentSchedule = async (
) => { ) => {
const schedule = await findScheduleById(deployment.scheduleId); const schedule = await findScheduleById(deployment.scheduleId);
try {
const serverId = const serverId =
schedule.application?.serverId || schedule.application?.serverId ||
schedule.compose?.serverId || schedule.compose?.serverId ||
schedule.server?.serverId; schedule.server?.serverId;
await removeLastTenDeployments(deployment.scheduleId, "schedule", serverId); await removeLastTenDeployments(deployment.scheduleId, "schedule", serverId);
try {
const { SCHEDULES_PATH } = paths(!!serverId); const { SCHEDULES_PATH } = paths(!!serverId);
const formattedDateTime = format(new Date(), "yyyy-MM-dd:HH:mm:ss"); const formattedDateTime = format(new Date(), "yyyy-MM-dd:HH:mm:ss");
const fileName = `${schedule.appName}-${formattedDateTime}.log`; const fileName = `${schedule.appName}-${formattedDateTime}.log`;
@@ -515,7 +514,6 @@ export const createDeploymentVolumeBackup = async (
) => { ) => {
const volumeBackup = await findVolumeBackupById(deployment.volumeBackupId); const volumeBackup = await findVolumeBackupById(deployment.volumeBackupId);
try {
const serverId = const serverId =
volumeBackup.application?.serverId || volumeBackup.compose?.serverId; volumeBackup.application?.serverId || volumeBackup.compose?.serverId;
await removeLastTenDeployments( await removeLastTenDeployments(
@@ -523,6 +521,7 @@ export const createDeploymentVolumeBackup = async (
"volumeBackup", "volumeBackup",
serverId, serverId,
); );
try {
const { VOLUME_BACKUPS_PATH } = paths(!!serverId); const { VOLUME_BACKUPS_PATH } = paths(!!serverId);
const formattedDateTime = format(new Date(), "yyyy-MM-dd:HH:mm:ss"); const formattedDateTime = format(new Date(), "yyyy-MM-dd:HH:mm:ss");
const fileName = `${volumeBackup.appName}-${formattedDateTime}.log`; const fileName = `${volumeBackup.appName}-${formattedDateTime}.log`;
@@ -601,24 +600,23 @@ export const removeDeployment = async (deploymentId: string) => {
.then((result) => result[0]); .then((result) => result[0]);
if (!deployment) { if (!deployment) {
throw new TRPCError({ return null;
code: "BAD_REQUEST",
message: "Deployment not found",
});
} }
const command = `
rm -f ${deployment.logPath}; const logPath = path.join(deployment.logPath);
`; if (logPath && logPath !== ".") {
const command = `rm -f ${logPath};`;
if (deployment.serverId) { if (deployment.serverId) {
await execAsyncRemote(deployment.serverId, command); await execAsyncRemote(deployment.serverId, command);
} else { } else {
await execAsync(command); await execAsync(command);
} }
}
return deployment; return deployment;
} catch (error) { } catch (error) {
const message = const message =
error instanceof Error ? error.message : "Error creating the deployment"; error instanceof Error ? error.message : "Error removing the deployment";
throw new TRPCError({ throw new TRPCError({
code: "BAD_REQUEST", code: "BAD_REQUEST",
message, message,
@@ -686,34 +684,49 @@ const removeLastTenDeployments = async (
if (serverId) { if (serverId) {
let command = ""; let command = "";
for (const oldDeployment of deploymentsToDelete) { for (const oldDeployment of deploymentsToDelete) {
try {
const logPath = path.join(oldDeployment.logPath); const logPath = path.join(oldDeployment.logPath);
if (oldDeployment.rollbackId) { if (oldDeployment.rollbackId) {
await removeRollbackById(oldDeployment.rollbackId); await removeRollbackById(oldDeployment.rollbackId);
} }
if (logPath !== ".") { if (logPath && logPath !== ".") {
command += ` command += `rm -rf ${logPath};`;
rm -rf ${logPath};
`;
} }
await removeDeployment(oldDeployment.deploymentId); await removeDeployment(oldDeployment.deploymentId);
} catch (err) {
console.error(
`Failed to remove deployment ${oldDeployment.deploymentId} during cleanup:`,
err,
);
}
} }
if (command) {
await execAsyncRemote(serverId, command); await execAsyncRemote(serverId, command);
}
} else { } else {
for (const oldDeployment of deploymentsToDelete) { for (const oldDeployment of deploymentsToDelete) {
try {
if (oldDeployment.rollbackId) { if (oldDeployment.rollbackId) {
await removeRollbackById(oldDeployment.rollbackId); await removeRollbackById(oldDeployment.rollbackId);
} }
const logPath = path.join(oldDeployment.logPath); const logPath = path.join(oldDeployment.logPath);
if ( if (
logPath &&
logPath !== "." &&
existsSync(logPath) && existsSync(logPath) &&
!oldDeployment.errorMessage && !oldDeployment.errorMessage
logPath !== "."
) { ) {
await fsPromises.unlink(logPath); await fsPromises.unlink(logPath);
} }
await removeDeployment(oldDeployment.deploymentId); await removeDeployment(oldDeployment.deploymentId);
} catch (err) {
console.error(
`Failed to remove deployment ${oldDeployment.deploymentId} during cleanup:`,
err,
);
}
} }
} }
} }