@evandu/super-pi 0.1.242 → 0.1.243

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,69 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { readFileSync, renameSync, writeFileSync } from "node:fs";
3
+ import { updateIsActive } from "../shared/update-status.js";
4
+ import { checkDiskVersion } from "./version.js";
5
+ /** Keeps the last operation available to the service that comes up after a restart. */
6
+ export class UpdateTracker {
7
+ file;
8
+ root;
9
+ operation = null;
10
+ constructor(file, root) {
11
+ this.file = file;
12
+ this.root = root;
13
+ try {
14
+ const value = JSON.parse(readFileSync(file, "utf8"));
15
+ if (value?.id && ["installing", "restarting", "completed", "failed"].includes(value.phase))
16
+ this.operation = value;
17
+ }
18
+ catch { /* A new service has no update history. */ }
19
+ const previous = this.operation;
20
+ if (!previous || !updateIsActive(previous))
21
+ return;
22
+ const versions = checkDiskVersion(this.root);
23
+ if (previous.phase === "restarting" && previous.targetVersion === versions.runningVersion && !versions.needsRestart) {
24
+ this.change(previous.id, { phase: "completed" });
25
+ }
26
+ else if (previous.phase === "restarting") {
27
+ this.fail(previous.id, "restarting", `服务已重新启动,当前运行 v${versions.runningVersion}。请核对已安装版本。`);
28
+ }
29
+ else
30
+ this.fail(previous.id, "installing", "上一次安装期间服务退出。请检查已安装版本后重试。");
31
+ }
32
+ save(operation) {
33
+ const temporary = `${this.file}.${process.pid}.tmp`;
34
+ writeFileSync(temporary, JSON.stringify(operation), { encoding: "utf8", mode: 0o600 });
35
+ renameSync(temporary, this.file);
36
+ }
37
+ snapshot() { return { ...checkDiskVersion(this.root), operation: this.operation }; }
38
+ begin(action, targetVersion) {
39
+ const { runningVersion } = checkDiskVersion(this.root);
40
+ const startedAt = new Date().toISOString();
41
+ const operation = { id: randomUUID(), action, phase: action === "update" ? "installing" : "restarting",
42
+ fromVersion: runningVersion, targetVersion, startedAt, changedAt: startedAt };
43
+ this.save(operation);
44
+ this.operation = operation;
45
+ return operation.id;
46
+ }
47
+ change(id, change) {
48
+ if (this.operation?.id !== id || !updateIsActive(this.operation))
49
+ return;
50
+ const operation = { ...this.operation, ...change, changedAt: new Date().toISOString() };
51
+ this.operation = operation;
52
+ try {
53
+ this.save(operation);
54
+ }
55
+ catch (error) {
56
+ console.warn("Super Pi: could not save update status:", error);
57
+ }
58
+ }
59
+ restarting(id, targetVersion) {
60
+ if (this.operation?.id !== id || this.operation.phase !== "installing")
61
+ return;
62
+ const operation = { ...this.operation, phase: "restarting", targetVersion, changedAt: new Date().toISOString() };
63
+ this.save(operation);
64
+ this.operation = operation;
65
+ }
66
+ fail(id, step, message, code) {
67
+ this.change(id, { phase: "failed", failure: { step, message, code } });
68
+ }
69
+ }
@@ -41,20 +41,3 @@ export declare function packageUpdateArgs(rootDir: string, version?: string, pla
41
41
  export declare function claimUpdateSlot(): boolean;
42
42
  /** Releases the update slot, so the next update can start. */
43
43
  export declare function releaseUpdateSlot(): void;
44
- /**
45
- * Whether an update is running RIGHT NOW.
46
- *
47
- * The client used to keep its buttons disabled only for as long as its own request was in
48
- * flight, which ended the moment the server answered 202 - while npm was still installing.
49
- * The server knew the whole time (it holds the slot above), so it says so out loud and the
50
- * client asks.
51
- */
52
- export declare function isUpdateInFlight(): boolean;
53
- /** Records the outcome of an update attempt. */
54
- export declare function recordUpdateOutcome(ok: boolean, code?: number): void;
55
- /** The most recent update outcome, or undefined when no update has run in this process. */
56
- export declare function getLastUpdateOutcome(): {
57
- ok: boolean;
58
- code?: number;
59
- at: string;
60
- } | undefined;
@@ -69,29 +69,3 @@ export function claimUpdateSlot() {
69
69
  export function releaseUpdateSlot() {
70
70
  updateInFlight = false;
71
71
  }
72
- /**
73
- * Whether an update is running RIGHT NOW.
74
- *
75
- * The client used to keep its buttons disabled only for as long as its own request was in
76
- * flight, which ended the moment the server answered 202 - while npm was still installing.
77
- * The server knew the whole time (it holds the slot above), so it says so out loud and the
78
- * client asks.
79
- */
80
- export function isUpdateInFlight() {
81
- return updateInFlight;
82
- }
83
- /**
84
- * WHAT THE LAST UPDATE ACTUALLY DID.
85
- *
86
- * The outcome of the most recent update, so the client can tell the user what actually
87
- * happened instead of showing a spinner forever.
88
- */
89
- let lastUpdate;
90
- /** Records the outcome of an update attempt. */
91
- export function recordUpdateOutcome(ok, code) {
92
- lastUpdate = { ok, code, at: new Date().toISOString() };
93
- }
94
- /** The most recent update outcome, or undefined when no update has run in this process. */
95
- export function getLastUpdateOutcome() {
96
- return lastUpdate;
97
- }
@@ -42,6 +42,11 @@ export type GroupReply = GroupPost & {
42
42
  assignment?: string;
43
43
  routeReason?: string;
44
44
  background?: boolean;
45
+ settledAt?: string;
46
+ handoff?: {
47
+ memberId: string;
48
+ assignment: string;
49
+ };
45
50
  };
46
51
  export type GroupMessage = GroupUserPost | GroupReply;
47
52
  export type GroupSummary = {
@@ -0,0 +1,28 @@
1
+ export type UpdateVersions = {
2
+ runningVersion: string;
3
+ diskVersion: string;
4
+ needsRestart: boolean;
5
+ };
6
+ export type UpdatePhase = "installing" | "restarting" | "completed" | "failed";
7
+ export type UpdateOperation = {
8
+ id: string;
9
+ action: "update" | "restart";
10
+ phase: UpdatePhase;
11
+ fromVersion: string;
12
+ targetVersion?: string;
13
+ startedAt: string;
14
+ changedAt: string;
15
+ failure?: {
16
+ step: "installing" | "restarting";
17
+ message: string;
18
+ code?: number;
19
+ };
20
+ };
21
+ export type UpdateStatus = UpdateVersions & {
22
+ operation: UpdateOperation | null;
23
+ };
24
+ export type UpdateCheck = UpdateStatus & {
25
+ latestVersion?: string;
26
+ updateAvailable: boolean;
27
+ };
28
+ export declare const updateIsActive: (operation?: UpdateOperation | null) => boolean;
@@ -0,0 +1 @@
1
+ export const updateIsActive = (operation) => operation?.phase === "installing" || operation?.phase === "restarting";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@evandu/super-pi",
3
- "version": "0.1.242",
3
+ "version": "0.1.243",
4
4
  "description": "Web control plane and companion for Pi",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -91,6 +91,14 @@ child.unref();
91
91
  // the new server is LEFT RUNNING: a start that is merely slow will still come up, and a process
92
92
  // that is stuck is worth more than no process at all.
93
93
  const READINESS_BUDGET_MS = 120000;
94
- for (let elapsed = 0; elapsed < READINESS_BUDGET_MS; elapsed += 200) { try { const response = await fetch(`http://127.0.0.1:${port}/api/health`); const body = await response.json(); if (response.ok && body.status === "ok" && body.service === "super-pi") { console.log("Server ready: /api/health passed."); process.exit(0); } } catch {} await waitMs(200); }
94
+ const deadline = Date.now() + READINESS_BUDGET_MS;
95
+ while (Date.now() < deadline) {
96
+ try {
97
+ const response = await fetch(`http://127.0.0.1:${port}/api/health`, { signal: AbortSignal.timeout(2_000) });
98
+ const body = await response.json();
99
+ if (response.ok && body.status === "ok" && body.service === "super-pi") { console.log("Server ready: /api/health passed."); process.exit(0); }
100
+ } catch { /* The replacement is still starting. */ }
101
+ await waitMs(200);
102
+ }
95
103
  console.error(`Server did not become reachable within ${READINESS_BUDGET_MS}ms. It is STILL RUNNING - do not kill it, a slow start still ends up serving. See ${logPath}:`); try { console.error(readFileSync(logPath, "utf8").slice(-8000)); } catch {}
96
104
  process.exit(1);