web/workers/fetch: retry only when needed & reduce attempts to 3

This commit is contained in:
wukko 2025-05-14 21:16:38 +06:00
parent a2d12ce82f
commit 9cf40549e3
No known key found for this signature in database
GPG Key ID: 3E30B3F26C7B4AA2
2 changed files with 7 additions and 8 deletions

View File

@ -1,8 +1,8 @@
{ {
"no_final_file": "no final file output", "no_final_file": "no final file output",
"worker_didnt_start": "couldn't start a processing worker", "worker_didnt_start": "couldn't start a processing worker",
"generic_error": "processing pipeline crashed, see console for details",
"fetch.crashed": "fetch worker crashed, see console for details",
"fetch.bad_response": "couldn't access the file tunnel", "fetch.bad_response": "couldn't access the file tunnel",
"fetch.no_file_reader": "couldn't write a file to cache", "fetch.no_file_reader": "couldn't write a file to cache",
"fetch.empty_tunnel": "file tunnel is empty, try again", "fetch.empty_tunnel": "file tunnel is empty, try again",

View File

@ -3,20 +3,19 @@ import * as Storage from "$lib/storage";
let attempts = 0; let attempts = 0;
const fetchFile = async (url: string) => { const fetchFile = async (url: string) => {
const error = async (code: string) => { const error = async (code: string, retry: boolean = true) => {
attempts++; attempts++;
if (attempts <= 5) { // try 3 more times before actually failing
// try 5 more times before actually failing if (retry && attempts <= 3) {
await fetchFile(url); await fetchFile(url);
} else { } else {
// if it still fails, then throw an error and quit
self.postMessage({ self.postMessage({
cobaltFetchWorker: { cobaltFetchWorker: {
error: code, error: code,
} }
}); });
self.close(); return self.close();
} }
}; };
@ -75,7 +74,7 @@ const fetchFile = async (url: string) => {
const file = Storage.retype(await storage.res(), contentType); const file = Storage.retype(await storage.res(), contentType);
if (contentLength && Number(contentLength) !== file.size) { if (contentLength && Number(contentLength) !== file.size) {
return error("queue.fetch.corrupted_file"); return error("queue.fetch.corrupted_file", false);
} }
self.postMessage({ self.postMessage({
@ -86,7 +85,7 @@ const fetchFile = async (url: string) => {
} catch (e) { } catch (e) {
console.error("error from the fetch worker:"); console.error("error from the fetch worker:");
console.error(e); console.error(e);
return error("queue.generic_error"); return error("queue.fetch.crashed", false);
} }
} }