Skip to content

Commit 5f13679

Browse files
authored
Merge pull request #3258 from Dokploy/fix/long-request-on-cleanup
Fix/long request on cleanup
2 parents 2976bb5 + 415327c commit 5f13679

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

apps/dokploy/components/dashboard/settings/servers/actions/show-storage-actions.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ export const ShowStorageActions = ({ serverId }: Props) => {
173173
serverId: serverId,
174174
})
175175
.then(async () => {
176-
toast.success("Cleaned all");
176+
toast.success("Cleaning in progress... Please wait");
177177
})
178178
.catch(() => {
179179
toast.error("Error cleaning all");

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
checkGPUStatus,
44
checkPortInUse,
55
cleanupAll,
6+
cleanupAllBackground,
67
cleanupBuilders,
78
cleanupContainers,
89
cleanupImages,
@@ -193,9 +194,10 @@ export const settingsRouter = createTRPCRouter({
193194
cleanAll: adminProcedure
194195
.input(apiServerSchema)
195196
.mutation(async ({ input }) => {
196-
await cleanupAll(input?.serverId);
197+
// Execute cleanup in background and return immediately to avoid gateway timeouts
198+
const result = await cleanupAllBackground(input?.serverId);
197199

198-
return true;
200+
return result;
199201
}),
200202
cleanMonitoring: adminProcedure.mutation(async () => {
201203
if (IS_CLOUD) {

packages/server/src/utils/docker/utils.ts

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,17 @@ ${exec}
171171
172172
echo "Execution completed."`;
173173

174+
const cleanupCommands = {
175+
containers: "docker container prune --force",
176+
images: "docker image prune --all --force",
177+
builders: "docker builder prune --all --force",
178+
system: "docker system prune --all --force",
179+
volumes: "docker volume prune --all --force",
180+
};
181+
174182
export const cleanupContainers = async (serverId?: string) => {
175183
try {
176-
const command = "docker container prune --force";
184+
const command = cleanupCommands.containers;
177185

178186
if (serverId) {
179187
await execAsyncRemote(serverId, dockerSafeExec(command));
@@ -189,7 +197,7 @@ export const cleanupContainers = async (serverId?: string) => {
189197

190198
export const cleanupImages = async (serverId?: string) => {
191199
try {
192-
const command = "docker image prune --all --force";
200+
const command = cleanupCommands.images;
193201

194202
if (serverId) {
195203
await execAsyncRemote(serverId, dockerSafeExec(command));
@@ -203,7 +211,7 @@ export const cleanupImages = async (serverId?: string) => {
203211

204212
export const cleanupVolumes = async (serverId?: string) => {
205213
try {
206-
const command = "docker volume prune --all --force";
214+
const command = cleanupCommands.volumes;
207215

208216
if (serverId) {
209217
await execAsyncRemote(serverId, dockerSafeExec(command));
@@ -219,7 +227,7 @@ export const cleanupVolumes = async (serverId?: string) => {
219227

220228
export const cleanupBuilders = async (serverId?: string) => {
221229
try {
222-
const command = "docker builder prune --all --force";
230+
const command = cleanupCommands.builders;
223231

224232
if (serverId) {
225233
await execAsyncRemote(serverId, dockerSafeExec(command));
@@ -235,7 +243,7 @@ export const cleanupBuilders = async (serverId?: string) => {
235243

236244
export const cleanupSystem = async (serverId?: string) => {
237245
try {
238-
const command = "docker system prune --all --force";
246+
const command = cleanupCommands.system;
239247

240248
if (serverId) {
241249
await execAsyncRemote(serverId, dockerSafeExec(command));
@@ -256,6 +264,34 @@ export const cleanupAll = async (serverId?: string) => {
256264
await cleanupSystem(serverId);
257265
};
258266

267+
export const cleanupAllBackground = async (serverId?: string) => {
268+
Promise.allSettled(
269+
Object.values(cleanupCommands).map(async (command) => {
270+
try {
271+
if (serverId) {
272+
await execAsyncRemote(serverId, dockerSafeExec(command));
273+
} else {
274+
await execAsync(dockerSafeExec(command));
275+
}
276+
} catch (error) {}
277+
}),
278+
)
279+
.then((results) => {
280+
const failed = results.filter((r) => r.status === "rejected");
281+
if (failed.length > 0) {
282+
console.error(`Docker cleanup: ${failed.length} operations failed`);
283+
} else {
284+
console.log("Docker cleanup completed successfully");
285+
}
286+
})
287+
.catch((error) => console.error("Error in cleanup:", error));
288+
289+
return {
290+
status: "scheduled",
291+
message: "Docker cleanup has been initiated in the background",
292+
};
293+
};
294+
259295
export const startService = async (appName: string) => {
260296
try {
261297
await execAsync(`docker service scale ${appName}=1 `);

0 commit comments

Comments
 (0)