a38986acdb
Implements the Sense observation engine runtime per RFC-001:
- IPC types: discriminated union for parent↔worker messages
- sense-runtime: openSenseDb (WAL), openPeerDb (readonly), runMigrations,
loadComputeFn, executeCompute with Result<T> error handling
- sense-worker: CLI bootstrap, reads nerve.yaml, inits per-sense DB,
builds peer map, enters IPC event loop
- examples/cpu-usage: sample sense with Drizzle schema + migration
- 15 unit tests covering migrations, DB ops, compute, peer isolation
小橘 🍊(NEKO Team)
24 lines
705 B
TypeScript
24 lines
705 B
TypeScript
import { loadavg } from "node:os";
|
|
|
|
import type { DrizzleDB, PeerMap } from "@uncaged/nerve-daemon";
|
|
|
|
import { samples } from "./schema.js";
|
|
|
|
/**
|
|
* Read the 1-minute CPU load average, persist it, and emit a Signal.
|
|
*
|
|
* Returns `null` only if `loadavg` is unavailable (non-POSIX platforms).
|
|
* On every successful read a row is inserted and the value is returned,
|
|
* which causes the engine to emit a Signal.
|
|
*/
|
|
export async function compute(db: DrizzleDB, _peers: PeerMap): Promise<number | null> {
|
|
const [oneMin] = loadavg();
|
|
|
|
if (typeof oneMin !== "number" || Number.isNaN(oneMin)) {
|
|
return null;
|
|
}
|
|
|
|
await db.insert(samples).values({ ts: Date.now(), value: oneMin });
|
|
return oneMin;
|
|
}
|