Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pm-stack/console/edge/src/edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,10 @@ export class Edge {
if (this.outward.length > OUTWARD_KEPT) this.outward.splice(0, this.outward.length - OUTWARD_KEPT);
},
});
// The Platform screen's round of Worker checks (#13494 step
// 3) is this object's own store: the classes read it from here
// rather than asking this Worker from outside.
made.ops.workerChecksSource = async () => this.machines.lastWorkerChecks();
this.kept = {
...made,
cfg,
Expand Down
18 changes: 17 additions & 1 deletion pm-stack/console/public/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3299,6 +3299,18 @@ const GROUP_SAID = { platform: "The platform", fleet: "Fleet", data: "Data", id:
* deploy went, and whether that needs looking at. */
export function workerSaid(w, now = Date.now()) {
if (w.state !== "in service") return { text: "Not deployed.", tone: "" };
// The edge's own last asking (#13494 step 3) is fresher than any
// deploy record: a Worker that did not answer it is said as that,
// and one that answered names the commit it is running.
if (w.check && !w.check.answering) {
return { text: `It did not answer the edge's check ${ago(new Date(w.check.askedAt).toISOString(), now)}.`, tone: "warn" };
}
if (w.check?.answering && w.check.commit) {
return {
text: `Answering: the edge asked ${ago(new Date(w.check.askedAt).toISOString(), now)}, and it named ${w.check.commit.slice(0, 7)} as the commit it runs.`,
tone: "good",
};
}
if (w.deploy !== "actions") return { text: "In service. Deployed by hand, from a machine.", tone: "warn" };
const run = w.run;
if (!run) return { text: "In service. Deployed from Actions; it has not run on main yet.", tone: "" };
Expand Down Expand Up @@ -3438,7 +3450,11 @@ async function viewPlatform() {
{ class: "meta" },
"One platform means each of these is deployed from Actions with a check and a way back, and is seen and managed from here. This list is the repository's own, and changes with a merge.",
),
p.checked ? h("p", { class: "meta" }, `Which are in service was last checked from outside on ${p.checked}.`) : null,
p.checksAt
? h("p", { class: "meta" }, `The edge last asked each of them ${ago(p.checksAt)}: which are in service below is what answered.`)
: p.checked
? h("p", { class: "meta" }, `Which are in service was last checked from outside on ${p.checked}.`)
: null,
p.malformed ? h("p", { class: "why" }, `${p.malformed} entr${p.malformed === 1 ? "y" : "ies"} in the list could not be read and ${p.malformed === 1 ? "is" : "are"} not shown.`) : null,
),
];
Expand Down
35 changes: 35 additions & 0 deletions pm-stack/console/src/notify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export interface Seen {
* what he was told: that it soon will, or that it has. Absent in a
* state written before tokens were watched. */
tokens?: Record<string, "soon" | "expired">;
/** Workers the owner has been told have stopped answering the
* edge's checks (#13494 step 3), by name. Absent in a state written
* before Workers were watched. */
workersDown?: string[];
}
export const NOTHING_SEEN: Seen = { primed: false, asks: {}, hands: [], questions: 0, failed: [] };

Expand Down Expand Up @@ -73,6 +77,9 @@ interface State {
questionCount?: { total: number } | null;
services?: { unit: string; active: string; description?: string }[] | null;
tokens?: TokenExpiry[] | null;
/** The Workers the edge's last round of checks asked, and whether
* each answered (#13494 step 3). Null where no round could be read. */
workers?: { name: string; answering: boolean }[] | null;
}

const clip = (s: string, n: number) => (s.length > n ? `${s.slice(0, n - 1)}…` : s);
Expand All @@ -99,6 +106,7 @@ export function changes(seen: Seen, now: State, at: number = Date.now()): { mess
failed: seen.failed,
...(seen.failing ? { failing: seen.failing } : {}),
...(seen.tokens ? { tokens: seen.tokens } : {}),
...(seen.workersDown ? { workersDown: seen.workersDown } : {}),
};
const messages: Message[] = [];
for (const ask of now.asks ?? []) {
Expand Down Expand Up @@ -275,6 +283,27 @@ export function changes(seen: Seen, now: State, at: number = Date.now()): { mess
for (const name of Object.keys(told)) if (!listed.has(name)) delete told[name];
next.tokens = told;
}
if (now.workers) {
// A Worker that has stopped answering the edge's checks is told
// of once, and not again until it has answered once more: the
// round is the edge's own asking (#13494 step 3), so a name here
// is a Worker in service that did not answer it. A round that
// could not be read (null) changes nothing, and a Worker the
// round no longer asks is forgotten, like a unit unlisted.
const down = now.workers.filter((w) => !w.answering).map((w) => w.name);
for (const name of down) {
if ((seen.workersDown ?? []).includes(name)) continue;
messages.push({
cls: "trouble",
title: "A Worker has stopped answering",
body: clip(`${name} did not answer the edge's latest check.`, 300),
url: "/console/#more/platform",
tag: `worker-${name}`,
actions: [{ action: "open", title: "Look", url: "/console/#more/platform" }],
});
}
next.workersDown = down;
}
return { messages: seen.primed ? messages : [], seen: next };
}

Expand All @@ -294,6 +323,9 @@ export class Notifier {
/** When the store tokens run out, where this side can read every
* token (a machine); absent where it cannot. */
private tokens?: () => Promise<TokenExpiry[]>,
/** Whether each Worker answered the edge's last round of checks
* (#13494 step 3); absent where no round can be read. */
private workers?: () => Promise<{ name: string; answering: boolean }[] | null>,
) {}

private async load(): Promise<Seen> {
Expand All @@ -318,6 +350,9 @@ export class Notifier {
const now = Object.fromEntries(WATCHED.map((n) => [n, this.docs.good(n)])) as State;
// Tokens that could not be read this pass change nothing.
if (this.tokens) now.tokens = await this.tokens().catch(() => null);
// A round of Worker checks that could not be read this pass
// changes nothing either.
if (this.workers) now.workers = await this.workers().catch(() => null);
// The services as of when they were last read, not as of now.
const { messages, seen } = changes(await this.load(), now, this.docs.goodAt("services") || this.now());
for (const m of messages) await this.push.send(m.cls, m);
Expand Down
34 changes: 32 additions & 2 deletions pm-stack/console/src/ops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import type { Config } from "./config.ts";
import { OpError } from "./errors.ts";
import { plain } from "./plain.ts";
import { WORKERS_FILE, workersList } from "./platform.ts";
import { WORKERS_FILE, type WorkerCheckSet, withWorkerChecks, workerCheckSet, workersList } from "./platform.ts";
import { STORES_FILE, storesList, type StoreRow } from "./stores.ts";
import {
type GitHubRun,
Expand Down Expand Up @@ -1382,13 +1382,43 @@ export class Ops {
flows.map(async (flow) => [flow, await last(flow).catch((e: Error) => ({ error: e.message }))] as const),
),
);
// The edge's latest round of checks (#13494 step 3): where a
// source for it is wired, each Worker the round asked carries
// what the round found, and the round's own time stands in place
// of the hand-written `checked` day. No source, or a round that
// cannot be read, leaves the list as it was: hand state only.
const checks = await this.workerChecks();
return {
checked: list.checked,
checksAt: checks ? new Date(checks.askedAt).toISOString() : null,
malformed: list.malformed,
workers: list.workers.map((w) => ({ ...w, run: w.workflow ? (runs.get(w.workflow) ?? null) : null })),
workers: withWorkerChecks(list.workers, checks).map((w) => ({
...w,
run: w.workflow ? (runs.get(w.workflow) ?? null) : null,
})),
};
}

/** Where the edge's last round of Worker checks is read from, where
* one is wired: at the edge, the object's own store; on a machine,
* the edge's health answer. Set by the side that starts this Ops;
* null where neither can be reached, and the Platform screen then
* shows the hand-written list alone. */
workerChecksSource: (() => Promise<unknown>) | null = null;

/** The edge's last round of Worker checks (#15793's store, read for
* #13494 step 3), or null when there is no source wired or what it
* gives is not a round: a round that cannot be read says nothing,
* rather than half of something. */
async workerChecks(): Promise<WorkerCheckSet | null> {
if (!this.workerChecksSource) return null;
try {
return workerCheckSet(await this.workerChecksSource());
} catch {
return null;
}
}

/** When each store token the platform holds runs out (#14255): the
* tokens the repository's list of stores names
* (pm-stack/platform/stores.json), each fetched by name the way the
Expand Down
60 changes: 60 additions & 0 deletions pm-stack/console/src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,63 @@ export function workersList(value: unknown): { checked: string | null; workers:
malformed: given.length - workers.length,
};
}

/** One Worker, as one asking of the edge's scheduled checks found it
* (#13494 step 3): the screen's copy of the shape the edge keeps
* (edge/src/worker-checks.ts). The two are kept alike by the tests in
* test/worker-checks.test.ts, which read both. */
export interface WorkerCheck {
name: string;
answering: boolean;
commit: string | null;
askedAt: number;
via: string | null;
status: number | null;
}

/** One round of the edge's asking: every check of the round, and when
* it ran. */
export interface WorkerCheckSet {
askedAt: number;
checks: WorkerCheck[];
}

/** A stored round of checks, read back: the set, or null when what is
* given is not one. The rules are the edge's own (its
* parseWorkerCheckSet): a round that cannot be read whole is not
* read at all, so the screen never shows half a round as the truth. */
export function workerCheckSet(value: unknown): WorkerCheckSet | null {
if (typeof value !== "object" || value === null) return null;
const record = value as Record<string, unknown>;
if (typeof record.askedAt !== "number" || !Array.isArray(record.checks)) return null;
const checks: WorkerCheck[] = [];
for (const item of record.checks) {
if (typeof item !== "object" || item === null) return null;
const check = item as Record<string, unknown>;
if (typeof check.name !== "string" || typeof check.answering !== "boolean") return null;
if (check.commit !== null && typeof check.commit !== "string") return null;
if (typeof check.askedAt !== "number") return null;
if (check.via !== null && typeof check.via !== "string") return null;
if (check.status !== null && typeof check.status !== "number") return null;
checks.push({
name: check.name,
answering: check.answering,
commit: check.commit as string | null,
askedAt: check.askedAt,
via: check.via as string | null,
status: check.status as number | null,
});
}
return { askedAt: record.askedAt, checks };
}

/** The list's rows with the round's answer for each: a row the round
* asked carries its check; one it did not (not in service, or no
* round yet) carries null, and the screen says no more than that. */
export function withWorkerChecks<T extends { name: string }>(
workers: T[],
set: WorkerCheckSet | null,
): (T & { check: WorkerCheck | null })[] {
const byName = new Map((set?.checks ?? []).map((c) => [c.name, c]));
return workers.map((w) => ({ ...w, check: byName.get(w.name) ?? null }));
}
17 changes: 17 additions & 0 deletions pm-stack/console/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,19 @@ if (import.meta.main) {
const check = process.argv.includes("--check");
const cfg = readConfig();
const ops = new Ops(cfg);
// The edge's rounds of Worker checks (#13494 step 3) are read from
// its health answer, which is public and names no secret: the last
// round rides on it as `workerChecks`. No edge in front of this
// machine leaves the source unwired, and the screen and the
// notifier then simply have no round to show or tell of.
if (cfg.edgeUrl) {
ops.workerChecksSource = async () => {
const res = await fetch(`${cfg.edgeUrl}/console/health`);
if (!res.ok) return null;
const health = (await res.json()) as { workerChecks?: unknown };
return health.workerChecks ?? null;
};
}
mkdirSync(dirname(cfg.auditFile), { recursive: true, mode: 0o700 });
const work = new Work(ops);
const docs = new Docs(loaders(ops, work, await morePages()));
Expand All @@ -759,6 +772,10 @@ if (import.meta.main) {
fileStore(join(state, "notified.json")),
Date.now,
async () => (await ops.tokenExpiries()).tokens,
async () => {
const set = await ops.workerChecks();
return set ? set.checks.map((c) => ({ name: c.name, answering: c.answering })) : null;
},
);
const devices = new Devices(fileStore(cfg.devicesFile));
const deps: Deps = {
Expand Down
72 changes: 71 additions & 1 deletion pm-stack/console/test/worker-checks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import {
type WorkerBinding,
type WorkerCheckSet,
} from "../edge/src/worker-checks.ts";
import { workersList } from "../src/platform.ts";
import { changes, NOTHING_SEEN, type Seen } from "../src/notify.ts";
import { type WorkerCheckSet as ScreenCheckSet, withWorkerChecks, workerCheckSet, workersList } from "../src/platform.ts";

const AT = 1_800_000_000_000;

Expand Down Expand Up @@ -263,3 +264,72 @@ describe("the Edge keeps a round", () => {
expect(await edge.lastWorkerChecks()).toBeNull();
});
});

// --- step 3 of #13494: the screen's read of the round, and the telling
// of a Worker that stops answering. Oracles written out by hand: the
// round below is what the edge's store holds; what the screen may
// make of it, and what the owner is told, is spelled out case by case.

const ROUND = {
askedAt: AT,
checks: [
{ name: "mcpx", answering: true, commit: "abc1234def", askedAt: AT, via: "/version", status: 200 },
{ name: "pkarr", answering: false, commit: null, askedAt: AT, via: null, status: null },
],
};

test("the screen reads a round whole or not at all", () => {
expect(workerCheckSet(ROUND)).toEqual(ROUND as ScreenCheckSet);
for (const bad of [
null,
"a round",
{ askedAt: "just now", checks: [] },
{ askedAt: AT, checks: "mcpx" },
{ askedAt: AT, checks: [{ name: "mcpx", answering: "yes", commit: null, askedAt: AT, via: null, status: null }] },
{ askedAt: AT, checks: [{ name: "mcpx", answering: true, commit: 7, askedAt: AT, via: null, status: null }] },
{ askedAt: AT, checks: [{ name: "mcpx", answering: true, commit: null, askedAt: AT, via: null, status: "200" }] },
]) {
expect(workerCheckSet(bad)).toBeNull();
}
});

test("the list's rows carry the round's answer for each, and no more", () => {
const rows = [{ name: "mcpx" }, { name: "pkarr" }, { name: "voice-line" }];
const merged = withWorkerChecks(rows, workerCheckSet(ROUND));
expect(merged.map((w) => [w.name, w.check?.answering ?? null])).toEqual([
["mcpx", true],
["pkarr", false],
["voice-line", null],
]);
expect(merged[0].check?.commit).toBe("abc1234def");
// No round: every row carries null, and the rows are otherwise as given.
expect(withWorkerChecks(rows, null)).toEqual(rows.map((w) => ({ ...w, check: null })));
});

test("a Worker that stops answering is told of once, and again only after it answers once more", () => {
const primed: Seen = { ...NOTHING_SEEN, primed: true };
const tell = (seen: Seen, workers: { name: string; answering: boolean }[] | null) => changes(seen, { workers }, AT);
let step = tell(primed, [{ name: "pkarr", answering: false }]);
expect(step.messages).toHaveLength(1);
expect(step.messages[0]).toMatchObject({ cls: "trouble", tag: "worker-pkarr", url: "/console/#more/platform" });
expect(step.messages[0].body).toContain("pkarr");
expect(step.seen.workersDown).toEqual(["pkarr"]);
// Still down on the next round: not told again.
step = tell(step.seen, [{ name: "pkarr", answering: false }]);
expect(step.messages).toHaveLength(0);
// A round that could not be read changes nothing.
step = tell(step.seen, null);
expect(step.messages).toHaveLength(0);
expect(step.seen.workersDown).toEqual(["pkarr"]);
// Answering again re-arms it, silently.
step = tell(step.seen, [{ name: "pkarr", answering: true }]);
expect(step.messages).toHaveLength(0);
expect(step.seen.workersDown).toEqual([]);
step = tell(step.seen, [{ name: "pkarr", answering: false }]);
expect(step.messages).toHaveLength(1);
// The first pass only takes stock: nothing is told of a Worker
// already down when watching starts.
const first = changes(NOTHING_SEEN, { workers: [{ name: "pkarr", answering: false }] }, AT);
expect(first.messages).toHaveLength(0);
expect(first.seen.workersDown).toEqual(["pkarr"]);
});