Algorick commited on
Commit
bb1cc99
·
verified ·
1 Parent(s): c50a3d9

bf0ac120a01a

Browse files
Files changed (1) hide show
  1. web/backend.js +54 -27
web/backend.js CHANGED
@@ -313,6 +313,12 @@
313
  clientPromise = null;
314
  }
315
 
 
 
 
 
 
 
316
  const DAY = 24 * 3600 * 1000;
317
  const FIRST_USE = "pixrick.gpu.inizio";
318
 
@@ -419,36 +425,57 @@
419
  const state = { job: placeholderJob(id, payload), payload };
420
  jobs.set(id, state);
421
 
422
- const run = client.submit("/genera", [JSON.stringify(request), files.map((f) => handle(f)), mask ? handle(mask) : null]);
423
- (async () => {
424
- try {
425
- for await (const msg of run) {
426
- if (msg.type === "status") {
427
- if (msg.stage === "error") throw engineError(msg.message);
428
- if (msg.stage === "pending" && msg.position != null && state.job.status !== "generazione") {
429
- state.job = { ...state.job, status: "in_coda", position: msg.position, eta: msg.eta ?? null, queue_size: msg.queue_size ?? null };
430
- }
 
 
 
 
431
  }
432
- if (msg.type === "data") {
433
- const [pub, out] = msg.data;
434
- if (!pub) continue;
435
- const live = { ...pub, elapsed: Math.round((Date.now() - started) / 100) / 10 };
436
- if (pub.status === "fatto" && out && out.length) {
437
- state.job = { ...live, status: "salvataggio" };
438
- const results = [];
439
- for (let i = 0; i < out.length; i++) {
440
- const res = await fetch(out[i].url);
441
- const png = await res.blob();
442
- const meta = { ...pub.results[i], riferimenti: payload.riferimenti || [], maschera: payload.maschera || null };
443
- results.push(await storeItem("bozze", meta, png, await thumbnail(png)));
444
- }
445
- markGpuUse();
446
- state.job = { ...live, status: "fatto", results, progress: 1 };
447
- emit({ type: "generata", count: results.length });
448
- return;
449
  }
450
- state.job = { ...live, status: pub.status === "fatto" ? "salvataggio" : pub.status };
 
 
 
451
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
452
  }
453
  } catch (e) {
454
  const err = e instanceof Error && e.code ? e : engineError(e);
 
313
  clientPromise = null;
314
  }
315
 
316
+ let hiddenSince = 0;
317
+ document.addEventListener("visibilitychange", () => {
318
+ if (document.hidden) hiddenSince = Date.now();
319
+ else if (hiddenSince && Date.now() - hiddenSince > 20000) clientPromise = null;
320
+ });
321
+
322
  const DAY = 24 * 3600 * 1000;
323
  const FIRST_USE = "pixrick.gpu.inizio";
324
 
 
425
  const state = { job: placeholderJob(id, payload), payload };
426
  jobs.set(id, state);
427
 
428
+ const inputs = [JSON.stringify(request), files.map((f) => handle(f)), mask ? handle(mask) : null];
429
+
430
+ async function follow(run) {
431
+ let received = false;
432
+ for await (const msg of run) {
433
+ if (msg.type === "status") {
434
+ if (msg.stage === "error") {
435
+ const err = engineError(msg.message);
436
+ err.lost = !received && !msg.message;
437
+ throw err;
438
+ }
439
+ if (msg.stage === "pending" && msg.position != null && state.job.status !== "generazione") {
440
+ state.job = { ...state.job, status: "in_coda", position: msg.position, eta: msg.eta ?? null, queue_size: msg.queue_size ?? null };
441
  }
442
+ }
443
+ if (msg.type === "data") {
444
+ received = true;
445
+ const [pub, out] = msg.data;
446
+ if (!pub) continue;
447
+ const live = { ...pub, elapsed: Math.round((Date.now() - started) / 100) / 10 };
448
+ if (pub.status === "fatto" && out && out.length) {
449
+ state.job = { ...live, status: "salvataggio" };
450
+ const results = [];
451
+ for (let i = 0; i < out.length; i++) {
452
+ const res = await fetch(out[i].url);
453
+ const png = await res.blob();
454
+ const meta = { ...pub.results[i], riferimenti: payload.riferimenti || [], maschera: payload.maschera || null };
455
+ results.push(await storeItem("bozze", meta, png, await thumbnail(png)));
 
 
 
456
  }
457
+ markGpuUse();
458
+ state.job = { ...live, status: "fatto", results, progress: 1 };
459
+ emit({ type: "generata", count: results.length });
460
+ return;
461
  }
462
+ state.job = { ...live, status: pub.status === "fatto" ? "salvataggio" : pub.status };
463
+ }
464
+ }
465
+ const err = new Error("Il motore non risponde. Riprova tra poco.");
466
+ err.lost = !received;
467
+ throw err;
468
+ }
469
+
470
+ (async () => {
471
+ try {
472
+ try {
473
+ await follow(client.submit("/genera", inputs));
474
+ } catch (e) {
475
+ if (e?.code || e?.lost === false) throw e;
476
+ clientPromise = null;
477
+ const fresh = await engine();
478
+ await follow(fresh.submit("/genera", inputs));
479
  }
480
  } catch (e) {
481
  const err = e instanceof Error && e.code ? e : engineError(e);