diff --git a/apps/dokploy/server/api/routers/settings.ts b/apps/dokploy/server/api/routers/settings.ts index bd182527a..aa9b18628 100644 --- a/apps/dokploy/server/api/routers/settings.ts +++ b/apps/dokploy/server/api/routers/settings.ts @@ -3,7 +3,6 @@ import { checkGPUStatus, checkPortInUse, cleanupAll, - cleanupAllBackground, cleanupBuilders, cleanupContainers, cleanupImages, @@ -202,9 +201,12 @@ export const settingsRouter = createTRPCRouter({ .input(apiServerSchema) .mutation(async ({ input }) => { // Execute cleanup in background and return immediately to avoid gateway timeouts - const result = await cleanupAllBackground(input?.serverId); + void cleanupAll(input?.serverId); - return result; + return { + status: "scheduled", + message: "Docker cleanup has been initiated in the background", + }; }), cleanMonitoring: adminProcedure.mutation(async () => { if (IS_CLOUD) { diff --git a/packages/server/src/db/schema/server.ts b/packages/server/src/db/schema/server.ts index 176f35948..c02247b79 100644 --- a/packages/server/src/db/schema/server.ts +++ b/packages/server/src/db/schema/server.ts @@ -39,7 +39,7 @@ export const server = pgTable("server", { appName: text("appName") .notNull() .$defaultFn(() => generateAppName("server")), - enableDockerCleanup: boolean("enableDockerCleanup").notNull().default(false), + enableDockerCleanup: boolean("enableDockerCleanup").notNull().default(true), createdAt: text("createdAt").notNull(), organizationId: text("organizationId") .notNull() diff --git a/packages/server/src/utils/docker/utils.ts b/packages/server/src/utils/docker/utils.ts index 045040061..2d7e8acae 100644 --- a/packages/server/src/utils/docker/utils.ts +++ b/packages/server/src/utils/docker/utils.ts @@ -156,7 +156,7 @@ CHECK_INTERVAL=10 echo "Preparing for execution..." while true; do - PROCESSES=$(ps aux | grep -E "^.*docker [A-Za-z]" | grep -v grep) + PROCESSES=$(ps aux | grep -E "^.*docker [a-zA-Z]" | grep -v grep) if [ -z "$PROCESSES" ]; then echo "Docker is idle. Starting execution..." @@ -173,11 +173,11 @@ echo "Execution completed." `; const cleanupCommands = { - containers: "docker container prune --force", - images: "docker image prune --all --force", - volumes: "docker volume prune --all --force", - builders: "docker builder prune --all --force", - system: "docker system prune --all --force", + containers: dockerSafeExec("docker container prune --force"), + images: dockerSafeExec("docker image prune --all --force"), + volumes: dockerSafeExec("docker volume prune --all --force"), + builders: dockerSafeExec("docker builder prune --all --force"), + system: dockerSafeExec("docker system prune --all --force"), }; export const cleanupContainers = async (serverId?: string) => { @@ -185,14 +185,12 @@ export const cleanupContainers = async (serverId?: string) => { const command = cleanupCommands.containers; if (serverId) { - await execAsyncRemote(serverId, dockerSafeExec(command)); + await execAsyncRemote(serverId, command); } else { - await execAsync(dockerSafeExec(command)); + await execAsync(command); } } catch (error) { console.error(error); - - throw error; } }; @@ -201,12 +199,12 @@ export const cleanupImages = async (serverId?: string) => { const command = cleanupCommands.images; if (serverId) { - await execAsyncRemote(serverId, dockerSafeExec(command)); - } else await execAsync(dockerSafeExec(command)); + await execAsyncRemote(serverId, command); + } else { + await execAsync(command); + } } catch (error) { console.error(error); - - throw error; } }; @@ -215,14 +213,12 @@ export const cleanupVolumes = async (serverId?: string) => { const command = cleanupCommands.volumes; if (serverId) { - await execAsyncRemote(serverId, dockerSafeExec(command)); + await execAsyncRemote(serverId, command); } else { - await execAsync(dockerSafeExec(command)); + await execAsync(command); } } catch (error) { console.error(error); - - throw error; } }; @@ -231,14 +227,12 @@ export const cleanupBuilders = async (serverId?: string) => { const command = cleanupCommands.builders; if (serverId) { - await execAsyncRemote(serverId, dockerSafeExec(command)); + await execAsyncRemote(serverId, command); } else { - await execAsync(dockerSafeExec(command)); + await execAsync(command); } } catch (error) { console.error(error); - - throw error; } }; @@ -247,14 +241,12 @@ export const cleanupSystem = async (serverId?: string) => { const command = cleanupCommands.system; if (serverId) { - await execAsyncRemote(serverId, dockerSafeExec(command)); + await execAsyncRemote(serverId, command); } else { - await execAsync(dockerSafeExec(command)); + await execAsync(command); } } catch (error) { console.error(error); - - throw error; } }; @@ -276,47 +268,16 @@ export const cleanupAll = async (serverId?: string) => { try { if (serverId) { - await execAsyncRemote(serverId, dockerSafeExec(command)); + await execAsyncRemote(serverId, command); } else { - await execAsync(dockerSafeExec(command)); + await execAsync(command); } - } catch {} + } catch (error) { + console.error(error); + } } }; -export const cleanupAllBackground = async (serverId?: string) => { - Promise.allSettled( - ( - Object.entries(cleanupCommands) as [ - keyof typeof cleanupCommands, - string, - ][] - ) - .filter(([key]) => !excludedCleanupAllCommands.includes(key)) - .map(async ([, command]) => { - if (serverId) { - await execAsyncRemote(serverId, dockerSafeExec(command)); - } else { - await execAsync(dockerSafeExec(command)); - } - }), - ) - .then((results) => { - const failed = results.filter((r) => r.status === "rejected"); - if (failed.length > 0) { - console.error(`Docker cleanup: ${failed.length} operations failed`); - } else { - console.log("Docker cleanup completed successfully"); - } - }) - .catch((error) => console.error("Error in cleanup:", error)); - - return { - status: "scheduled", - message: "Docker cleanup has been initiated in the background", - }; -}; - export const startService = async (appName: string) => { try { await execAsync(`docker service scale ${appName}=1 `);