aboutsummaryrefslogtreecommitdiff
path: root/scripts/cli.ts
blob: bf3e44a01601b4b15d7848fc8b7cd7fc8406bea9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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()
    }
}