TypeScript
fix(web): servers still restarting after an app update no longer offer the update again (#26109)
Tristan Knight8f0d66de5fa6
diff --git a/apps/web/src/components/sidebar/updateEverythingRun.test.ts b/apps/web/src/components/sidebar/updateEverythingRun.test.tsnew file mode 100644index 00000000000..ec6e5b79b7c--- /dev/null+++ b/apps/web/src/components/sidebar/updateEverythingRun.test.ts@@ -0,0 +1,70 @@+import { EnvironmentId } from "@t3tools/contracts";+import { afterEach, describe, expect, it, vi } from "vite-plus/test";++import { appAtomRegistry } from "~/rpc/atomRegistry";+import { serverEnvironment } from "~/state/server";+import { startDesktopUpdateLaunchNotice } from "./updateEverythingRun";++function relaunchWhileRestarting(environmentId: EnvironmentId) {+ const now = 1_800_000_000_000;+ vi.useFakeTimers({ now });+ const store = new Map([+ [+ "t3code:update-everything-result:v1",+ JSON.stringify({+ savedAt: now,+ localVersion: null,+ updatedServers: 0,+ updatedProviders: 0,+ failures: [],+ handedOff: [{ environmentId, label: "Slow", targetVersion: "9.9.9", startedAt: now }],+ }),+ ],+ ]);+ vi.stubGlobal("window", {+ localStorage: {+ getItem: (key: string) => store.get(key) ?? null,+ removeItem: (key: string) => store.delete(key),+ },+ });+ return startDesktopUpdateLaunchNotice();+}++describe("startDesktopUpdateLaunchNotice", () => {+ afterEach(() => {+ vi.useRealTimers();+ vi.unstubAllGlobals();+ });++ it("keeps a server that is still restarting from looking idle after the app relaunches", async () => {+ const environmentId = EnvironmentId.make("slow-machine");+ const stop = relaunchWhileRestarting(environmentId);+ try {+ // Lets the registry run its cleanup of unobserved atoms before the UI mounts.+ await vi.advanceTimersByTimeAsync(1_000);+ expect(appAtomRegistry.get(serverEnvironment.updateStateAtom(environmentId))).toMatchObject({+ status: "running",+ stage: "resuming",+ targetVersion: "9.9.9",+ });+ } finally {+ await vi.advanceTimersByTimeAsync(32 * 60 * 1000);+ stop();+ }+ });++ it("clears the restored state when the server never comes back", async () => {+ const environmentId = EnvironmentId.make("lost-machine");+ const stateAtom = serverEnvironment.updateStateAtom(environmentId);+ const stop = relaunchWhileRestarting(environmentId);+ // Observed like the UI does, so the registry cannot reset it by dropping the atom.+ const release = appAtomRegistry.mount(stateAtom);+ try {+ await vi.advanceTimersByTimeAsync(32 * 60 * 1000);+ expect(appAtomRegistry.get(stateAtom)).toEqual({ status: "idle" });+ } finally {+ release();+ stop();+ }+ });+});diff --git a/apps/web/src/components/sidebar/updateEverythingRun.tsx b/apps/web/src/components/sidebar/updateEverythingRun.tsxindex 82f42a055f0..0422fffe6a9 100644--- a/apps/web/src/components/sidebar/updateEverythingRun.tsx+++ b/apps/web/src/components/sidebar/updateEverythingRun.tsx@@ -1,4 +1,5 @@import { useAtomValue } from "@effect/atom-react";+import { serverUpdateStateAtom } from "@t3tools/client-runtime/state/server";import { EnvironmentId } from "@t3tools/contracts";import * as Option from "effect/Option";import * as Schema from "effect/Schema";@@ -267,12 +268,26 @@ function followHandedOffServers(servers: ReadonlyArray<HandedOffServer>): void {let updatedServers = 0;const failures: string[] = [];for (const server of servers) {+ // Update state is in memory, so restore it or the relaunched app offers this server's update again.+ const stateAtom = serverUpdateStateAtom(server.environmentId);+ appAtomRegistry.set(stateAtom, {+ status: "running",+ stage: "resuming",+ fromVersion:+ appAtomRegistry.get(serverEnvironment.configValueAtom(server.environmentId))?.environment+ .serverVersion ?? server.targetVersion,+ targetVersion: server.targetVersion,+ });+ // Nothing observes the atom until the UI mounts, and the registry drops unobserved atoms.+ const releaseState = appAtomRegistry.mount(stateAtom);let settled = false;const settle = (failure: string | null) => {if (settled) return;settled = true;clearTimeout(timer);queueMicrotask(() => unsubscribe());+ appAtomRegistry.set(stateAtom, { status: "idle" });+ releaseState();settleUpdateRunMachine(server.environmentId, failure);if (failure === null) updatedServers += 1;else failures.push(`${server.label}: ${failure}`);