initial commit
This commit is contained in:
34
src/monitor.ts
Normal file
34
src/monitor.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { existsSync } from "node:fs";
|
||||
import { readFile } from "node:fs/promises";
|
||||
|
||||
async function sleep(ms: number) {
|
||||
await new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
export async function monitorRun(input: {
|
||||
eventsPath: string;
|
||||
resultPath: string;
|
||||
onEvent?: (event: any) => void;
|
||||
pollMs?: number;
|
||||
}) {
|
||||
const pollMs = input.pollMs ?? 50;
|
||||
let offset = 0;
|
||||
|
||||
while (true) {
|
||||
if (existsSync(input.eventsPath)) {
|
||||
const text = await readFile(input.eventsPath, "utf8");
|
||||
const next = text.slice(offset);
|
||||
offset = text.length;
|
||||
|
||||
for (const line of next.split("\n").filter(Boolean)) {
|
||||
input.onEvent?.(JSON.parse(line));
|
||||
}
|
||||
}
|
||||
|
||||
if (existsSync(input.resultPath)) {
|
||||
return JSON.parse(await readFile(input.resultPath, "utf8"));
|
||||
}
|
||||
|
||||
await sleep(pollMs);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user