A launched bundle's mesh/subscribe binds the module's own durable consumer — EVENTS, <node>_<module>, by name as the module's own runtime bound it, so nothing is lost or replayed in the move — and every event goes to each child of the module that subscribed as mesh/event, acknowledged only when all answered, negatively acknowledged after a short delay when one failed or died, terminated when it is not an event. mesh/ask calls a tool as the module. Every launched bundle is started again when it exits, with backoff, since long-running code waits for no call. Requires SDK 0.1.6.
25 lines
1.1 KiB
JavaScript
25 lines
1.1 KiB
JavaScript
// 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", {}) },
|
|
]);
|