Skip to content

Commit 490012d

Browse files
committed
fix(server): finish all service file restore attempts
1 parent 4f746bf commit 490012d

2 files changed

Lines changed: 41 additions & 16 deletions

File tree

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

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -809,26 +809,36 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
809809
}),
810810
);
811811

812-
it.effect.each(["linux", "darwin"] as const)(
813-
"leaves the service stopped if restoring its previous state fails on %s",
814-
(platform) =>
812+
it.effect.each(
813+
(["linux", "darwin"] as const).flatMap((platform) =>
814+
(["state", "unit"] as const).flatMap((failedRestore) =>
815+
[false, true].map((hasMarker) => ({ platform, failedRestore, hasMarker })),
816+
),
817+
),
818+
)(
819+
"restores remaining files and leaves the service stopped after $failedRestore restore failure on $platform (marker: $hasMarker)",
820+
({ platform, failedRestore, hasMarker }) =>
815821
Effect.gen(function* () {
816822
const { service, fs, baseDir, statePath, starts, commands, makeService } =
817823
yield* makeHarness(platform);
818824
const path = yield* Path.Path;
819825
const plan = yield* service.install();
826+
const markerPath = path.join(baseDir, "runtime", SERVICE_RESTART_PENDING_FILE);
827+
if (hasMarker) yield* fs.writeFileString(markerPath, "1.2.3\n");
820828
let stateWrites = 0;
821-
let unitWritten = false;
829+
let unitWrites = 0;
822830
const fileSystem = FileSystem.FileSystem.of({
823831
...fs,
824832
rename: (from, to) =>
825833
Effect.gen(function* () {
826-
if (to === statePath && ++stateWrites === 2) {
834+
if (to === statePath && ++stateWrites === 2 && failedRestore === "state") {
835+
return yield* fs.rename(path.join(baseDir, "missing-restore-source"), to);
836+
}
837+
if (to === plan.unitPath && ++unitWrites === 2 && failedRestore === "unit") {
827838
return yield* fs.rename(path.join(baseDir, "missing-restore-source"), to);
828839
}
829840
yield* fs.rename(from, to);
830-
if (to === plan.unitPath && !unitWritten) {
831-
unitWritten = true;
841+
if (to === plan.unitPath && unitWrites === 1) {
832842
yield* fs.rename(from, to);
833843
}
834844
}),
@@ -845,6 +855,16 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
845855
? "systemctl --user stop t3code.service"
846856
: "launchctl bootout --wait gui/501/com.t3tools.t3code.service",
847857
);
858+
expect(parseServiceState(yield* fs.readFileString(statePath))?.activeVersion).toBe(
859+
failedRestore === "state" ? "1.2.4" : "1.2.3",
860+
);
861+
expect(BootService.bootServiceProgramOf(yield* fs.readFileString(plan.unitPath))).toBe(
862+
pinnedRuntimePaths(path, baseDir, failedRestore === "unit" ? "1.2.4" : "1.2.3", platform)
863+
.entryPath,
864+
);
865+
expect(
866+
yield* fs.readFileString(markerPath).pipe(Effect.orElseSucceed(() => undefined)),
867+
).toBe(hasMarker ? "1.2.3\n" : undefined);
848868
expect(yield* fs.readFileString(plan.logPath)).toContain(
849869
"Could not set up the T3 Code background service.",
850870
);

‎apps/server/src/cloud/bootService.ts‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -945,10 +945,21 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
945945
if (Exit.isSuccess(exit) || !stopAttempted || activationAttempted) return Effect.void;
946946
return Effect.gen(function* () {
947947
if (previousFiles !== undefined) {
948+
let restoreError: BootServiceInstallError | undefined;
948949
for (const [filePath, contents] of previousFiles) {
949-
if (Option.isSome(contents)) yield* writeDurably(filePath, contents.value);
950-
else yield* fs.remove(filePath, { force: true });
950+
const restore = Option.isSome(contents)
951+
? writeDurably(filePath, contents.value)
952+
: fs
953+
.remove(filePath, { force: true })
954+
.pipe(Effect.mapError((cause) => new BootServiceInstallError({ cause })));
955+
yield* restore.pipe(
956+
Effect.catch((error) => {
957+
restoreError ??= error;
958+
return Effect.void;
959+
}),
960+
);
951961
}
962+
if (restoreError !== undefined) return yield* restoreError;
952963
// systemd may already have loaded the replacement definition.
953964
if (activationStarted && previousUnit !== manager.render(plan)) {
954965
yield* runSteps(manager.finalize);
@@ -971,13 +982,7 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
971982
: Effect.void;
972983
}),
973984
);
974-
}).pipe(
975-
Effect.mapError((cause) =>
976-
cause._tag === "PlatformError" ? new BootServiceInstallError({ cause }) : cause,
977-
),
978-
Effect.tapError(logFailure),
979-
Effect.ignore,
980-
);
985+
}).pipe(Effect.tapError(logFailure), Effect.ignore);
981986
}),
982987
);
983988
return plan;

0 commit comments

Comments
 (0)