From 73ea2cb2942cc7880a8a60dbe3bf45a06c8cea40 Mon Sep 17 00:00:00 2001 From: Peter Gonda Date: Tue, 6 Oct 2026 13:14:57 +0200 Subject: [PATCH] panel: retry transient failures + maintenance overlay (survive roll cutovers) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When the panel self-updates (Nomad canary roll), Traefik returns 502/503/504 for the cutover window and in-flight requests can fail at the network layer. The tRPC client had no retry config, so actions submitted then (e.g. Redeploy, a save) failed silently and a failed request read as a logout. - api.ts: isTransientError() + queryClientConfig retry — queries 5x, mutations 3x, ONLY on 5xx/network errors (never 4xx), exponential backoff capped at 5s. So an action during a cutover retries and succeeds once the panel is back. (In @trpc/next v11 queryClientConfig goes inside the config() return.) - maintenance-overlay.tsx + _app.tsx: a slim non-blocking 'Panel is updating — reconnecting…' banner that polls /api/health (15s healthy / 2s down) and auto-clears when the panel answers. Never traps the user. Co-Authored-By: Claude Opus 4.8 --- .../components/shared/maintenance-overlay.tsx | 58 +++++++++++++++++++ apps/dokploy/pages/_app.tsx | 2 + apps/dokploy/utils/api.ts | 44 +++++++++++++- 3 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 apps/dokploy/components/shared/maintenance-overlay.tsx diff --git a/apps/dokploy/components/shared/maintenance-overlay.tsx b/apps/dokploy/components/shared/maintenance-overlay.tsx new file mode 100644 index 000000000..e0f131e63 --- /dev/null +++ b/apps/dokploy/components/shared/maintenance-overlay.tsx @@ -0,0 +1,58 @@ +import { Loader2 } from "lucide-react"; +import { useEffect, useState } from "react"; +import { cn } from "@/lib/utils"; + +const HEALTH_URL = "/api/health"; +const POLL_HEALTHY_MS = 15000; +const POLL_DOWN_MS = 2000; + +/** + * Polls the panel's readiness probe (/api/health) and shows a slim, non-blocking + * banner while the panel is unreachable — e.g. during a self-update (Nomad canary + * roll) when Traefik briefly has no healthy backend and returns 502/503. The + * banner auto-clears as soon as the panel answers again. It never traps the user + * in a full-screen modal; the rest of the UI stays usable while requests retry. + */ +export const MaintenanceOverlay = () => { + const [down, setDown] = useState(false); + + useEffect(() => { + if (typeof window === "undefined") return; + + let cancelled = false; + + const check = async () => { + try { + const res = await fetch(HEALTH_URL, { cache: "no-store" }); + if (!cancelled) setDown(!res.ok); + } catch { + if (!cancelled) setDown(true); + } + }; + + // Run one check immediately, then poll faster while down so recovery is quick. + void check(); + const interval = setInterval(check, down ? POLL_DOWN_MS : POLL_HEALTHY_MS); + + return () => { + cancelled = true; + clearInterval(interval); + }; + }, [down]); + + if (!down) return null; + + return ( + + + Panel is updating — reconnecting… + + ); +}; diff --git a/apps/dokploy/pages/_app.tsx b/apps/dokploy/pages/_app.tsx index df3411151..16aed8e49 100644 --- a/apps/dokploy/pages/_app.tsx +++ b/apps/dokploy/pages/_app.tsx @@ -9,6 +9,7 @@ import NextTopLoader from "nextjs-toploader"; import type { ReactElement, ReactNode } from "react"; import { SearchCommand } from "@/components/dashboard/search-command"; import { WhitelabelingProvider } from "@/components/proprietary/whitelabeling/whitelabeling-provider"; +import { MaintenanceOverlay } from "@/components/shared/maintenance-overlay"; import { Toaster } from "@/components/ui/sonner"; import { api } from "@/utils/api"; @@ -49,6 +50,7 @@ const MyApp = ({ forcedTheme={Component.theme} > + diff --git a/apps/dokploy/utils/api.ts b/apps/dokploy/utils/api.ts index d7f165f6a..4d9af7b63 100644 --- a/apps/dokploy/utils/api.ts +++ b/apps/dokploy/utils/api.ts @@ -3,6 +3,7 @@ import { httpBatchLink, httpLink, splitLink, + TRPCClientError, wsLink, } from "@trpc/client"; import { createTRPCNext } from "@trpc/next"; @@ -10,6 +11,26 @@ import type { inferRouterInputs, inferRouterOutputs } from "@trpc/server"; import superjson from "superjson"; import type { AppRouter } from "@/server/api/root"; +/** + * Transient failures are the ones worth retrying during a panel self-update + * (a Nomad canary roll). While Traefik has no healthy backend it returns + * 502/503/504, and in-flight requests may fail at the network layer before + * they ever reach the server. Those should be retried so the user's action + * survives the cutover. Real 4xx errors (400/401/403/404) are never retried. + */ +export const isTransientError = (error: unknown): boolean => { + if (error instanceof TRPCClientError) { + const status = (error as TRPCClientError).data?.httpStatus; + if (typeof status === "number") { + return status >= 500; + } + // No tRPC error shape at all (network/parse failure mid-cutover). + return true; + } + // Plain Error/TypeError from a failed fetch — treat as a network failure. + return true; +}; + const getBaseUrl = () => { if (typeof window !== "undefined") return ""; return `http://localhost:${process.env.PORT ?? 3000}`; @@ -73,7 +94,28 @@ const links = export const api = createTRPCNext({ config() { - return { links }; + return { + links, + // Retry only transient failures (see isTransientError) so a user's + // action survives the panel self-update cutover (Traefik 502/503/504 + // or an outright network failure while no backend is healthy). + // NB: in @trpc/next v11 this lives inside the config() return value + // (it's forwarded to the internal QueryClient), not alongside config(). + queryClientConfig: { + defaultOptions: { + queries: { + retry: (count, err) => count < 5 && isTransientError(err), + retryDelay: (count) => Math.min(1000 * 2 ** count, 5000), + }, + mutations: { + // A 502 at the LB means the mutation never reached the server, + // so retrying is safe for the panel-roll cutover case. + retry: (count, err) => count < 3 && isTransientError(err), + retryDelay: (count) => Math.min(1000 * 2 ** count, 5000), + }, + }, + }, + }; }, ssr: false, transformer: superjson,