Skip to content

Commit 08b23cc

Browse files
committed
feat(desktop): download updates in the background and install on quit
The desktop app downloads an update as soon as a check finds one, instead of waiting for a click. Its server relaunches the app into it once nothing is running and no window is in use; quitting the app installs it too.
1 parent 3d81780 commit 08b23cc

3 files changed

Lines changed: 39 additions & 5 deletions

File tree

‎apps/desktop/src/updates/DesktopUpdates.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ describe("DesktopUpdates", () => {
205205
).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
206206
});
207207

208-
it.effect("checks for newer releases after an update has been downloaded", () => {
208+
it.effect("downloads newer releases found after an update has been downloaded", () => {
209209
const harness = makeHarness();
210210

211211
return Effect.scoped(
@@ -241,8 +241,9 @@ describe("DesktopUpdates", () => {
241241
harness.emit("update-available", { version: "1.2.5" });
242242
yield* flushCallbacks;
243243

244+
// A release the background poller finds starts downloading on its own.
244245
const state = yield* updates.getState;
245-
assert.equal(state.status, "available");
246+
assert.equal(state.status, "downloading");
246247
assert.equal(state.availableVersion, "1.2.5");
247248
assert.isNull(state.downloadedVersion);
248249
}),

‎apps/desktop/src/updates/DesktopUpdates.ts‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import {
5050
const AUTO_UPDATE_STARTUP_DELAY = "15 seconds";
5151
const AUTO_UPDATE_POLL_INTERVAL = "4 minutes";
5252
const PREPARED_INSTALL_CHECK_WAIT = Duration.seconds(90);
53+
const BACKGROUND_DOWNLOAD_START_WAIT = Duration.minutes(2);
5354

5455
type UpdateAction = "check" | "download" | "install" | "install-recovery" | "channel";
5556

@@ -289,6 +290,7 @@ export const make = Effect.gen(function* () {
289290
const finishedUpdateActions = yield* PubSub.unbounded<UpdateAction>();
290291
const updaterConfiguredRef = yield* Ref.make(false);
291292
const lastLoggedDownloadMilestoneRef = yield* Ref.make(-1);
293+
const lastCheckReasonRef = yield* Ref.make<string | null>(null);
292294
const updateStateRef = yield* Ref.make<DesktopUpdateState>(
293295
createInitialDesktopUpdateState(
294296
environment.appVersion,
@@ -425,6 +427,7 @@ export const make = Effect.gen(function* () {
425427

426428
if (actionReservation === "acquire" && !(yield* tryStartUpdateAction("check"))) return false;
427429

430+
yield* Ref.set(lastCheckReasonRef, reason);
428431
const check = Effect.gen(function* () {
429432
const checkedAt = yield* currentIsoTimestamp;
430433
yield* setState(reduceDesktopUpdateStateOnCheckStart(state, checkedAt));
@@ -516,6 +519,28 @@ export const make = Effect.gen(function* () {
516519
);
517520
}).pipe(Effect.withSpan("desktop.updates.downloadAvailableUpdate"));
518521

522+
// update-available fires inside the check that found it, so the download
523+
// waits for that check to release the updater. Installing stays gated: the
524+
// app's server relaunches into it when idle, and quitting installs it.
525+
const downloadInBackground = Effect.scoped(
526+
Effect.gen(function* () {
527+
const completions = yield* PubSub.subscribe(finishedUpdateActions);
528+
while (Option.isSome(yield* activeUpdateAction)) {
529+
yield* PubSub.take(completions);
530+
}
531+
if (yield* Ref.get(desktopState.quitting)) return;
532+
yield* downloadAvailableUpdate;
533+
}),
534+
).pipe(
535+
Effect.timeout(BACKGROUND_DOWNLOAD_START_WAIT),
536+
Effect.catchCause((cause) =>
537+
Cause.hasInterruptsOnly(cause)
538+
? Effect.void
539+
: logUpdaterWarning("background update download did not start"),
540+
),
541+
Effect.withSpan("desktop.updates.downloadInBackground"),
542+
);
543+
519544
// Tells the primary backend that the coming stop is an update restart, so it
520545
// keeps its managed tunnel for the backend the updated app starts. Best
521546
// effort: without the marker the backend only re-provisions its tunnel.
@@ -773,6 +798,12 @@ export const make = Effect.gen(function* () {
773798
releaseNoteGroups: releaseNotes.length,
774799
omittedReleaseCount,
775800
});
801+
// Updates found by the background pollers download on their own; a
802+
// check someone started keeps its Download button.
803+
const reason = yield* Ref.get(lastCheckReasonRef);
804+
if (reason === "startup" || reason === "poll") {
805+
yield* Effect.forkDetach(downloadInBackground);
806+
}
776807
}),
777808
),
778809
Effect.catchCause((cause) => {
@@ -931,7 +962,8 @@ export const make = Effect.gen(function* () {
931962
yield* Ref.set(updaterConfiguredRef, true);
932963

933964
yield* electronUpdater.setAutoDownload(false);
934-
yield* electronUpdater.setAutoInstallOnAppQuit(false);
965+
// A download that is never relaunched into installs when the user quits.
966+
yield* electronUpdater.setAutoInstallOnAppQuit(true);
935967
yield* applyAutoUpdaterChannel(settings.updateChannel);
936968
yield* electronUpdater.setDisableDifferentialDownload(
937969
isArm64HostRunningIntelBuild(environment.runtimeInfo),

‎docs/user/updating.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ Then it installs:
8282
- provider CLI updates that have a one-click update;
8383
- the newest T3 Code release on its channel, when it runs as a background service
8484
(`t3 service install`). It downloads first and restarts only inside that window;
85-
- a desktop app update that has finished downloading, when no desktop window is
86-
focused or the machine has been idle for 15 minutes.
85+
- a desktop app update, which downloads in the background as soon as it is
86+
published and installs when no desktop window is focused or the machine has
87+
been idle for 15 minutes. Quitting the app also installs a downloaded update.
8788

8889
Servers started from a terminal or with `npx` are never replaced automatically.
8990
A failed provider update is retried after 6 hours or when a newer version is

0 commit comments

Comments
 (0)