From 2352939e87e85de2058653747e454b4a5cb818a1 Mon Sep 17 00:00:00 2001 From: Mauricio Siu <47042324+Siumauricio@users.noreply.github.com> Date: Sun, 6 Apr 2025 03:05:46 -0600 Subject: [PATCH] fix(application): enhance application reload process with error handling and Docker container management - Added mechanizeDockerContainer function to manage Docker container state during application reload. - Improved error handling to update application status on failure and provide more informative error messages. - Refactored authorization check to throw an error if the user is not authorized to reload the application. --- .../dokploy/server/api/routers/application.ts | 47 +++++++++++-------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/apps/dokploy/server/api/routers/application.ts b/apps/dokploy/server/api/routers/application.ts index 2397e4ca..9d73b590 100644 --- a/apps/dokploy/server/api/routers/application.ts +++ b/apps/dokploy/server/api/routers/application.ts @@ -33,6 +33,7 @@ import { findApplicationById, findProjectById, getApplicationStats, + mechanizeDockerContainer, readConfig, readRemoteConfig, removeDeployments, @@ -132,28 +133,36 @@ export const applicationRouter = createTRPCRouter({ .input(apiReloadApplication) .mutation(async ({ input, ctx }) => { const application = await findApplicationById(input.applicationId); - if ( - application.project.organizationId !== ctx.session.activeOrganizationId - ) { + + try { + if ( + application.project.organizationId !== + ctx.session.activeOrganizationId + ) { + throw new TRPCError({ + code: "UNAUTHORIZED", + message: "You are not authorized to reload this application", + }); + } + + if (application.serverId) { + await stopServiceRemote(application.serverId, input.appName); + } else { + await stopService(input.appName); + } + + await updateApplicationStatus(input.applicationId, "idle"); + await mechanizeDockerContainer(application); + await updateApplicationStatus(input.applicationId, "done"); + return true; + } catch (error) { + await updateApplicationStatus(input.applicationId, "error"); throw new TRPCError({ - code: "UNAUTHORIZED", - message: "You are not authorized to reload this application", + code: "INTERNAL_SERVER_ERROR", + message: "Error reloading application", + cause: error, }); } - if (application.serverId) { - await stopServiceRemote(application.serverId, input.appName); - } else { - await stopService(input.appName); - } - await updateApplicationStatus(input.applicationId, "idle"); - - if (application.serverId) { - await startServiceRemote(application.serverId, input.appName); - } else { - await startService(input.appName); - } - await updateApplicationStatus(input.applicationId, "done"); - return true; }), delete: protectedProcedure