@femtomc/mu-server 26.2.55 → 26.2.56
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.
- package/README.md +33 -4
- package/dist/control_plane.d.ts +61 -2
- package/dist/control_plane.js +621 -37
- package/dist/generation_supervisor.d.ts +21 -0
- package/dist/generation_supervisor.js +107 -0
- package/dist/server.d.ts +5 -2
- package/dist/server.js +495 -23
- package/package.json +1 -1
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { GenerationReloadAttempt, GenerationSupervisorSnapshot, ReloadableGenerationIdentity, ReloadLifecycleReason } from "@femtomc/mu-control-plane";
|
|
2
|
+
export type ControlPlaneGenerationSupervisorOpts = {
|
|
3
|
+
supervisorId?: string;
|
|
4
|
+
nowMs?: () => number;
|
|
5
|
+
initialGeneration?: ReloadableGenerationIdentity | null;
|
|
6
|
+
};
|
|
7
|
+
export type BeginGenerationReloadResult = {
|
|
8
|
+
attempt: GenerationReloadAttempt;
|
|
9
|
+
coalesced: boolean;
|
|
10
|
+
};
|
|
11
|
+
export declare class ControlPlaneGenerationSupervisor {
|
|
12
|
+
#private;
|
|
13
|
+
constructor(opts?: ControlPlaneGenerationSupervisorOpts);
|
|
14
|
+
beginReload(reason: ReloadLifecycleReason): BeginGenerationReloadResult;
|
|
15
|
+
markSwapInstalled(attemptId: string): boolean;
|
|
16
|
+
rollbackSwapInstalled(attemptId: string): boolean;
|
|
17
|
+
finishReload(attemptId: string, outcome: "success" | "failure"): boolean;
|
|
18
|
+
activeGeneration(): ReloadableGenerationIdentity | null;
|
|
19
|
+
pendingReload(): GenerationReloadAttempt | null;
|
|
20
|
+
snapshot(): GenerationSupervisorSnapshot;
|
|
21
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
function cloneGeneration(generation) {
|
|
2
|
+
if (!generation) {
|
|
3
|
+
return null;
|
|
4
|
+
}
|
|
5
|
+
return { ...generation };
|
|
6
|
+
}
|
|
7
|
+
function cloneAttempt(attempt) {
|
|
8
|
+
if (!attempt) {
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
return {
|
|
12
|
+
...attempt,
|
|
13
|
+
from_generation: cloneGeneration(attempt.from_generation),
|
|
14
|
+
to_generation: { ...attempt.to_generation },
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
export class ControlPlaneGenerationSupervisor {
|
|
18
|
+
#supervisorId;
|
|
19
|
+
#nowMs;
|
|
20
|
+
#generationSeq;
|
|
21
|
+
#attemptSeq = 0;
|
|
22
|
+
#activeGeneration;
|
|
23
|
+
#pendingReload = null;
|
|
24
|
+
#lastReload = null;
|
|
25
|
+
constructor(opts = {}) {
|
|
26
|
+
this.#supervisorId = opts.supervisorId?.trim() || "control-plane";
|
|
27
|
+
this.#nowMs = opts.nowMs ?? Date.now;
|
|
28
|
+
this.#activeGeneration = cloneGeneration(opts.initialGeneration ?? null);
|
|
29
|
+
this.#generationSeq = this.#activeGeneration?.generation_seq ?? -1;
|
|
30
|
+
}
|
|
31
|
+
#nextGeneration() {
|
|
32
|
+
const nextSeq = this.#generationSeq + 1;
|
|
33
|
+
return {
|
|
34
|
+
generation_id: `${this.#supervisorId}-gen-${nextSeq}`,
|
|
35
|
+
generation_seq: nextSeq,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
beginReload(reason) {
|
|
39
|
+
if (this.#pendingReload) {
|
|
40
|
+
return {
|
|
41
|
+
attempt: cloneAttempt(this.#pendingReload),
|
|
42
|
+
coalesced: true,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
this.#attemptSeq += 1;
|
|
46
|
+
const nowMs = Math.trunc(this.#nowMs());
|
|
47
|
+
const attempt = {
|
|
48
|
+
attempt_id: `${this.#supervisorId}-reload-${this.#attemptSeq.toString(36)}`,
|
|
49
|
+
reason,
|
|
50
|
+
state: "planned",
|
|
51
|
+
requested_at_ms: nowMs,
|
|
52
|
+
swapped_at_ms: null,
|
|
53
|
+
finished_at_ms: null,
|
|
54
|
+
from_generation: cloneGeneration(this.#activeGeneration),
|
|
55
|
+
to_generation: this.#nextGeneration(),
|
|
56
|
+
};
|
|
57
|
+
this.#pendingReload = attempt;
|
|
58
|
+
return {
|
|
59
|
+
attempt: cloneAttempt(attempt),
|
|
60
|
+
coalesced: false,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
markSwapInstalled(attemptId) {
|
|
64
|
+
if (!this.#pendingReload || this.#pendingReload.attempt_id !== attemptId) {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
this.#pendingReload.state = "swapped";
|
|
68
|
+
this.#pendingReload.swapped_at_ms = Math.trunc(this.#nowMs());
|
|
69
|
+
this.#activeGeneration = { ...this.#pendingReload.to_generation };
|
|
70
|
+
this.#generationSeq = this.#pendingReload.to_generation.generation_seq;
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
rollbackSwapInstalled(attemptId) {
|
|
74
|
+
if (!this.#pendingReload ||
|
|
75
|
+
this.#pendingReload.attempt_id !== attemptId ||
|
|
76
|
+
this.#pendingReload.swapped_at_ms == null) {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
this.#activeGeneration = cloneGeneration(this.#pendingReload.from_generation);
|
|
80
|
+
this.#generationSeq = this.#activeGeneration?.generation_seq ?? -1;
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
finishReload(attemptId, outcome) {
|
|
84
|
+
if (!this.#pendingReload || this.#pendingReload.attempt_id !== attemptId) {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
this.#pendingReload.state = outcome === "success" ? "completed" : "failed";
|
|
88
|
+
this.#pendingReload.finished_at_ms = Math.trunc(this.#nowMs());
|
|
89
|
+
this.#lastReload = cloneAttempt(this.#pendingReload);
|
|
90
|
+
this.#pendingReload = null;
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
activeGeneration() {
|
|
94
|
+
return cloneGeneration(this.#activeGeneration);
|
|
95
|
+
}
|
|
96
|
+
pendingReload() {
|
|
97
|
+
return cloneAttempt(this.#pendingReload);
|
|
98
|
+
}
|
|
99
|
+
snapshot() {
|
|
100
|
+
return {
|
|
101
|
+
supervisor_id: this.#supervisorId,
|
|
102
|
+
active_generation: cloneGeneration(this.#activeGeneration),
|
|
103
|
+
pending_reload: cloneAttempt(this.#pendingReload),
|
|
104
|
+
last_reload: cloneAttempt(this.#lastReload),
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
}
|
package/dist/server.d.ts
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
|
+
import { GenerationTelemetryRecorder, type ReloadableGenerationIdentity } from "@femtomc/mu-control-plane";
|
|
1
2
|
import type { EventEnvelope, JsonlStore } from "@femtomc/mu-core";
|
|
2
3
|
import { EventLog } from "@femtomc/mu-core/node";
|
|
3
4
|
import { ForumStore } from "@femtomc/mu-forum";
|
|
4
5
|
import { IssueStore } from "@femtomc/mu-issue";
|
|
5
|
-
import { type MuConfig } from "./config.js";
|
|
6
6
|
import { ControlPlaneActivitySupervisor } from "./activity_supervisor.js";
|
|
7
|
+
import { type MuConfig } from "./config.js";
|
|
7
8
|
import { type ControlPlaneConfig, type ControlPlaneHandle } from "./control_plane.js";
|
|
8
|
-
import { ActivityHeartbeatScheduler } from "./heartbeat_scheduler.js";
|
|
9
9
|
import { HeartbeatProgramRegistry } from "./heartbeat_programs.js";
|
|
10
|
+
import { ActivityHeartbeatScheduler } from "./heartbeat_scheduler.js";
|
|
10
11
|
type ControlPlaneReloader = (opts: {
|
|
11
12
|
repoRoot: string;
|
|
12
13
|
previous: ControlPlaneHandle | null;
|
|
13
14
|
config: ControlPlaneConfig;
|
|
15
|
+
generation: ReloadableGenerationIdentity;
|
|
14
16
|
}) => Promise<ControlPlaneHandle | null>;
|
|
15
17
|
type ConfigReader = (repoRoot: string) => Promise<MuConfig>;
|
|
16
18
|
type ConfigWriter = (repoRoot: string, config: MuConfig) => Promise<string>;
|
|
@@ -21,6 +23,7 @@ export type ServerOptions = {
|
|
|
21
23
|
heartbeatScheduler?: ActivityHeartbeatScheduler;
|
|
22
24
|
activitySupervisor?: ControlPlaneActivitySupervisor;
|
|
23
25
|
controlPlaneReloader?: ControlPlaneReloader;
|
|
26
|
+
generationTelemetry?: GenerationTelemetryRecorder;
|
|
24
27
|
config?: MuConfig;
|
|
25
28
|
configReader?: ConfigReader;
|
|
26
29
|
configWriter?: ConfigWriter;
|