Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

updateHandoff.ts

60 lines2.2 KB

1import { DESKTOP_UPDATE_RESTART_MARKER_FILE } from "@t3tools/contracts";
2import * as Clock from "effect/Clock";
3import * as Duration from "effect/Duration";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6import * as Option from "effect/Option";
7import * as Path from "effect/Path";
8​
9import * as ServerConfig from "../config.ts";
10import {
11 SERVICE_STATE_FILE,
12 SERVICE_STOP_MARKER_FILE,
13 serviceStateHasPendingUpdate,
14} from "./serviceProtocol.ts";
15​
16const DESKTOP_UPDATE_RESTART_MARKER_TTL = Duration.minutes(1);
17​
18export const pendingServiceUpdateExists = Effect.gen(function* () {
19 const config = yield* ServerConfig.ServerConfig;
20 const fs = yield* FileSystem.FileSystem;
21 const path = yield* Path.Path;
22 const runtimeDir = path.join(config.baseDir, "runtime");
23 const stateText = yield* fs
24 .readFileString(path.join(runtimeDir, SERVICE_STATE_FILE))
25 .pipe(Effect.option);
26 return Option.isSome(stateText) && serviceStateHasPendingUpdate(stateText.value);
27});
28​
29export const pendingUpdateHandoffExists = Effect.gen(function* () {
30 if (!(yield* pendingServiceUpdateExists)) {
31 return false;
32 }
33 const config = yield* ServerConfig.ServerConfig;
34 const fs = yield* FileSystem.FileSystem;
35 const path = yield* Path.Path;
36 const runtimeDir = path.join(config.baseDir, "runtime");
37 const stopping = yield* fs
38 .exists(path.join(runtimeDir, SERVICE_STOP_MARKER_FILE))
39 .pipe(Effect.orElseSucceed(() => false));
40 return !stopping;
41});
42​
43// Reading consumes it, so shutdown checks it first.
44export const desktopUpdateRestartPending = Effect.gen(function* () {
45 const config = yield* ServerConfig.ServerConfig;
46 const fs = yield* FileSystem.FileSystem;
47 const path = yield* Path.Path;
48 const markerPath = path.join(config.baseDir, "runtime", DESKTOP_UPDATE_RESTART_MARKER_FILE);
49 const marker = yield* fs.stat(markerPath).pipe(Effect.option);
50 if (Option.isNone(marker)) {
51 return false;
52 }
53 yield* fs.remove(markerPath).pipe(Effect.ignore);
54 const now = yield* Clock.currentTimeMillis;
55 return Option.match(marker.value.mtime, {
56 onNone: () => false,
57 onSome: (writtenAt) =>
58 now - writtenAt.getTime() < Duration.toMillis(DESKTOP_UPDATE_RESTART_MARKER_TTL),
59 });
60});