From 187f051484ab3c51d6d19647713e49aa7e267adf Mon Sep 17 00:00:00 2001 From: UndefinedPony Date: Tue, 31 Dec 2024 11:20:59 +0100 Subject: [PATCH] fix: check health endpoint instead of awaiting service restart to fix update success status --- .../settings/web-server/update-webserver.tsx | 97 +++++++++++++------ apps/dokploy/server/api/routers/settings.ts | 4 +- 2 files changed, 71 insertions(+), 30 deletions(-) diff --git a/apps/dokploy/components/dashboard/settings/web-server/update-webserver.tsx b/apps/dokploy/components/dashboard/settings/web-server/update-webserver.tsx index c1e5de70..537a1add 100644 --- a/apps/dokploy/components/dashboard/settings/web-server/update-webserver.tsx +++ b/apps/dokploy/components/dashboard/settings/web-server/update-webserver.tsx @@ -11,30 +11,50 @@ import { } from "@/components/ui/alert-dialog"; import { Button } from "@/components/ui/button"; import { api } from "@/utils/api"; -import { HardDriveDownload } from "lucide-react"; +import { HardDriveDownload, Loader2 } from "lucide-react"; +import { useState } from "react"; import { toast } from "sonner"; -interface Props { - isNavbar?: boolean; -} +export const UpdateWebServer = () => { + const [updating, setUpdating] = useState(false); + const [open, setOpen] = useState(false); -export const UpdateWebServer = ({ isNavbar }: Props) => { - const { mutateAsync: updateServer, isLoading } = - api.settings.updateServer.useMutation(); + const { mutateAsync: updateServer } = api.settings.updateServer.useMutation(); - const buttonLabel = isNavbar ? "Update available" : "Update Server"; - - const handleConfirm = async () => { + const checkIsUpdateFinished = async () => { try { - await updateServer(); + const response = await fetch("/api/health"); + if (!response.ok) { + throw new Error("Health check failed"); + } + toast.success( "The server has been updated. The page will be reloaded to reflect the changes...", ); + setTimeout(() => { // Allow seeing the toast before reloading window.location.reload(); }, 2000); + } catch { + // Delay each request + await new Promise((resolve) => setTimeout(resolve, 2000)); + // Keep running until it returns 200 + void checkIsUpdateFinished(); + } + }; + + const handleConfirm = async () => { + try { + setUpdating(true); + await updateServer(); + + // Give some time for docker service restart before starting to check status + await new Promise((resolve) => setTimeout(resolve, 8000)); + + await checkIsUpdateFinished(); } catch (error) { + setUpdating(false); console.error("Error updating server:", error); toast.error( "An error occurred while updating the server, please try again.", @@ -43,35 +63,54 @@ export const UpdateWebServer = ({ isNavbar }: Props) => { }; return ( - + - Are you absolutely sure? + + {updating + ? "Server update in progress" + : "Are you absolutely sure?"} + - This action cannot be undone. This will update the web server to the - new version. The page will be reloaded once the update is finished. + {updating ? ( + + + The server is being updated, please wait... + + ) : ( + <> + This action cannot be undone. This will update the web server to + the new version. You will not be able to use the panel during + the update process. The page will be reloaded once the update is + finished. + + )} - - Cancel - Confirm - + {!updating && ( + + setOpen(false)}> + Cancel + + + Confirm + + + )} ); diff --git a/apps/dokploy/server/api/routers/settings.ts b/apps/dokploy/server/api/routers/settings.ts index b40ee95b..449a2233 100644 --- a/apps/dokploy/server/api/routers/settings.ts +++ b/apps/dokploy/server/api/routers/settings.ts @@ -359,7 +359,9 @@ export const settingsRouter = createTRPCRouter({ await pullLatestRelease(); - await spawnAsync("docker", [ + // This causes restart of dokploy, thus it will not finish executing properly, so don't await it + // Status after restart is checked via frontend /api/health endpoint + void spawnAsync("docker", [ "service", "update", "--force",