add graceful violent interupt

This commit is contained in:
JOLIMAITRE Matthieu 2023-12-09 00:19:09 +01:00
parent 54c4e91af8
commit a1963cf491
2 changed files with 29 additions and 16 deletions

View file

@ -24,21 +24,26 @@ export function new_cmd_stop() {
export type Cmd = CmdStatus | CmdEnable | CmdDisable | CmdStop;
export async function* daemon_listen(sock_path: string) {
export function daemon_listen(sock_path: string) {
const server = Deno.listen({ transport: "unix", path: sock_path });
for await (const request of server) {
const respond = async (message: string) => {
const generator = async function* () {
for await (const request of server) {
const respond = async (message: string) => {
try {
await request.write(new TextEncoder().encode(message));
} catch (_) { /* bof mais bon */ }
};
try {
await request.write(new TextEncoder().encode(message));
} catch (_) { /* bof mais bon */ }
};
try {
for await (const line of lines(request.readable)) {
const cmd = JSON.parse(line) as Cmd;
yield { cmd, respond };
}
} catch (_) { /* ok tier */ }
}
for await (const line of lines(request.readable)) {
const cmd = JSON.parse(line) as Cmd;
yield { cmd, respond };
}
} catch (_) { /* ok tier */ }
}
};
const result = generator() as ReturnType<typeof generator> & { server: typeof server };
result.server = server;
return result;
}
export async function daemon_send(sock_path: string, command: Cmd) {