blob: 1bbe0c749f8ff91b9f778abd9f8c84780281aba0 (
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
|
const ws = new WebSocket(Deno.args[0])
async function do_work(key: string) {
let progress = 0
while (progress < 1) {
await new Promise(r => setTimeout(r, Math.random() * 400))
progress += 0.1
ws.send(JSON.stringify({ t: "metadata", key, data: { progress } }))
}
ws.send(JSON.stringify({ t: "complete", key }))
}
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: "dummy worker", task_kinds: ["youtube"] }))
ws.send(JSON.stringify({ t: "accept" }))
ws.send(JSON.stringify({ t: "accept" }))
}
ws.onmessage = async ev => {
if (typeof ev.data != "string") return
const p = JSON.parse(ev.data)
if (p.t == "error") console.error(`error: ${p.message}`);
if (p.t == "work") {
if (!p.data.output) throw new Error("no output");
await do_work(p.key)
ws.send(JSON.stringify({ t: "accept" }))
}
}
|