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
26 changes: 16 additions & 10 deletions workers/nos-runner-watchdog/src/decide.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,17 @@ const SELF_HOSTED = "mimolette";
* Pure decision for one watchdog tick.
* @param {State} prev state saved by the previous tick
* @param {Observed} observed what this tick saw
* @param {{selfHosted: string, fallback: string}} [labels] the pool's labels; defaults to NOS
* @returns {Decision}
*/
export function decide(prev, observed) {
export function decide(
prev,
observed,
labels = { selfHosted: SELF_HOSTED, fallback: FALLBACK },
) {
const { selfHosted, fallback } = labels;
const unset = !observed.varValue;
const current = unset ? FALLBACK : observed.varValue;
const current = unset ? fallback : observed.varValue;
const external = prev.lastVar !== null && current !== prev.lastVar;
let misses = external ? 0 : (prev.misses ?? 0);
let goods = external ? 0 : (prev.goods ?? 0);
Expand All @@ -54,7 +60,7 @@ export function decide(prev, observed) {
const sameBusyTicks =
queued && allBusy && sameNames ? (prev.sameBusyTicks ?? 0) + 1 : 0;

if (current !== SELF_HOSTED && current !== FALLBACK) {
if (current !== selfHosted && current !== fallback) {
// Manual hold: leave the variable alone, alert once per new value.
return {
next: {
Expand All @@ -72,7 +78,7 @@ export function decide(prev, observed) {
};
}

if (current !== SELF_HOSTED) healthyStreak = 0;
if (current !== selfHosted) healthyStreak = 0;

const unhealthy =
runners.length === 0 ||
Expand All @@ -85,12 +91,12 @@ export function decide(prev, observed) {
misses += 1;
goods = 0;
healthyStreak = 0;
} else if (current === FALLBACK && allBusy) {
} else if (current === fallback && allBusy) {
// Neutral: the supervisor keeps idle slots, so all-busy is not recovery.
} else {
goods += 1;
misses = 0;
if (current === SELF_HOSTED) healthyStreak += 1;
if (current === selfHosted) healthyStreak += 1;
if (healthyStreak >= STABLE_TICKS) flaps = 0;
}

Expand All @@ -99,13 +105,13 @@ export function decide(prev, observed) {
let setVar = null;
let notify = null;
let rerunStuck = false;
if (unhealthy && misses >= 2 && current === SELF_HOSTED) {
setVar = FALLBACK;
if (unhealthy && misses >= 2 && current === selfHosted) {
setVar = fallback;
notify = "down";
rerunStuck = true;
flaps += 1;
} else if (!unhealthy && goods >= needGoods && current === FALLBACK) {
setVar = SELF_HOSTED;
} else if (!unhealthy && goods >= needGoods && current === fallback) {
setVar = selfHosted;
notify = unset ? "online" : "recovered";
}
if (setVar) {
Expand Down
89 changes: 89 additions & 0 deletions workers/nos-runner-watchdog/src/pools.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Runner pools the watchdog manages. NOS is the live pool; swmMacos is added dormant (no cron yet in wrangler.toml).

export const APPS = {
nos: {
idKey: "APP_ID",
installKey: "INSTALLATION_ID",
keyKey: "APP_KEY_PKCS8",
},
swm: {
idKey: "SWM_APP_ID",
installKey: "SWM_INSTALLATION_ID",
keyKey: "SWM_APP_KEY_PKCS8",
},
};

// NOS texts are copies of index.js MESSAGES; pool_nos_messages_unchanged pins them. Task 2 switches index.js to these.
const NOS_MESSAGES = {
down: () =>
"NOS runner down: NOS_RUNNER set to ubuntu-latest; stuck runs cancelled for re-run.",
recovered: () => "NOS runner recovered: NOS_RUNNER set to mimolette.",
online: () => "NOS runner online: NOS_RUNNER was unset, now mimolette.",
held: (v) =>
`NOS runner held: NOS_RUNNER=${v}; the watchdog leaves it alone.`,
skip: (cls) => `watchdog skipping: ${cls}`,
ok: "watchdog ok: ticks are running again.",
drill: "NOS runner drill: watchdog alert path. No action needed.",
};

const MACOS_MESSAGES = {
down: () =>
"macOS runner: down. SWM_MACOS_RUNNER set to macos-latest; stuck runs cancelled for re-run.",
recovered: () =>
"macOS runner: recovered. SWM_MACOS_RUNNER set to mimolette-macos.",
online: () =>
"macOS runner: online. SWM_MACOS_RUNNER was unset, now mimolette-macos.",
held: (v) =>
`macOS runner: held. SWM_MACOS_RUNNER=${v}; the watchdog leaves it alone.`,
skip: (cls) => `macOS runner: watchdog skipping: ${cls}`,
ok: "macOS runner: watchdog ok: ticks are running again.",
drill: "macOS runner: drill of the watchdog alert path. No action needed.",
};

export const POOLS = {
nos: {
id: "nos",
app: "nos",
org: "nightowlstudiollc",
variable: "NOS_RUNNER",
label: "mimolette",
fallback: "ubuntu-latest",
visibility: "private",
prefix: "",
cron: "*/10 * * * *",
rerunLost: false,
messages: NOS_MESSAGES,
},
swmMacos: {
id: "swm-macos",
app: "swm",
org: "smartwatermelon",
variable: "SWM_MACOS_RUNNER",
label: "mimolette-macos",
fallback: "macos-latest",
visibility: "all",
prefix: "swm-macos:",
cron: "5,15,25,35,45,55 * * * *",
rerunLost: true,
messages: MACOS_MESSAGES,
},
};

/** Pool for a cron string. A missing cron is NOS (manual invocation); an unknown cron is null. */
export function poolForCron(cron) {
if (cron === undefined || cron === null) return POOLS.nos;
return Object.values(POOLS).find((p) => p.cron === cron) ?? null;
}

/** The pool's App credentials as {APP_ID, INSTALLATION_ID, APP_KEY_PKCS8}, or null unless all three are non-empty. */
export function appEnv(env, pool) {
const a = APPS[pool.app];
const out = {
APP_ID: env[a.idKey],
INSTALLATION_ID: env[a.installKey],
APP_KEY_PKCS8: env[a.keyKey],
};
return Object.values(out).every((v) => typeof v === "string" && v !== "")
? out
: null;
}
109 changes: 109 additions & 0 deletions workers/nos-runner-watchdog/test/decide-pool.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import { decide } from "../src/decide.js";

const L = { selfHosted: "mimolette-macos", fallback: "macos-latest" };
const fresh = {
lastVar: null,
misses: 0,
goods: 0,
busyNames: [],
sameBusyTicks: 0,
};
const idle = [
{ name: "a", busy: false },
{ name: "b", busy: false },
];
const good = (varValue = "mimolette-macos") => ({
varValue,
runners: idle,
oldestQueuedMinutes: null,
});
const bad = (varValue = "mimolette-macos") => ({
varValue,
runners: [],
oldestQueuedMinutes: null,
});
const prevOf = (r) => ({ ...r.next });

test("pool_macos_healthy_stays", () => {
const r = decide({ ...fresh, lastVar: "mimolette-macos" }, good(), L);
assert.equal(r.setVar, null);
assert.equal(r.notify, null);
assert.equal(r.next.lastVar, "mimolette-macos");
});

test("pool_macos_two_misses_switch_to_macos_latest", () => {
const r1 = decide({ ...fresh, lastVar: "mimolette-macos" }, bad(), L);
const r2 = decide(prevOf(r1), bad(), L);
assert.equal(r2.setVar, "macos-latest");
assert.equal(r2.notify, "down");
assert.equal(r2.rerunStuck, true);
});

test("pool_macos_unset_goes_online", () => {
const r1 = decide(fresh, good(null), L);
assert.equal(r1.setVar, null);
const r2 = decide(prevOf(r1), good(null), L);
assert.equal(r2.setVar, "mimolette-macos");
assert.equal(r2.notify, "online");
});

test("pool_macos_macos26_is_hold", () => {
const r = decide(
{ ...fresh, lastVar: "mimolette-macos" },
good("macos-26"),
L,
);
assert.equal(r.setVar, null);
assert.equal(r.notify, "held");
assert.equal(r.next.lastVar, "macos-26");
const r2 = decide(prevOf(r), good("macos-26"), L);
assert.equal(r2.notify, null);
});

test("pool_macos_mimolette_value_is_hold", () => {
const r = decide({ ...fresh, lastVar: "macos-latest" }, good("mimolette"), L);
assert.equal(r.setVar, null);
assert.equal(r.notify, "held");
});

test("pool_macos_single_gap_no_switch", () => {
const r1 = decide({ ...fresh, lastVar: "mimolette-macos" }, bad(), L);
assert.equal(r1.setVar, null);
const r2 = decide(prevOf(r1), good(), L);
assert.equal(r2.setVar, null);
assert.equal(r2.next.misses, 0);
});

test("pool_macos_new_busy_name_is_progress", () => {
let prev = { ...fresh, lastVar: "mimolette-macos" };
for (let i = 0; i < 4; i += 1) {
const r = decide(
prev,
{
varValue: "mimolette-macos",
runners: [{ name: `vm-${i}`, busy: true }],
oldestQueuedMinutes: 25,
},
L,
);
assert.equal(r.setVar, null, `tick ${i}`);
prev = prevOf(r);
}
});

test("pool_macos_manual_flip_back_stays", () => {
let prev = { ...fresh, lastVar: "mimolette-macos" };
for (let i = 0; i < 2; i += 1) {
prev = prevOf(decide(prev, bad(), L));
}
assert.equal(prev.lastVar, "macos-latest");
// Operator flips the variable back to the label by hand; runners are healthy.
for (let i = 0; i < 3; i += 1) {
const r = decide(prev, good("mimolette-macos"), L);
assert.equal(r.setVar, null, `tick ${i}`);
assert.equal(r.next.lastVar, "mimolette-macos");
prev = prevOf(r);
}
});
109 changes: 109 additions & 0 deletions workers/nos-runner-watchdog/test/pools.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { POOLS, poolForCron, appEnv } from "../src/pools.js";

test("pool_missing_cron_is_nos", () => {
assert.equal(poolForCron(undefined), POOLS.nos);
});

test("pool_nos_cron_is_nos", () => {
assert.equal(poolForCron("*/10 * * * *"), POOLS.nos);
});

test("pool_macos_cron_is_macos", () => {
assert.equal(poolForCron("5,15,25,35,45,55 * * * *"), POOLS.swmMacos);
});

test("pool_unknown_cron_is_null", () => {
assert.equal(poolForCron("0 * * * *"), null);
});

test("pool_crons_unique", () => {
const crons = Object.values(POOLS).map((p) => p.cron);
assert.equal(new Set(crons).size, crons.length);
});

test("pool_nos_messages_unchanged", () => {
const m = POOLS.nos.messages;
assert.equal(
m.down(),
"NOS runner down: NOS_RUNNER set to ubuntu-latest; stuck runs cancelled for re-run.",
);
assert.equal(
m.recovered(),
"NOS runner recovered: NOS_RUNNER set to mimolette.",
);
assert.equal(
m.online(),
"NOS runner online: NOS_RUNNER was unset, now mimolette.",
);
assert.equal(
m.held("x"),
"NOS runner held: NOS_RUNNER=x; the watchdog leaves it alone.",
);
assert.equal(m.skip("network"), "watchdog skipping: network");
assert.equal(m.ok, "watchdog ok: ticks are running again.");
assert.equal(
m.drill,
"NOS runner drill: watchdog alert path. No action needed.",
);
});

test("pool_macos_messages_prefixed", () => {
const m = POOLS.swmMacos.messages;
const texts = [
m.down(),
m.recovered(),
m.online(),
m.held("macos-26"),
m.skip("network"),
m.ok,
m.drill,
];
for (const t of texts) assert.ok(t.startsWith("macOS runner:"), t);
assert.equal(
m.held("macos-26"),
"macOS runner: held. SWM_MACOS_RUNNER=macos-26; the watchdog leaves it alone.",
);
assert.equal(
m.down(),
"macOS runner: down. SWM_MACOS_RUNNER set to macos-latest; stuck runs cancelled for re-run.",
);
});

test("app_env_nos_reads_old_secret_names", () => {
const env = { APP_ID: "1", INSTALLATION_ID: "2", APP_KEY_PKCS8: "k" };
assert.deepEqual(appEnv(env, POOLS.nos), env);
});

test("app_env_swm_null_when_any_missing", () => {
const full = {
SWM_APP_ID: "1",
SWM_INSTALLATION_ID: "2",
SWM_APP_KEY_PKCS8: "k",
};
assert.deepEqual(appEnv(full, POOLS.swmMacos), {
APP_ID: "1",
INSTALLATION_ID: "2",
APP_KEY_PKCS8: "k",
});
for (const key of Object.keys(full)) {
const env = { ...full, [key]: "" };
assert.equal(appEnv(env, POOLS.swmMacos), null, key);
delete env[key];
assert.equal(appEnv(env, POOLS.swmMacos), null, key);
}
});

test("pool_wrangler_crons_match", () => {
const toml = readFileSync(
new URL("../wrangler.toml", import.meta.url),
"utf8",
);
const m = toml.match(/^crons\s*=\s*\[([^\]]*)\]/m);
assert.ok(m, "crons array present");
const crons = [...m[1].matchAll(/"([^"]*)"/g)].map((x) => x[1]);
assert.ok(crons.length > 0);
for (const c of crons) assert.notEqual(poolForCron(c), null, c);
});
Loading