Spaces:
Running
Running
add retry on network error
Browse files- wasm-demo.js +55 -22
wasm-demo.js
CHANGED
|
@@ -299,7 +299,7 @@ $('btnEncrypt').onclick = async () => {
|
|
| 299 |
}
|
| 300 |
};
|
| 301 |
|
| 302 |
-
async function pollTaskStatus(currentTaskId, currentUid) {
|
| 303 |
try {
|
| 304 |
const statusResponse = await fetch(`${SERVER}/get_task_status?task_id=${currentTaskId}&uid=${currentUid}`);
|
| 305 |
if (!statusResponse.ok) {
|
|
@@ -375,24 +375,64 @@ async function pollTaskStatus(currentTaskId, currentUid) {
|
|
| 375 |
}
|
| 376 |
return null;
|
| 377 |
} else {
|
| 378 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 379 |
if (finalStatus && (finalStatus.status === 'success' || finalStatus.status === 'completed')) {
|
| 380 |
getTaskResult(currentTaskId, currentUid, 'synthid');
|
| 381 |
}
|
| 382 |
-
}),
|
| 383 |
return null;
|
| 384 |
}
|
| 385 |
} catch (e) {
|
| 386 |
console.error('[Poll] Polling exception:', e);
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
}
|
| 395 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 396 |
}
|
| 397 |
}
|
| 398 |
|
|
@@ -412,7 +452,7 @@ async function getTaskResult(currentTaskId, currentUid, taskName) {
|
|
| 412 |
|
| 413 |
if (!resultResponse.ok) {
|
| 414 |
const errorText = await resultResponse.text();
|
| 415 |
-
throw new Error(`Failed to get results`);
|
| 416 |
}
|
| 417 |
|
| 418 |
const resultArrayBuffer = await resultResponse.arrayBuffer();
|
|
@@ -482,7 +522,7 @@ $('btnSend').onclick = async () => {
|
|
| 482 |
|
| 483 |
if (!startTaskResponse.ok) {
|
| 484 |
const errorText = await startTaskResponse.text();
|
| 485 |
-
throw new Error(`Server error: ${startTaskResponse.status}`);
|
| 486 |
}
|
| 487 |
|
| 488 |
const { task_id: newTaskId } = await startTaskResponse.json();
|
|
@@ -490,7 +530,7 @@ $('btnSend').onclick = async () => {
|
|
| 490 |
console.log('[Main] Task submitted to server. Task ID:', taskId);
|
| 491 |
$('srvStatus').textContent = 'Request submitted. Checking status...';
|
| 492 |
|
| 493 |
-
pollTaskStatus(taskId, sessionUid).then(finalStatus => {
|
| 494 |
if (finalStatus && (finalStatus.status === 'success' || finalStatus.status === 'completed')) {
|
| 495 |
getTaskResult(taskId, sessionUid, 'synthid');
|
| 496 |
}
|
|
@@ -500,14 +540,7 @@ $('btnSend').onclick = async () => {
|
|
| 500 |
const duration = window.taskStartTime ? ((performance.now() - window.taskStartTime) / 1000).toFixed(2) : 'N/A';
|
| 501 |
console.error(`[Main] Task submission failed after ${duration}s:`, e);
|
| 502 |
$('srvStatus').textContent = 'Failed to submit request. Please try again.';
|
| 503 |
-
|
| 504 |
-
show('progressContainer', false);
|
| 505 |
-
$('srvComputing').hidden = true;
|
| 506 |
-
window.processingStartTime = null; // Reset processing start time
|
| 507 |
-
if (progressTimer) {
|
| 508 |
-
clearInterval(progressTimer);
|
| 509 |
-
progressTimer = null;
|
| 510 |
-
}
|
| 511 |
}
|
| 512 |
};
|
| 513 |
|
|
|
|
| 299 |
}
|
| 300 |
};
|
| 301 |
|
| 302 |
+
async function pollTaskStatus(currentTaskId, currentUid, retryCount = 0, maxRetries = 10) {
|
| 303 |
try {
|
| 304 |
const statusResponse = await fetch(`${SERVER}/get_task_status?task_id=${currentTaskId}&uid=${currentUid}`);
|
| 305 |
if (!statusResponse.ok) {
|
|
|
|
| 375 |
}
|
| 376 |
return null;
|
| 377 |
} else {
|
| 378 |
+
// Calculate delay with exponential backoff (max 30 seconds)
|
| 379 |
+
const baseDelay = 5000;
|
| 380 |
+
const delay = Math.min(baseDelay * Math.pow(1.5, retryCount), 30000);
|
| 381 |
+
|
| 382 |
+
setTimeout(() => pollTaskStatus(currentTaskId, currentUid, retryCount, maxRetries).then(finalStatus => {
|
| 383 |
if (finalStatus && (finalStatus.status === 'success' || finalStatus.status === 'completed')) {
|
| 384 |
getTaskResult(currentTaskId, currentUid, 'synthid');
|
| 385 |
}
|
| 386 |
+
}), delay);
|
| 387 |
return null;
|
| 388 |
}
|
| 389 |
} catch (e) {
|
| 390 |
console.error('[Poll] Polling exception:', e);
|
| 391 |
+
|
| 392 |
+
// Check if we've exceeded max retries
|
| 393 |
+
if (retryCount >= maxRetries) {
|
| 394 |
+
console.error(`[Poll] Max retries (${maxRetries}) exceeded. Giving up.`);
|
| 395 |
+
$('srvStatus').textContent = 'Connection failed after multiple attempts. Please try again later.';
|
| 396 |
+
cleanupPolling();
|
| 397 |
+
return null;
|
| 398 |
}
|
| 399 |
+
|
| 400 |
+
// Distinguish between network errors and other errors
|
| 401 |
+
const isNetworkError = e.name === 'TypeError' && e.message.includes('fetch');
|
| 402 |
+
const isDnsError = e.message.includes('ERR_NAME_NOT_RESOLVED');
|
| 403 |
+
|
| 404 |
+
if (isNetworkError || isDnsError) {
|
| 405 |
+
console.warn(`[Poll] Network error (attempt ${retryCount + 1}/${maxRetries}):`, e.message);
|
| 406 |
+
$('srvStatus').textContent = `Connection error (attempt ${retryCount + 1}/${maxRetries}). Retrying...`;
|
| 407 |
+
|
| 408 |
+
// Calculate delay with exponential backoff
|
| 409 |
+
const baseDelay = 5000;
|
| 410 |
+
const delay = Math.min(baseDelay * Math.pow(1.5, retryCount), 30000);
|
| 411 |
+
|
| 412 |
+
setTimeout(() => pollTaskStatus(currentTaskId, currentUid, retryCount + 1, maxRetries).then(finalStatus => {
|
| 413 |
+
if (finalStatus && (finalStatus.status === 'success' || finalStatus.status === 'completed')) {
|
| 414 |
+
getTaskResult(currentTaskId, currentUid, 'synthid');
|
| 415 |
+
}
|
| 416 |
+
}), delay);
|
| 417 |
+
return null;
|
| 418 |
+
} else {
|
| 419 |
+
// Non-network error - don't retry
|
| 420 |
+
console.error('[Poll] Non-recoverable error:', e);
|
| 421 |
+
$('srvStatus').textContent = 'An unexpected error occurred. Please try again.';
|
| 422 |
+
cleanupPolling();
|
| 423 |
+
return null;
|
| 424 |
+
}
|
| 425 |
+
}
|
| 426 |
+
}
|
| 427 |
+
|
| 428 |
+
function cleanupPolling() {
|
| 429 |
+
show('spin', false);
|
| 430 |
+
show('progressContainer', false);
|
| 431 |
+
$('srvComputing').hidden = true;
|
| 432 |
+
window.processingStartTime = null;
|
| 433 |
+
if (progressTimer) {
|
| 434 |
+
clearInterval(progressTimer);
|
| 435 |
+
progressTimer = null;
|
| 436 |
}
|
| 437 |
}
|
| 438 |
|
|
|
|
| 452 |
|
| 453 |
if (!resultResponse.ok) {
|
| 454 |
const errorText = await resultResponse.text();
|
| 455 |
+
throw new Error(`Failed to get results: ${errorText}`);
|
| 456 |
}
|
| 457 |
|
| 458 |
const resultArrayBuffer = await resultResponse.arrayBuffer();
|
|
|
|
| 522 |
|
| 523 |
if (!startTaskResponse.ok) {
|
| 524 |
const errorText = await startTaskResponse.text();
|
| 525 |
+
throw new Error(`Server error: ${startTaskResponse.status} - ${errorText}`);
|
| 526 |
}
|
| 527 |
|
| 528 |
const { task_id: newTaskId } = await startTaskResponse.json();
|
|
|
|
| 530 |
console.log('[Main] Task submitted to server. Task ID:', taskId);
|
| 531 |
$('srvStatus').textContent = 'Request submitted. Checking status...';
|
| 532 |
|
| 533 |
+
pollTaskStatus(taskId, sessionUid, 0, 10).then(finalStatus => {
|
| 534 |
if (finalStatus && (finalStatus.status === 'success' || finalStatus.status === 'completed')) {
|
| 535 |
getTaskResult(taskId, sessionUid, 'synthid');
|
| 536 |
}
|
|
|
|
| 540 |
const duration = window.taskStartTime ? ((performance.now() - window.taskStartTime) / 1000).toFixed(2) : 'N/A';
|
| 541 |
console.error(`[Main] Task submission failed after ${duration}s:`, e);
|
| 542 |
$('srvStatus').textContent = 'Failed to submit request. Please try again.';
|
| 543 |
+
cleanupPolling();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 544 |
}
|
| 545 |
};
|
| 546 |
|