Skip to content

Commit 4c7e22b

Browse files
committed
feat(server): background services update themselves
1 parent 590da14 commit 4c7e22b

35 files changed

Lines changed: 1660 additions & 201 deletions

‎apps/mobile/src/features/settings/SettingsServerControlsRouteScreen.tsx‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -552,15 +552,11 @@ function ServerSettingsDetail(props: { readonly page: SettingsPage }) {
552552
: !supportsAutomaticUpdates
553553
? "Update older servers to control automatic updates."
554554
: uniform("enableProviderUpdateChecks") === false
555-
? "Turn on Provider update checks to update automatically."
556-
: "Install updates for provider CLIs that support one-click updates on the selected environments while nothing is running."
555+
? "Update background-service installs on the selected environments while nothing is running. Provider CLIs update only while Provider update checks is on."
556+
: "Install provider CLI updates, and update background-service installs, on the selected environments while nothing is running."
557557
}
558558
value={uniform("automaticUpdates")}
559-
disabled={
560-
disabledFor("automaticUpdates") ||
561-
!supportsAutomaticUpdates ||
562-
uniform("enableProviderUpdateChecks") === false
563-
}
559+
disabled={disabledFor("automaticUpdates") || !supportsAutomaticUpdates}
564560
onValueChange={(value) => write({ automaticUpdates: value })}
565561
/>
566562
</View>

‎apps/server/src/cli/update.test.ts‎

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { assert, it } from "@effect/vitest";
33
import type { ManagedUpdatesPolicy } from "@t3tools/contracts";
44
import * as HostProcess from "@t3tools/shared/HostProcess";
55
import * as Duration from "effect/Duration";
6+
import * as DateTime from "effect/DateTime";
67
import * as Effect from "effect/Effect";
78
import * as Exit from "effect/Exit";
89
import * as FileSystem from "effect/FileSystem";
@@ -40,6 +41,11 @@ const unrelatedLsof = ProcessRunner.ProcessRunner.of({
4041
stderrInvalidUtf8: false,
4142
}),
4243
});
44+
import {
45+
SERVICE_LAUNCHER_PROTOCOL,
46+
SERVICE_RESTART_PENDING_FILE,
47+
} from "../cloud/serviceProtocol.ts";
48+
import { pruneServiceRuntimes } from "../serviceLauncher.ts";
4349

4450
afterEach(() => vi.restoreAllMocks());
4551

@@ -191,17 +197,21 @@ it.layer(NodeServices.layer)("t3 update under a managed policy", (it) => {
191197
});
192198

193199
it.layer(NodeServices.layer)("t3 update launcher", (it) => {
194-
it.effect("repoints a symlink that lives in a runtime versions tree", () =>
200+
it.effect("keeps a repointed CLI runtime after subsequent service updates", () =>
195201
Effect.gen(function* () {
196202
const fs = yield* FileSystem.FileSystem;
197203
const path = yield* Path.Path;
198204
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-update-" });
199205
const oldExe = path.join(root, "runtime/versions/1.0.0/t3");
200206
const newExe = path.join(root, "runtime/versions/2.0.0/t3");
201207
const launcher = path.join(root, "bin/t3");
202-
for (const file of [oldExe, newExe]) {
208+
const twoHoursAgo = DateTime.toDate(DateTime.subtract(yield* DateTime.now, { hours: 2 }));
209+
for (const version of ["1.0.0", "2.0.0"]) {
210+
const file = path.join(root, "runtime/versions", version, "t3");
203211
yield* fs.makeDirectory(path.dirname(file), { recursive: true });
204-
yield* fs.writeFileString(file, "");
212+
yield* fs.writeFileString(file, version);
213+
yield* fs.writeFileString(path.join(path.dirname(file), ".install-complete"), version);
214+
yield* fs.utimes(path.dirname(file), twoHoursAgo, twoHoursAgo);
205215
}
206216
yield* fs.makeDirectory(path.dirname(launcher), { recursive: true });
207217
yield* fs.symlink(oldExe, launcher);
@@ -214,6 +224,41 @@ it.layer(NodeServices.layer)("t3 update launcher", (it) => {
214224

215225
assert.deepStrictEqual(Option.getOrUndefined(repointed), launcher);
216226
assert.equal(yield* fs.readLink(launcher), newExe);
227+
assert.isFalse(yield* fs.exists(path.join(root, "runtime", SERVICE_RESTART_PENDING_FILE)));
228+
for (const version of [3, 4]) {
229+
const versionDir = path.join(root, "runtime/versions", `${version}.0.0`);
230+
yield* fs.makeDirectory(versionDir);
231+
yield* fs.writeFileString(path.join(versionDir, "t3"), `${version}.0.0`);
232+
yield* fs.writeFileString(path.join(versionDir, ".install-complete"), `${version}.0.0`);
233+
yield* fs.utimes(versionDir, twoHoursAgo, twoHoursAgo);
234+
if (version === 4) {
235+
const tempLink = `${launcher}.${process.pid}.tmp`;
236+
yield* fs.writeFileString(tempLink, "");
237+
yield* repointLauncher({
238+
launchedAs: launcher,
239+
baseDir: root,
240+
targetEntryPath: path.join(versionDir, "t3"),
241+
}).pipe(Effect.flip);
242+
yield* fs.remove(tempLink);
243+
}
244+
yield* Effect.promise(() =>
245+
pruneServiceRuntimes(
246+
root,
247+
{
248+
protocol: SERVICE_LAUNCHER_PROTOCOL,
249+
activeVersion: `${version}.0.0`,
250+
update: {
251+
id: `update-${version}`,
252+
fromVersion: `${version - 1}.0.0`,
253+
targetVersion: `${version}.0.0`,
254+
status: "committed",
255+
},
256+
},
257+
"1.0.0",
258+
),
259+
);
260+
}
261+
assert.equal(yield* fs.readFileString(launcher), "2.0.0");
217262
}).pipe(Effect.scoped, Effect.provideService(HostProcess.Platform, "linux")),
218263
);
219264

‎apps/server/src/cli/update.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ const ReleaseIndex = Schema.Array(
5555
Schema.Struct({
5656
tag_name: Schema.String,
5757
draft: Schema.optional(Schema.Boolean),
58+
prerelease: Schema.optional(Schema.Boolean),
5859
}),
5960
);
6061
const decodeReleaseIndex = Schema.decodeUnknownEffect(Schema.fromJsonString(ReleaseIndex));

‎apps/server/src/cloud/pinnedRuntime.test.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
pinnedRuntimeCommand,
2121
pinnedRuntimePaths,
2222
PinnedRuntimeInstallError,
23+
PinnedRuntimePreflightBlockedError,
2324
type PinnedRuntimeProgress,
2425
prunePinnedRuntimes,
2526
} from "./pinnedRuntime.ts";
@@ -246,7 +247,10 @@ it.layer(NodeServices.layer)("ensurePinnedRuntimeInstalled", (it) => {
246247
}),
247248
);
248249

249-
it.effect("removes staging and leaves no final runtime when validation fails", () =>
250+
it.effect.each([
251+
new PinnedRuntimeInstallError({ step: "validating the staged runtime" }),
252+
new PinnedRuntimePreflightBlockedError({ version, reason: "Different client protocol." }),
253+
])("removes staging and leaves no final runtime when validation fails: %s", (error) =>
250254
Effect.gen(function* () {
251255
const fs = yield* FileSystem.FileSystem;
252256
const path = yield* Path.Path;
@@ -262,8 +266,7 @@ it.layer(NodeServices.layer)("ensurePinnedRuntimeInstalled", (it) => {
262266
arch: "x64",
263267
httpClient: releaseHttpClient(yield* validChecksums),
264268
runner: extractingRunner(fs, path),
265-
validate: () =>
266-
Effect.fail(new PinnedRuntimeInstallError({ step: "validating the staged runtime" })),
269+
validate: () => Effect.fail(error),
267270
}).pipe(Effect.flip);
268271

269272
assert.isFalse(yield* fs.exists(finalPaths.versionDir));

0 commit comments

Comments
 (0)