From e129b04c671fc4bb5c07b2646d1cb7f8382ac581 Mon Sep 17 00:00:00 2001 From: Lemon-miaow Date: Sun, 27 Sep 2026 15:31:40 +0800 Subject: [PATCH] =?UTF-8?q?fix(panel):=20=E5=A4=87=E4=BB=BD=E9=A1=B5?= =?UTF-8?q?=E8=BD=AE=E8=AF=A2=E6=9C=8D=E5=8A=A1=E5=99=A8=E7=8A=B6=E6=80=81?= =?UTF-8?q?=EF=BC=8C=E7=AB=8B=E5=8D=B3=E5=A4=87=E4=BB=BD=E6=8C=89=E5=8E=BB?= =?UTF-8?q?=E5=90=91=E5=88=A4=E6=96=AD=E5=B9=B6=E5=9C=A8=E5=B7=B2=E6=9C=89?= =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=88=96=E5=9B=9E=E6=A1=A3=E6=97=B6=E7=AD=89?= =?UTF-8?q?=E5=BE=85=EF=BC=8C=E9=87=8D=E8=AF=BB=E5=A4=B1=E8=B4=A5=E4=BF=9D?= =?UTF-8?q?=E7=95=99=E9=A1=B5=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- panel/src/i18n/resources/en-US/backups.json | 1 + panel/src/i18n/resources/zh-CN/backups.json | 1 + panel/src/pages/ServerBackups.test.tsx | 63 +++++++++++++++++++++ panel/src/pages/ServerBackups.tsx | 34 ++++++++--- 4 files changed, 92 insertions(+), 7 deletions(-) diff --git a/panel/src/i18n/resources/en-US/backups.json b/panel/src/i18n/resources/en-US/backups.json index 5166a3e..6810750 100644 --- a/panel/src/i18n/resources/en-US/backups.json +++ b/panel/src/i18n/resources/en-US/backups.json @@ -15,6 +15,7 @@ "backup_now": "Back up now", "backup_in_progress": "Starting backup…", "backup_started": "Backup started — its progress shows under recent operations below.", + "backup_world_busy": "A backup or restore is already running; back up once it finishes.", "backup_requires_stopped": "Stop the server before backing it up.", "expires_in": "Expires {{when}}", "expired": "Expired", diff --git a/panel/src/i18n/resources/zh-CN/backups.json b/panel/src/i18n/resources/zh-CN/backups.json index 803fcb0..e886d32 100644 --- a/panel/src/i18n/resources/zh-CN/backups.json +++ b/panel/src/i18n/resources/zh-CN/backups.json @@ -15,6 +15,7 @@ "backup_now": "立即备份", "backup_in_progress": "正在启动备份……", "backup_started": "已开始备份——进度可在下方“最近操作”中查看。", + "backup_world_busy": "已有备份或回档在进行,等它结束再备份。", "backup_requires_stopped": "备份前需要先停止服务器。", "expires_in": "{{when}}过期", "expired": "已过期", diff --git a/panel/src/pages/ServerBackups.test.tsx b/panel/src/pages/ServerBackups.test.tsx index cb0db8c..edf0e18 100644 --- a/panel/src/pages/ServerBackups.test.tsx +++ b/panel/src/pages/ServerBackups.test.tsx @@ -5,6 +5,7 @@ import userEvent from "@testing-library/user-event"; import { MemoryRouter, Route, Routes } from "react-router-dom"; import i18next from "i18next"; import { ServerBackups } from "./ServerBackups"; +import { humanizeError } from "@/lib/api"; import type { BackupView } from "@/lib/types"; const calls = vi.hoisted(() => ({ @@ -154,3 +155,65 @@ describe("ServerBackups", () => { expect(screen.getByText(i18next.t("backups:stop_timeout"))).toBeTruthy(); }); }); + +describe("ServerBackups back up now", () => { + const backUp = () => screen.getByRole("button", { name: "Back up now" }) as HTMLButtonElement; + + it("follows the server, so a stop lands on the page without a reload", async () => { + vi.useFakeTimers({ shouldAdvanceTime: true }); + calls.status.mockResolvedValue({ name: "survival", displayName: "Survival", phase: "Running", desiredState: "Running" }); + renderPage(); + await screen.findByText("Page 1 of 3"); + expect(backUp().disabled).toBe(true); + expect(backUp().title).toBe("Stop the server before backing it up."); + + calls.status.mockResolvedValue({ name: "survival", displayName: "Survival", phase: "Stopped", desiredState: "Stopped" }); + await act(() => vi.advanceTimersByTimeAsync(14_000)); + expect(backUp().disabled).toBe(true); + await act(() => vi.advanceTimersByTimeAsync(1_000)); + expect(backUp().disabled).toBe(false); + expect(backUp().title).toBe(""); + }); + + it("rereads fast while the server is on its way somewhere", async () => { + vi.useFakeTimers({ shouldAdvanceTime: true }); + calls.status.mockResolvedValue({ name: "survival", displayName: "Survival", phase: "Stopping", desiredState: "Stopped" }); + renderPage(); + await screen.findByText("Page 1 of 3"); + const before = calls.status.mock.calls.length; + await act(() => vi.advanceTimersByTimeAsync(4_000)); + expect(calls.status.mock.calls.length).toBe(before + 1); + }); + + it("counts a server just woken as starting and offers no backup", async () => { + calls.status.mockResolvedValue({ name: "survival", displayName: "Survival", phase: "Stopped", desiredState: "Running" }); + renderPage(); + await screen.findByText("Page 1 of 3"); + expect(backUp().disabled).toBe(true); + expect(screen.getByText("Starting")).toBeTruthy(); + }); + + it.each([ + ["a backup running", { name: "backup-survival-aa", kind: "backup", state: "running" }], + // The safety snapshot is done and the restore it leads to has yet to start. + ["a restore about to start", { name: "backup-survival-bb", kind: "backup", state: "succeeded", then_restore: "pending" }], + ])("waits for %s", async (_, job) => { + calls.serverJobs.mockResolvedValue([job]); + renderPage(); + await screen.findByText("Page 1 of 3"); + await vi.waitFor(() => expect(backUp().disabled).toBe(true)); + expect(backUp().title).toBe("A backup or restore is already running; back up once it finishes."); + }); + + it("keeps the page when a reread fails, and says the status may be stale", async () => { + vi.useFakeTimers({ shouldAdvanceTime: true }); + renderPage(); + await screen.findByText("Page 1 of 3"); + const outage = { status: 503, code: "unavailable", message: "status unavailable" }; + calls.status.mockRejectedValue(outage); + await act(() => vi.advanceTimersByTimeAsync(15_000)); + expect((await screen.findByRole("alert")).textContent).toContain(humanizeError(outage)); + expect(screen.getByText("Page 1 of 3")).toBeTruthy(); + expect(backUp().disabled).toBe(false); + }); +}); diff --git a/panel/src/pages/ServerBackups.tsx b/panel/src/pages/ServerBackups.tsx index 8c51dad..6a2a000 100644 --- a/panel/src/pages/ServerBackups.tsx +++ b/panel/src/pages/ServerBackups.tsx @@ -27,12 +27,12 @@ import { DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; -import { PhaseBadge } from "@/components/PhaseBadge"; -import { Loading, ErrorState, EmptyState, NotYours } from "@/components/States"; +import { PhaseBadge, pendingPower, shownPhase, startFailure } from "@/components/PhaseBadge"; +import { Loading, ErrorState, EmptyState, NotYours, RefreshError } from "@/components/States"; import { PageHeader } from "@/components/PageHeader"; import { Pagination } from "@/components/Pagination"; import { api, humanizeError } from "@/lib/api"; -import { useAsync } from "@/lib/hooks"; +import { useAsync, usePolling, STATUS_POLL_FAST_MS, STATUS_POLL_SLOW_MS } from "@/lib/hooks"; import { useTier } from "@/lib/tier"; import { canManage, ownershipPending } from "@/lib/ownership"; import { formatBytes, formatRelative, formatAbsolute, isExpired } from "@/lib/format"; @@ -459,6 +459,16 @@ export function ServerBackups() { const { t, i18n } = useTranslation("backups"); const { isAdmin, loading: tierLoading } = useTier(); const statusQ = useAsync(() => api.status(name), [name]); + // "Back up now" and the badge follow the server, so a wake from the lobby or an + // idle stop shows here without a reload: reread fast while it is on its way + // somewhere, slow while nothing is due. + const moving = + !!statusQ.data && + (statusQ.data.phase === "Starting" || + statusQ.data.phase === "Stopping" || + pendingPower(statusQ.data) !== null || + startFailure(statusQ.data) === "retrying"); + usePolling(statusQ.reload, moving ? STATUS_POLL_FAST_MS : STATUS_POLL_SLOW_MS); const mineQ = useAsync( () => (isAdmin ? Promise.resolve([]) : api.myServers()), [isAdmin, name], @@ -541,7 +551,7 @@ export function ServerBackups() { ); } - if (statusQ.error) { + if (statusQ.error && !statusQ.data) { return ( <> {back} @@ -560,6 +570,11 @@ export function ServerBackups() { .sort((a, b) => Date.parse(b.created_at) - Date.parse(a.created_at)); const total = backupsQ.data?.total ?? 0; const latestID = page === 1 ? all.find((b) => !b.corrupt)?.id : undefined; + // A server just woken still reads Stopped until its pod starts, so the gate is + // where it is heading. A backup or restore already under way holds the world + // (the API would answer 409), so the button waits for it too. + const phase = shownPhase(statusQ.data); + const worldBusy = (jobsQ.data ?? []).some((j) => j.state === "running" || j.then_restore === "pending"); const header = ( {backingUp ? ( @@ -586,7 +605,7 @@ export function ServerBackups() { {backingUp ? t("backup_in_progress") : t("backup_now")} )} - + } className="mb-6" @@ -597,6 +616,7 @@ export function ServerBackups() { <> {back} {header} + {!!statusQ.error && } {pending ? ( ) : mineQ.error ? (