const ws = new WebSocket(Deno.args[0]) async function do_stuff() { switch (Deno.args[1]) { case "enqueue": if (Deno.args.length >= 4) ws.send(JSON.stringify({ t: "metadata", key: Deno.args[2], data: JSON.parse(Deno.args[3]) })) ws.send(JSON.stringify({ t: "enqueue", key: Deno.args[2], ignore_complete: true })) break; // deno-lint-ignore no-case-declarations case "enqueue_batch": const a = await new Response(Deno.stdin.readable).text() for (let key of a.split("\n")) { key = key.trim() if (!key) continue ws.send(JSON.stringify({ t: "enqueue", key, ignore_complete: true })) } break; case "retry_all": ws.send(JSON.stringify({ t: "query", state: "complete", data: { failed: null }, cookie: "for_enqueue" })) return; case "save": break; default: break } ws.send(JSON.stringify({ t: "save" })) exit() } function exit() { setTimeout(() => Deno.exit(0), 200) // not sure if websockets are flushed since they're non-blocking } ws.onerror = () => console.error("ws error") ws.onclose = () => console.error("ws closed") ws.onopen = () => { console.log("ws open"); ws.send(JSON.stringify({ t: "register", name: "cli", task_kinds: [] })) do_stuff() } ws.onmessage = ev => { if (typeof ev.data != "string") return const p = JSON.parse(ev.data) if (p.t == "config") return if (p.t == "error") console.error(`error: ${p.message}`); if (p.t == "query_response" && p.cookie == "for_enqueue") { for (const key of p.keys) { ws.send(JSON.stringify({ t: "metadata", key, data: { failed: null } })) ws.send(JSON.stringify({ t: "enqueue", key, ignore_complete: true })) } ws.send(JSON.stringify({ t: "save" })) exit() } }