@muhkoo/theater-transcoder 0.3.2 → 0.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +4 -3
  2. package/src/worker.js +35 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@muhkoo/theater-transcoder",
3
- "version": "0.3.2",
3
+ "version": "0.3.4",
4
4
  "description": "Drain a Muhkoo Theater transcode queue with NATIVE ffmpeg. Run it on any machine signed in as you to add transcoding power to your library.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -25,10 +25,11 @@
25
25
  "access": "public"
26
26
  },
27
27
  "dependencies": {
28
- "@muhkoo/connect": "0.10.2-alpha.0",
28
+ "@muhkoo/connect": "0.10.4-alpha.0",
29
29
  "ffmpeg-static": "^5.2.0",
30
30
  "node-datachannel": "^0.12.0",
31
31
  "snarkjs": "^0.7.5",
32
32
  "ws": "^8.18.0"
33
- }
33
+ },
34
+ "packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
34
35
  }
package/src/worker.js CHANGED
@@ -38,6 +38,33 @@ export function startWorker({ client, space, appKey, baseUrl }) {
38
38
 
39
39
  const patch = (id, values) => jobs().update(id, { ...values, updated_at: Date.now() });
40
40
 
41
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
42
+
43
+ // Fetch the raw by manifest, retrying while the uploader's background upload
44
+ // finishes landing on origin (or a P2P peer appears). A missing-shard error is
45
+ // transient here — the bytes are still arriving — so we back off and retry
46
+ // instead of failing the job. Only a genuine, persistent gap eventually throws.
47
+ async function fetchRawWithRetry(space, manifest, job) {
48
+ const deadline = Date.now() + 8 * 60_000; // headroom for a slow big-file upload
49
+ let attempt = 0;
50
+ for (;;) {
51
+ try {
52
+ const { data } = await space.getFile(manifest);
53
+ return data;
54
+ } catch (e) {
55
+ const msg = String(e?.message || e);
56
+ const transient = /shard|unrecoverable|missing|404|network|fetch|econn|timeout/i.test(msg);
57
+ if (!transient || Date.now() > deadline) throw e;
58
+ attempt++;
59
+ const wait = Math.min(8000, 1500 * attempt);
60
+ log(`raw not fully available yet (${msg.slice(0, 70)}) — retry ${attempt} in ${wait}ms`);
61
+ await patch(job._id, { phase: "Fetching raw… (uploading)", heartbeat_at: Date.now() }).catch(() => {});
62
+ await postSignal({ kind: "progress", jobId: job._id, progress: 0, phase: "Fetching raw… (uploading)", worker: WORKER_LABEL });
63
+ await sleep(wait);
64
+ }
65
+ }
66
+ }
67
+
41
68
  async function claimNext() {
42
69
  const { rows } = await jobs().query({
43
70
  where: [{ column: "owner", op: "eq", value: commitment }],
@@ -86,7 +113,14 @@ export function startWorker({ client, space, appKey, baseUrl }) {
86
113
  // Read through the Space's getFile (NOT client.storage.readByManifest):
87
114
  // only the Space file surface has the P2P peer source wired, so this pulls
88
115
  // shards from the uploader's browser over WebRTC first, origin as fallback.
89
- const { data } = await space.getFile(manifest);
116
+ //
117
+ // Because the uploader returns the manifest as soon as shards are cached +
118
+ // announced (background upload), origin may not hold every shard yet when we
119
+ // first ask — and if P2P can't reach the uploader, a shard 404s and getFile
120
+ // fails "N shards missing". That's transient: the upload is still landing, so
121
+ // retry (with backoff) until we can reassemble or we time out. The heartbeat
122
+ // ticker keeps our claim alive throughout.
123
+ const data = await fetchRawWithRetry(space, manifest, job);
90
124
  log(`transcoding ${(data.length / 1e6).toFixed(1)}MB…`);
91
125
  const { hls_index, duration, size } = await transcodeToHls({
92
126
  rawBytes: data, filename: job.filename || "input.mp4", space, onProgress, signal: abort.signal,