|
| 1 | +/* global Zinnia */ |
| 2 | + |
| 3 | +const sleep = dt => new Promise(resolve => setTimeout(resolve, dt)) |
| 4 | + |
| 5 | +// Create activity events when we bacome operational or fail, but only once |
| 6 | +export class ActivityState { |
| 7 | + #ok = true |
| 8 | + |
| 9 | + onError () { |
| 10 | + if (this.#ok) { |
| 11 | + this.#ok = false |
| 12 | + Zinnia.activity.error('SPARK failed reporting retrieval') |
| 13 | + } |
| 14 | + } |
| 15 | + |
| 16 | + onSuccess () { |
| 17 | + if (!this.#ok) { |
| 18 | + this.#ok = true |
| 19 | + Zinnia.activity.success('SPARK retrieval reporting resumed') |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +export default class Spark { |
| 25 | + #fetch |
| 26 | + #activity = new ActivityState() |
| 27 | + |
| 28 | + constructor ({ fetch = globalThis.fetch } = {}) { |
| 29 | + this.#fetch = fetch |
| 30 | + } |
| 31 | + |
| 32 | + async getRetrieval () { |
| 33 | + console.log('Geting retrieval...') |
| 34 | + const res = await this.#fetch('https://spark.fly.dev/retrievals', { |
| 35 | + method: 'POST' |
| 36 | + }) |
| 37 | + const retrieval = await res.json() |
| 38 | + console.log({ retrieval }) |
| 39 | + return retrieval |
| 40 | + } |
| 41 | + |
| 42 | + async fetchCAR (url, stats) { |
| 43 | + console.log('Fetching CAR...') |
| 44 | + |
| 45 | + // Abort if no progress was made for 10 seconds |
| 46 | + const controller = new AbortController() |
| 47 | + const { signal } = controller |
| 48 | + let timeout |
| 49 | + const resetTimeout = () => { |
| 50 | + if (timeout) { |
| 51 | + clearTimeout(timeout) |
| 52 | + } |
| 53 | + timeout = setTimeout(() => controller.abort(), 10_000) |
| 54 | + } |
| 55 | + |
| 56 | + try { |
| 57 | + resetTimeout() |
| 58 | + const res = await this.#fetch(url, { signal }) |
| 59 | + stats.statusCode = res.status |
| 60 | + |
| 61 | + if (res.ok) { |
| 62 | + resetTimeout() |
| 63 | + for await (const value of res.body) { |
| 64 | + if (stats.firstByteAt === null) { |
| 65 | + stats.firstByteAt = new Date() |
| 66 | + } |
| 67 | + stats.byteLength += value.byteLength |
| 68 | + resetTimeout() |
| 69 | + } |
| 70 | + } |
| 71 | + } finally { |
| 72 | + clearTimeout(timeout) |
| 73 | + } |
| 74 | + |
| 75 | + stats.endAt = new Date() |
| 76 | + console.log(stats) |
| 77 | + } |
| 78 | + |
| 79 | + async submitRetrieval (id, stats) { |
| 80 | + console.log('Submitting retrieval...') |
| 81 | + const res = await this.#fetch(`https://spark.fly.dev/retrievals/${id}`, { |
| 82 | + method: 'PATCH', |
| 83 | + body: JSON.stringify({ |
| 84 | + ...stats, |
| 85 | + walletAddress: Zinnia.walletAddress |
| 86 | + }), |
| 87 | + headers: { |
| 88 | + 'Content-Type': 'application/json' |
| 89 | + } |
| 90 | + }) |
| 91 | + if (res.status !== 200) { |
| 92 | + let body |
| 93 | + try { |
| 94 | + body = await res.text() |
| 95 | + } catch {} |
| 96 | + throw new Error(`Failed to submit retrieval (${res.status}): ${body}`) |
| 97 | + } |
| 98 | + console.log('Retrieval submitted') |
| 99 | + } |
| 100 | + |
| 101 | + async nextRetrieval () { |
| 102 | + const retrieval = await this.getRetrieval() |
| 103 | + |
| 104 | + let success = false |
| 105 | + const stats = { |
| 106 | + startAt: new Date(), |
| 107 | + firstByteAt: null, |
| 108 | + endAt: null, |
| 109 | + byteLength: 0, |
| 110 | + statusCode: null |
| 111 | + } |
| 112 | + const url = `https://strn.pl/ipfs/${retrieval.cid}` |
| 113 | + try { |
| 114 | + await this.fetchCAR(url, stats) |
| 115 | + success = true |
| 116 | + this.#activity.onSuccess() |
| 117 | + } catch (err) { |
| 118 | + console.error(`Failed to fetch ${url}`) |
| 119 | + console.error(err) |
| 120 | + this.#activity.onError() |
| 121 | + } |
| 122 | + |
| 123 | + await this.submitRetrieval(retrieval.id, { success, ...stats }) |
| 124 | + Zinnia.jobCompleted() |
| 125 | + return retrieval.id |
| 126 | + } |
| 127 | + |
| 128 | + async run () { |
| 129 | + while (true) { |
| 130 | + try { |
| 131 | + await this.nextRetrieval() |
| 132 | + } catch (err) { |
| 133 | + this.#activity.onError() |
| 134 | + console.error(err) |
| 135 | + } |
| 136 | + await sleep(1_000) |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments