Skip to content

Commit 42d645a

Browse files
fix(web): update Retry stops re-sending servers already on the target version (pingdotgg#32924)
1 parent 2904fea commit 42d645a

2 files changed

Lines changed: 98 additions & 11 deletions

File tree

‎apps/web/src/components/sidebar/updateEverythingRun.test.ts‎

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
import { EnvironmentId } from "@t3tools/contracts";
1+
import { EnvironmentId, type ServerConfig } from "@t3tools/contracts";
2+
import { Atom } from "effect/reactivity";
23
import { afterEach, describe, expect, it, vi } from "vite-plus/test";
34

45
import { appAtomRegistry } from "~/rpc/atomRegistry";
56
import { serverEnvironment } from "~/state/server";
6-
import { startDesktopUpdateLaunchNotice } from "./updateEverythingRun";
7+
import type { ServerUpdateOutcome, ServerUpdateTarget } from "../ServerUpdateAction";
8+
import type { UpdateEverythingRun } from "./updateEverything.logic";
9+
import {
10+
retryFailedUpdateRunServers,
11+
setUpdateEverythingRun,
12+
startDesktopUpdateLaunchNotice,
13+
} from "./updateEverythingRun";
714

815
function relaunchWhileRestarting(environmentId: EnvironmentId) {
916
const now = 1_800_000_000_000;
@@ -68,3 +75,65 @@ describe("startDesktopUpdateLaunchNotice", () => {
6875
}
6976
});
7077
});
78+
79+
describe("retryFailedUpdateRunServers", () => {
80+
it("settles a failed server that already restarted on the target and retries only the others", () => {
81+
const targetVersion = "9.9.9";
82+
const serverVersions = new Map([
83+
[EnvironmentId.make("already-updated"), targetVersion],
84+
[EnvironmentId.make("behind"), "9.9.8"],
85+
]);
86+
using _config = vi
87+
.spyOn(serverEnvironment, "configValueAtom")
88+
.mockImplementation((environmentId) =>
89+
Atom.make({
90+
environment: { serverVersion: serverVersions.get(environmentId!) },
91+
} as unknown as ServerConfig),
92+
);
93+
const rejection = "Remote updates must select a newer server version.";
94+
const failedMachine = (environmentId: EnvironmentId) => ({
95+
environmentId,
96+
label: environmentId,
97+
updatesProviders: false,
98+
server: {
99+
environmentId,
100+
serverLabel: environmentId,
101+
selfUpdate: "boot-service" as const,
102+
targetVersion,
103+
},
104+
handedOffVersion: null,
105+
startedAt: 0,
106+
result: "failed" as const,
107+
failure: rejection,
108+
providerFailure: null,
109+
});
110+
setUpdateEverythingRun(() => ({
111+
localVersion: null,
112+
phase: "servers",
113+
restartAt: null,
114+
machines: [...serverVersions.keys()].map(failedMachine),
115+
}));
116+
const update = vi.fn(
117+
(_target: ServerUpdateTarget) => new Promise<ServerUpdateOutcome>(() => {}),
118+
);
119+
120+
retryFailedUpdateRunServers(update);
121+
122+
let run: UpdateEverythingRun | null = null;
123+
setUpdateEverythingRun((current) => {
124+
run = current;
125+
return null;
126+
});
127+
expect(update.mock.calls.map(([target]) => target.environmentId)).toEqual(["behind"]);
128+
expect(
129+
(run as UpdateEverythingRun | null)?.machines.map(({ environmentId, result, server }) => ({
130+
environmentId,
131+
result,
132+
retryable: server !== null,
133+
})),
134+
).toEqual([
135+
{ environmentId: "already-updated", result: "done", retryable: false },
136+
{ environmentId: "behind", result: null, retryable: true },
137+
]);
138+
});
139+
});

‎apps/web/src/components/sidebar/updateEverythingRun.tsx‎

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useAtomValue } from "@effect/atom-react";
22
import { serverUpdateStateAtom } from "@t3tools/client-runtime/state/server";
3-
import { EnvironmentId } from "@t3tools/contracts";
3+
import { EnvironmentId, type ServerConfig } from "@t3tools/contracts";
44
import * as Option from "effect/Option";
55
import * as Schema from "effect/Schema";
66
import { AsyncResult, Atom } from "effect/reactivity";
@@ -63,6 +63,13 @@ export function settleUpdateRunMachine(
6363
);
6464
}
6565

66+
function runsVersion(config: ServerConfig | null, targetVersion: string): boolean {
67+
return (
68+
config !== null &&
69+
resolveVersionMismatch(config.environment.serverVersion, targetVersion) === null
70+
);
71+
}
72+
6673
export function retryFailedUpdateRunServers(
6774
update: (
6875
target: ServerUpdateTarget,
@@ -75,15 +82,31 @@ export function retryFailedUpdateRunServers(
7582
? [{ target: machine.server, providerFailure: machine.providerFailure }]
7683
: [],
7784
);
78-
const targets = failed.map(({ target }) => target);
85+
const pending: typeof failed = [];
86+
for (const entry of failed) {
87+
// The host rejects a same-version update; a server that came back on the target is done.
88+
if (
89+
runsVersion(
90+
appAtomRegistry.get(serverEnvironment.configValueAtom(entry.target.environmentId)),
91+
entry.target.targetVersion,
92+
)
93+
) {
94+
settleUpdateRunMachine(entry.target.environmentId, entry.providerFailure, {
95+
serverUpdated: true,
96+
});
97+
} else {
98+
pending.push(entry);
99+
}
100+
}
101+
const targets = pending.map(({ target }) => target);
79102
const retried = new Set(targets.map((target) => target.environmentId));
80103
const startedAt = Date.now();
81104
updateRunMachines((machine) =>
82105
retried.has(machine.environmentId)
83106
? { ...machine, result: null, failure: null, startedAt }
84107
: machine,
85108
);
86-
for (const { target, providerFailure } of failed) {
109+
for (const { target, providerFailure } of pending) {
87110
void update(target, { quiet: true }).then((outcome) =>
88111
settleUpdateRunMachine(
89112
target.environmentId,
@@ -309,12 +332,7 @@ function followHandedOffServers(servers: ReadonlyArray<HandedOffServer>): void {
309332
const unsubscribe = appAtomRegistry.subscribe(
310333
serverEnvironment.configValueAtom(server.environmentId),
311334
(config) => {
312-
if (
313-
config !== null &&
314-
resolveVersionMismatch(config.environment.serverVersion, server.targetVersion) === null
315-
) {
316-
settle(null);
317-
}
335+
if (runsVersion(config, server.targetVersion)) settle(null);
318336
},
319337
{ immediate: true },
320338
);

0 commit comments

Comments
 (0)