Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions apps/dokploy/components/shared/maintenance-overlay.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<output
aria-live="polite"
className={cn(
"fixed inset-x-0 top-0 z-[100] flex items-center justify-center gap-2",
"border-b border-amber-500/40 bg-amber-500/15 px-4 py-2",
"text-sm font-medium text-amber-700 backdrop-blur dark:text-amber-300",
)}
>
<Loader2 className="size-4 animate-spin" />
<span>Panel is updating — reconnecting…</span>
</output>
);
};
2 changes: 2 additions & 0 deletions apps/dokploy/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -49,6 +50,7 @@ const MyApp = ({
forcedTheme={Component.theme}
>
<NextTopLoader color="hsl(var(--sidebar-ring))" />
<MaintenanceOverlay />
<WhitelabelingProvider />
<Toaster richColors />
<SearchCommand />
Expand Down
44 changes: 43 additions & 1 deletion apps/dokploy/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,34 @@ import {
httpBatchLink,
httpLink,
splitLink,
TRPCClientError,
wsLink,
} from "@trpc/client";
import { createTRPCNext } from "@trpc/next";
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<AppRouter>).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}`;
Expand Down Expand Up @@ -73,7 +94,28 @@ const links =

export const api = createTRPCNext<AppRouter>({
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,
Expand Down
Loading