stabilize, add daemon state, add base build script
This commit is contained in:
parent
a1963cf491
commit
241d50e42a
12 changed files with 512 additions and 46 deletions
|
@ -1,5 +1,6 @@
|
|||
import { TextLineStream } from "https://deno.land/std@0.208.0/streams/mod.ts";
|
||||
|
||||
export type Channel<T> = ReturnType<typeof channel<T>>;
|
||||
export function channel<T>() {
|
||||
const inner = {
|
||||
queue: [] as T[],
|
||||
|
@ -41,3 +42,54 @@ export function lines(readable: ReadableStream<Uint8Array>) {
|
|||
.pipeThrough(new TextDecoderStream())
|
||||
.pipeThrough(new TextLineStream());
|
||||
}
|
||||
|
||||
const async_noop = async () => {};
|
||||
export type LoopProcess = ReturnType<typeof loop_process>;
|
||||
export function loop_process(
|
||||
command: Deno.Command,
|
||||
opts?: { delay?: number; on_start?: () => unknown; on_stop?: () => unknown },
|
||||
) {
|
||||
const events = {
|
||||
on_start: opts?.on_start ?? async_noop,
|
||||
on_stop: opts?.on_stop ?? async_noop,
|
||||
};
|
||||
const kill_sig = channel<"kill">();
|
||||
async function launch() {
|
||||
while (true) {
|
||||
await events.on_start();
|
||||
const child_process = command.spawn();
|
||||
const result = await Promise.any([kill_sig.receive(), child_process.output()]);
|
||||
if (result === "kill") {
|
||||
await events.on_stop();
|
||||
child_process.kill();
|
||||
break;
|
||||
}
|
||||
await events.on_stop();
|
||||
await sleep(opts?.delay ?? 500);
|
||||
}
|
||||
}
|
||||
const looping = launch();
|
||||
return {
|
||||
kill: async () => {
|
||||
kill_sig.send("kill"), await looping;
|
||||
},
|
||||
events,
|
||||
looping,
|
||||
};
|
||||
}
|
||||
|
||||
export function sleep(ms: number) {
|
||||
return new Promise((resolver) => setTimeout(resolver, ms));
|
||||
}
|
||||
|
||||
export async function run(command: string, ...args: string[]) {
|
||||
const { code } = await new Deno.Command(command, { args }).output();
|
||||
if (code != 0) throw new Error(`command ${command} failed`);
|
||||
}
|
||||
|
||||
export function log_from(...prefixes: string[]) {
|
||||
const prefix = prefixes.map((p) => `[${p}]`).join("").padStart(10);
|
||||
return function (...args: unknown[]) {
|
||||
console.log(prefix, ...args);
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue