Adds the remaining monitor types: a systeminformation-backed system widget (single-flight cached so multiple widgets don't force concurrent samples), an http widget with per-widget consecutive- failure tracking, and a traefik widget that reads router/entrypoint/ middleware status from Traefik's own API. Also adds opt-in label-based docker auto-discovery (discovery.docker in config.yml): containers carrying traefik.enable=true are turned into docker widgets in a synthetic "Discovered" group, re-scanned on a timer and whenever config.yml changes, with manual widgets always taking precedence over a discovered one for the same container. Fixes a real bug surfaced while testing discovery: Next's App Router bundles app/** through its own compiler pass, separate from server.ts (run directly via tsx), so configStore/effectiveConfigStore were silently instantiated twice - one instance watched and updated by server.ts, another frozen instance read by SSR. Live config edits were never reflected on page load without a full process restart. Both stores now key their singleton off globalThis, which both module graphs share within the same process. Verified against a real Traefik v3 container (API-driven router list, ping-based http check) and a labeled nginx container (auto-discovery), including that a config.yml edit now shows up in a fresh page load without restarting the server, in both dev and the production build.
78 lines
2.3 KiB
TypeScript
78 lines
2.3 KiB
TypeScript
import { configStore } from "./loader";
|
|
import { discoverDockerWidgets } from "@/lib/discovery/traefik-labels";
|
|
import type { Config, Widget } from "./schema";
|
|
|
|
const DISCOVERY_INTERVAL_MS = 60_000;
|
|
|
|
type Listener = (config: Config) => void;
|
|
|
|
function mergeDiscovered(config: Config, discovered: Widget[]): Config {
|
|
if (discovered.length === 0) return config;
|
|
return { ...config, groups: [...config.groups, { name: "Discovered", widgets: discovered }] };
|
|
}
|
|
|
|
function discoveredKey(widget: Widget): string {
|
|
return widget.type === "docker" || widget.type === "database" ? widget.containerName : "";
|
|
}
|
|
|
|
function sameDiscoveredSet(a: Widget[], b: Widget[]): boolean {
|
|
if (a.length !== b.length) return false;
|
|
const setA = new Set(a.map(discoveredKey));
|
|
return b.every((widget) => setA.has(discoveredKey(widget)));
|
|
}
|
|
|
|
class EffectiveConfigStore {
|
|
private discovered: Widget[] = [];
|
|
private timer: ReturnType<typeof setInterval> | null = null;
|
|
private readonly listeners = new Set<Listener>();
|
|
|
|
get(): Config {
|
|
return mergeDiscovered(configStore.get(), this.discovered);
|
|
}
|
|
|
|
onUpdate(listener: Listener): () => void {
|
|
this.listeners.add(listener);
|
|
return () => this.listeners.delete(listener);
|
|
}
|
|
|
|
async start(): Promise<void> {
|
|
configStore.onUpdate(() => {
|
|
this.emit();
|
|
void this.scan();
|
|
});
|
|
await this.scan();
|
|
this.timer = setInterval(() => void this.scan(), DISCOVERY_INTERVAL_MS);
|
|
}
|
|
|
|
stop(): void {
|
|
if (this.timer) clearInterval(this.timer);
|
|
this.timer = null;
|
|
}
|
|
|
|
private async scan(): Promise<void> {
|
|
try {
|
|
const next = await discoverDockerWidgets(configStore.get());
|
|
if (!sameDiscoveredSet(this.discovered, next)) {
|
|
this.discovered = next;
|
|
this.emit();
|
|
}
|
|
} catch (err) {
|
|
console.error(`[discovery] ${(err as Error).message}`);
|
|
}
|
|
}
|
|
|
|
private emit(): void {
|
|
const effective = this.get();
|
|
for (const listener of this.listeners) listener(effective);
|
|
}
|
|
}
|
|
|
|
// See lib/config/loader.ts for why this is keyed off globalThis rather than
|
|
// exported as a plain module singleton.
|
|
declare global {
|
|
var __pulsenodeEffectiveConfigStore: EffectiveConfigStore | undefined;
|
|
}
|
|
|
|
export const effectiveConfigStore = globalThis.__pulsenodeEffectiveConfigStore ?? new EffectiveConfigStore();
|
|
globalThis.__pulsenodeEffectiveConfigStore = effectiveConfigStore;
|