Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
77 lines
3.2 KiB
TypeScript
77 lines
3.2 KiB
TypeScript
/**
|
|
* Whether this server is going down only to be replaced by an update. Shutdown
|
|
* keeps the managed tunnel across those restarts instead of releasing it.
|
|
*/
|
|
import { DESKTOP_UPDATE_RESTART_MARKER_FILE } from "@t3tools/contracts";
|
|
import * as Clock from "effect/Clock";
|
|
import * as Duration from "effect/Duration";
|
|
import * as Effect from "effect/Effect";
|
|
import * as FileSystem from "effect/FileSystem";
|
|
import * as Option from "effect/Option";
|
|
import * as Path from "effect/Path";
|
|
|
|
import * as ServerConfig from "../config.ts";
|
|
import {
|
|
SERVICE_STATE_FILE,
|
|
SERVICE_STOP_MARKER_FILE,
|
|
serviceStateHasPendingUpdate,
|
|
} from "./serviceProtocol.ts";
|
|
|
|
// The desktop app stops its backends within seconds of writing the marker.
|
|
const DESKTOP_UPDATE_RESTART_MARKER_TTL = Duration.minutes(1);
|
|
|
|
// The launcher owns this durable state, so read it directly both when a trial
|
|
// decides whether it owns pre-activation cleanup and while a server tears down.
|
|
export const pendingServiceUpdateExists = Effect.gen(function* () {
|
|
const config = yield* ServerConfig.ServerConfig;
|
|
const fs = yield* FileSystem.FileSystem;
|
|
const path = yield* Path.Path;
|
|
const runtimeDir = path.join(config.baseDir, "runtime");
|
|
const stateText = yield* fs
|
|
.readFileString(path.join(runtimeDir, SERVICE_STATE_FILE))
|
|
.pipe(Effect.option);
|
|
return Option.isSome(stateText) && serviceStateHasPendingUpdate(stateText.value);
|
|
});
|
|
|
|
// A pending update alone is not proof a replacement server is coming: an
|
|
// explicit launcher stop (`t3 service uninstall`, `systemctl stop`,
|
|
// `launchctl bootout`) during
|
|
// the pending window also tears this server down. The launcher marks that case
|
|
// just before it signals the child, so pending + no marker is the handoff.
|
|
export const pendingUpdateHandoffExists = Effect.gen(function* () {
|
|
if (!(yield* pendingServiceUpdateExists)) {
|
|
return false;
|
|
}
|
|
const config = yield* ServerConfig.ServerConfig;
|
|
const fs = yield* FileSystem.FileSystem;
|
|
const path = yield* Path.Path;
|
|
const runtimeDir = path.join(config.baseDir, "runtime");
|
|
const stopping = yield* fs
|
|
.exists(path.join(runtimeDir, SERVICE_STOP_MARKER_FILE))
|
|
.pipe(Effect.orElseSucceed(() => false));
|
|
return !stopping;
|
|
});
|
|
|
|
// The desktop app writes its marker right before it stops this server to
|
|
// install an update, whether a remote client or the local app started it.
|
|
// Reading consumes it, so shutdown checks it first. Only a fresh marker counts,
|
|
// so a marker the server never read (a hard kill) cannot keep the tunnel on a
|
|
// later quit.
|
|
export const desktopUpdateRestartPending = Effect.gen(function* () {
|
|
const config = yield* ServerConfig.ServerConfig;
|
|
const fs = yield* FileSystem.FileSystem;
|
|
const path = yield* Path.Path;
|
|
const markerPath = path.join(config.baseDir, "runtime", DESKTOP_UPDATE_RESTART_MARKER_FILE);
|
|
const marker = yield* fs.stat(markerPath).pipe(Effect.option);
|
|
if (Option.isNone(marker)) {
|
|
return false;
|
|
}
|
|
yield* fs.remove(markerPath).pipe(Effect.ignore);
|
|
const now = yield* Clock.currentTimeMillis;
|
|
return Option.match(marker.value.mtime, {
|
|
onNone: () => false,
|
|
onSome: (writtenAt) =>
|
|
now - writtenAt.getTime() < Duration.toMillis(DESKTOP_UPDATE_RESTART_MARKER_TTL),
|
|
});
|
|
});
|