fix(web): show server update progress through reconnect #4903
Mergedt3dotgg(GitHub profile, opens in a new tab)merged 12 commits into
main fromrefs/pull/4903/headmerged by t3dotgg(GitHub profile, opens in a new tab) View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/server/src/ws.ts lines 1387 to 1387OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1365,2 +1367,21 @@13651367 "rpc.aggregate": "server",13661368 }),1369+ [WS_METHODS.serverUpdateServerWithProgress]: (input) =>1370+ observeRpcStream(1371+ WS_METHODS.serverUpdateServerWithProgress,1372+ Stream.callback<ServerSelfUpdateProgressEvent, ServerSelfUpdateError>((queue) =>1373+ serverSelfUpdate1374+ .update(input, (stage) =>1375+ Queue.offer(queue, {1376+ type: "progress",1377+ stage,1378+ }).pipe(Effect.asVoid),1379+ )1380+ .pipe(1381+ Effect.flatMap((result) =>1382+ Queue.offer(queue, {1383+ type: "complete",1384+ result,1385+ }),1386+ ),1387+ Effect.catchTag("ServerSelfUpdateError", (error) => Queue.fail(queue, error)),
- packages/client-runtime/src/state/server.ts line 432OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -273,0 +412,21 @@412+ serverUpdateStateForProgressEvent(targetVersion, event),413+ );414+ }).pipe(415+ Effect.andThen(416+ event.type === "complete"417+ ? Ref.set(terminal, Option.some(event.result))418+ : Effect.void,419+ ),420+ ),421+ ),422+ );423+ return yield* Ref.get(terminal).pipe(424+ Effect.flatMap(425+ Option.match({426+ onNone: () =>427+ Effect.fail(new ServerUpdateProgressIncompleteError({ targetVersion })),428+ onSome: Effect.succeed,429+ }),430+ ),431+ );432+ })
- packages/client-runtime/src/state/server.ts line 168Resolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -29,0 +117,21 @@117+ cause.reasons.length > 0 &&118+ cause.reasons.every((reason) => Cause.isFailReason(reason) && isRpcClientError(reason.error))119+ );120+}121+122+export function resolveServerUpdateProgressResult<E>(123+ targetVersion: string,124+ terminal: Option.Option<ServerSelfUpdateResult>,125+ streamExit: Exit.Exit<void, E>,126+): Effect.Effect<ServerSelfUpdateResult, E | ServerUpdateProgressIncompleteError> {127+ if (128+ Option.isSome(terminal) &&129+ (Exit.isSuccess(streamExit) || isLegacyUpdateHandoffLoss(streamExit.cause))130+ ) {131+ return Effect.succeed(terminal.value);132+ }133+ if (Exit.isFailure(streamExit)) {134+ return Effect.failCause(streamExit.cause);135+ }136+ return Effect.fail(new ServerUpdateProgressIncompleteError({ targetVersion }));137+}apps/web/src/components/ServerUpdateAction.tsx line 213Resolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -188,11 +199,6 @@188199 );189200 }190201191- return pending ? (192- <Button size="xs" disabled>193- <Spinner className="size-3.5" />194- Updating...195- </Button>196- ) : (202+ return (197203 <Button size="xs" onClick={() => void handleUpdate()}>198- Update server204+ {label}
- packages/client-runtime/src/state/server.ts line 143Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -29,0 +92,21 @@92+ override get message(): string {93+ return `The t3@${this.targetVersion} update ended before the server accepted the restart.`;94+ }95+}96+97+export function serverUpdateStateForProgressEvent(98+ targetVersion: string,99+ event: ServerSelfUpdateProgressEvent,100+): Extract<ServerUpdateState, { status: "running" }> {101+ return {102+ status: "running",103+ stage: event.type === "complete" ? "resuming" : event.stage,104+ targetVersion,105+ };106+}107+108+function serverUpdateFailureMessage(error: unknown): string {109+ return error instanceof Error ? error.message : "Server update failed.";110+}111+112+export function isLegacyUpdateHandoffLoss(cause: Cause.Cause<unknown>): boolean {packages/client-runtime/src/state/server.ts lines 432 to 442Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -271,3 +380,18 @@271380 );272381 }).pipe(Atom.withLabel(`environment-data:server:config:${environmentId}`));273382 });383+ const updateStateAtom = (environmentId: EnvironmentId | null) =>384+ environmentId === null ? EMPTY_SERVER_UPDATE_STATE_ATOM : serverUpdateStateAtom(environmentId);385+ const updateServer = createRuntimeCommand<386+ EnvironmentRegistry | EnvironmentCacheStore | R,387+ E,388+ ServerUpdateTarget,389+ ServerSelfUpdateResult,390+ unknown391+ >(runtime, {392+ label: "environment-data:server:update-server",393+ concurrency: {394+ mode: "singleFlight",395+ key: ({ environmentId }) => environmentId,396+ },397+ execute: (target, atomRegistry) => {packages/client-runtime/src/state/server.ts lines 395 to 395OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -271,3 +380,16 @@271380 );272381 }).pipe(Atom.withLabel(`environment-data:server:config:${environmentId}`));273382 });383+ const updateStateAtom = (environmentId: EnvironmentId | null) =>384+ environmentId === null ? EMPTY_SERVER_UPDATE_STATE_ATOM : serverUpdateStateAtom(environmentId);385+ const updateServer = createRuntimeCommand<386+ EnvironmentRegistry | EnvironmentCacheStore | R,387+ E,388+ ServerUpdateTarget,389+ ServerSelfUpdateResult,390+ unknown391+ >(runtime, {392+ label: "environment-data:server:update-server",393+ concurrency: {394+ mode: "singleFlight",395+ key: ({ environmentId }) => environmentId,
- packages/client-runtime/src/state/server.ts line 591Resolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -273,0 +505,21 @@505+506+ atomRegistry.set(stateAtom, IDLE_SERVER_UPDATE_STATE);507+ return result;508+ }).pipe(509+ Effect.onExit((exit) =>510+ Effect.sync(() => {511+ if (Exit.isSuccess(exit)) {512+ return;513+ }514+ if (Cause.hasInterruptsOnly(exit.cause)) {515+ atomRegistry.set(stateAtom, IDLE_SERVER_UPDATE_STATE);516+ return;517+ }518+ atomRegistry.set(stateAtom, {519+ status: "failed",520+ stage: currentStage,521+ targetVersion,522+ message: serverUpdateFailureMessage(Cause.squash(exit.cause)),523+ });524+ }),525+ ),
- packages/client-runtime/src/state/server.ts lines 133 to 140Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -29,0 +117,21 @@117+ ? state118+ : IDLE_SERVER_UPDATE_STATE;119+}120+121+function serverUpdateFailureMessage(error: unknown): string {122+ return error instanceof Error ? error.message : "Server update failed.";123+}124+125+function isRpcSocketError(error: unknown): boolean {126+ if (!isRpcClientError(error)) {127+ return false;128+ }129+ switch (error.reason._tag) {130+ case "SocketReadError":131+ case "SocketWriteError":132+ case "SocketOpenError":133+ case "SocketCloseError":134+ return true;135+ default:136+ return false;137+ }
- packages/client-runtime/src/state/server.ts line 441Resolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -273 +410,21 @@273410 });411+ const updateStateValueAtom = Atom.family((environmentId: EnvironmentId) =>412+ Atom.make((get) =>413+ serverUpdateStateForServerVersion(414+ get(serverUpdateStateAtom(environmentId)),415+ get(configValueAtom(environmentId))?.environment.serverVersion ?? null,416+ ),417+ ).pipe(Atom.withLabel(`environment-data:server:update-state-value:${environmentId}`)),418+ );419+ const updateStateAtom = (environmentId: EnvironmentId | null) =>420+ environmentId === null ? EMPTY_SERVER_UPDATE_STATE_ATOM : updateStateValueAtom(environmentId);421+ const updateServer = createRuntimeCommand<422+ EnvironmentRegistry | EnvironmentCacheStore | R,423+ E,424+ ServerUpdateTarget,425+ ServerSelfUpdateResult,426+ unknown427+ >(runtime, {428+ label: "environment-data:server:update-server",429+ scheduler: configScheduler,430+ concurrency: configConcurrency,
- packages/client-runtime/src/state/server.ts lines 117 to 119OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -29,0 +99,21 @@99+100+export function serverUpdateStateForProgressEvent(101+ fromVersion: string,102+ targetVersion: string,103+ event: ServerSelfUpdateProgressEvent,104+): Extract<ServerUpdateState, { status: "running" }> {105+ return {106+ status: "running",107+ stage: event.type === "complete" ? "resuming" : event.stage,108+ fromVersion,109+ targetVersion,110+ };111+}112+113+export function serverUpdateStateForServerVersion(114+ state: ServerUpdateState,115+ serverVersion: string | null,116+): ServerUpdateState {117+ return state.status === "idle" || serverVersion === null || state.fromVersion === serverVersion118+ ? state119+ : IDLE_SERVER_UPDATE_STATE;
- packages/client-runtime/src/state/server.ts line 572Resolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -273,0 +538,21 @@538+539+ const resumed = yield* environmentRegistry540+ .followStream(target.environmentId, subscribe(WS_METHODS.subscribeServerLifecycle, {}))541+ .pipe(542+ Stream.filter(543+ (event) =>544+ event.type === "ready" && event.payload.environment.serverVersion === targetVersion,545+ ),546+ Stream.runHead,547+ Effect.timeoutOption(Duration.seconds(120)),548+ Effect.map(Option.flatten),549+ );550+ if (Option.isNone(resumed)) {551+ return yield* new ServerUpdateResumeTimeoutError({552+ environmentId: target.environmentId,553+ targetVersion,554+ });555+ }556+557+ atomRegistry.set(stateAtom, IDLE_SERVER_UPDATE_STATE);558+ return result;
- packages/client-runtime/src/state/server.ts line 374Resolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -225,4 +365,10 @@225365 projection: ServerConfigProjection | null,226366 initialConfig: ServerConfig | null,227367 ): ServerConfig | null {228- if (projection?.source === "live") return projection.config;368+ if (369+ projection?.source === "live" &&370+ (initialConfig === null ||371+ projection.config.environment.serverVersion === initialConfig.environment.serverVersion)372+ ) {373+ return projection.config;374+ }
- packages/client-runtime/src/state/server.ts line 471Resolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -273,0 +443,21 @@443+ const stateAtom = serverUpdateStateAtom(target.environmentId);444+ const targetVersion = target.input.targetVersion;445+ let fromVersion = targetVersion;446+ let currentStage: ServerUpdateStage = "downloading";447+448+ return Effect.gen(function* () {449+ const environmentRegistry = yield* EnvironmentRegistry;450+ const result = yield* scheduleAtomCommandEffect(451+ atomRegistry,452+ configScheduler,453+ configConcurrency,454+ target,455+ Effect.gen(function* () {456+ const currentConfig = atomRegistry.get(configValueAtom(target.environmentId));457+ fromVersion = currentConfig?.environment.serverVersion ?? targetVersion;458+ atomRegistry.set(stateAtom, {459+ status: "running",460+ stage: currentStage,461+ fromVersion,462+ targetVersion,463+ });
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.


Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
889648f8-58c3-448c-9dbd-cc10df48905cYou can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share