Функция → мониторинги (core/feature-status.ts), серая кнопка входа через eGov и статус MCP, отказ startEgovFlow, журнал и сообщения в канал. После переезда живых обновлений на WebSocket (PEP-225): область `features` в LIVE_PLAIN_SCOPES, открыта без входа (SSE и WebSocket с `anonymous: true` — только открытые области), смотрится по таймеру вместе с `observe`, смена сбрасывает кэш статусов. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
54 lines
2.4 KiB
TypeScript
54 lines
2.4 KiB
TypeScript
// PEP-202: the `peppa-observer` service — runs the monitors of Observability. Every second it
|
|
// starts the checks that are due (they run in the background, each with its own timeout), every
|
|
// 30 s it writes its heartbeat (the watchdog watches it like the bot and the web), every hour it
|
|
// deletes checks older than the retention (7 days). Monitors are rows in observe.db: the web and
|
|
// MCP change them, the service picks the change up on its next pass — no restart.
|
|
import { beat, featureTick, observeTick, pruneObserve, seedFeatureMonitors, seedMonitors, type ObserveProbes } from "@peppa/core";
|
|
import { loadEnv } from "@peppa/shared";
|
|
import type { Db } from "@peppa/db";
|
|
|
|
export interface ObserverOptions {
|
|
log?: (m: string) => void;
|
|
tickMs?: number;
|
|
probes?: ObserveProbes;
|
|
}
|
|
|
|
export async function runObserver(db: Db, o: ObserverOptions = {}, signal?: AbortSignal): Promise<void> {
|
|
const log = o.log ?? (() => {});
|
|
const made = seedMonitors();
|
|
if (made.length) log(`observer: built-in monitors created: ${made.map((m) => m.name).join(", ")}`);
|
|
// PEP-224: MCP monitors switch the «mcp» feature off; created once, when the web has a public address.
|
|
const mcp = seedFeatureMonitors(loadEnv().PEPPA_WEB_URL);
|
|
if (mcp.length) log(`observer: built-in monitors created: ${mcp.map((m) => m.name).join(", ")}`);
|
|
let featuresBusy = false;
|
|
log("observer started");
|
|
let lastBeat = 0;
|
|
let lastPrune = 0;
|
|
while (!signal?.aborted) {
|
|
const now = Date.now();
|
|
if (now - lastBeat >= 30_000) {
|
|
lastBeat = now;
|
|
await beat(db, "observer").catch((e: Error) => log(`observer: heartbeat failed: ${e.message}`));
|
|
}
|
|
if (now - lastPrune >= 3_600_000) {
|
|
lastPrune = now;
|
|
try {
|
|
const n = pruneObserve();
|
|
if (n) log(`observer: ${n} old checks deleted`);
|
|
} catch (e) {
|
|
log(`observer: prune failed: ${(e as Error).message}`);
|
|
}
|
|
}
|
|
// Not awaited: a slow check must not hold the others or the heartbeat.
|
|
void observeTick(db, { log, probes: o.probes }).catch((e: Error) => log(`observer: tick failed: ${e.message}`));
|
|
// PEP-224: features follow their monitors — announce a change (journal, channel, live pages).
|
|
if (!featuresBusy) {
|
|
featuresBusy = true;
|
|
void featureTick(db, { log })
|
|
.catch((e: Error) => log(`observer: features failed: ${e.message}`))
|
|
.finally(() => (featuresBusy = false));
|
|
}
|
|
await new Promise((r) => setTimeout(r, o.tickMs ?? 1_000));
|
|
}
|
|
}
|