// A module whose code runs long (novox/hq ADR 0198): it subscribes as it is imported, handles each // event once it can, fails the first time it is asked to, dies the first time it is told to, and one // tool asks another module's tool through the runtime. What it did is written to WATCH_LOG. import { appendFileSync, existsSync, writeFileSync } from "node:fs"; import { on } from "@novox/mesh-sdk/events"; import { broker } from "@novox/mesh-sdk/messaging"; import { registerModuleTools } from "@novox/mesh-sdk/tools"; const log = process.env.WATCH_LOG; const once = (mark) => { const file = `${log}.${mark}`; if (existsSync(file)) return false; writeFileSync(file, "1"); return true; }; appendFileSync(log, "started\n"); await on("alpha.happened", async (e) => { if (e.body.fail && once(`fail-${e.body.n}`)) throw new Error(`not yet ${e.body.n}`); if (e.body.die && once(`die-${e.body.n}`)) process.exit(7); appendFileSync(log, `handled ${e.body.n}\n`); }); registerModuleTools("watcher", () => [ { name: "relay", description: "asks beta", input: {}, run: async () => broker().request("beta.three", {}) }, ]);