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

View File

@@ -117,12 +117,12 @@ export const createDeployment = async (
>, >,
) => { ) => {
const application = await findApplicationById(deployment.applicationId); const application = await findApplicationById(deployment.applicationId);
await removeLastTenDeployments(
deployment.applicationId,
"application",
application.serverId,
);
try { try {
await removeLastTenDeployments(
deployment.applicationId,
"application",
application.serverId,
);
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,
); );
await removeLastTenDeployments(
deployment.previewDeploymentId,
"previewDeployment",
previewDeployment?.application?.serverId,
);
try { try {
await removeLastTenDeployments(
deployment.previewDeploymentId,
"previewDeployment",
previewDeployment?.application?.serverId,
);
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);
await removeLastTenDeployments(
deployment.composeId,
"compose",
compose.serverId,
);
try { try {
await removeLastTenDeployments(
deployment.composeId,
"compose",
compose.serverId,
);
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;
} }
await removeLastTenDeployments(deployment.backupId, "backup", serverId);
try { try {
await removeLastTenDeployments(deployment.backupId, "backup", serverId);
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);
const serverId =
schedule.application?.serverId ||
schedule.compose?.serverId ||
schedule.server?.serverId;
await removeLastTenDeployments(deployment.scheduleId, "schedule", serverId);
try { try {
const serverId =
schedule.application?.serverId ||
schedule.compose?.serverId ||
schedule.server?.serverId;
await removeLastTenDeployments(deployment.scheduleId, "schedule", serverId);
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,14 +514,14 @@ export const createDeploymentVolumeBackup = async (
) => { ) => {
const volumeBackup = await findVolumeBackupById(deployment.volumeBackupId); const volumeBackup = await findVolumeBackupById(deployment.volumeBackupId);
const serverId =
volumeBackup.application?.serverId || volumeBackup.compose?.serverId;
await removeLastTenDeployments(
deployment.volumeBackupId,
"volumeBackup",
serverId,
);
try { try {
const serverId =
volumeBackup.application?.serverId || volumeBackup.compose?.serverId;
await removeLastTenDeployments(
deployment.volumeBackupId,
"volumeBackup",
serverId,
);
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 !== ".") {
if (deployment.serverId) { const command = `rm -f ${logPath};`;
await execAsyncRemote(deployment.serverId, command); if (deployment.serverId) {
} else { await execAsyncRemote(deployment.serverId, command);
await execAsync(command); } else {
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) {
const logPath = path.join(oldDeployment.logPath); try {
if (oldDeployment.rollbackId) { const logPath = path.join(oldDeployment.logPath);
await removeRollbackById(oldDeployment.rollbackId); if (oldDeployment.rollbackId) {
} await removeRollbackById(oldDeployment.rollbackId);
}
if (logPath !== ".") { if (logPath && logPath !== ".") {
command += ` command += `rm -rf ${logPath};`;
rm -rf ${logPath}; }
`; await removeDeployment(oldDeployment.deploymentId);
} catch (err) {
console.error(
`Failed to remove deployment ${oldDeployment.deploymentId} during cleanup:`,
err,
);
} }
await removeDeployment(oldDeployment.deploymentId);
} }
await execAsyncRemote(serverId, command); if (command) {
await execAsyncRemote(serverId, command);
}
} else { } else {
for (const oldDeployment of deploymentsToDelete) { for (const oldDeployment of deploymentsToDelete) {
if (oldDeployment.rollbackId) { try {
await removeRollbackById(oldDeployment.rollbackId); if (oldDeployment.rollbackId) {
await removeRollbackById(oldDeployment.rollbackId);
}
const logPath = path.join(oldDeployment.logPath);
if (
logPath &&
logPath !== "." &&
existsSync(logPath) &&
!oldDeployment.errorMessage
) {
await fsPromises.unlink(logPath);
}
await removeDeployment(oldDeployment.deploymentId);
} catch (err) {
console.error(
`Failed to remove deployment ${oldDeployment.deploymentId} during cleanup:`,
err,
);
} }
const logPath = path.join(oldDeployment.logPath);
if (
existsSync(logPath) &&
!oldDeployment.errorMessage &&
logPath !== "."
) {
await fsPromises.unlink(logPath);
}
await removeDeployment(oldDeployment.deploymentId);
} }
} }
} }