(fetchTask, onUpdate, maxWaitMs = 20 * 60 * 1000, options = {})
| 74 | } |
| 75 | |
| 76 | async function watchDomainTask(fetchTask, onUpdate, maxWaitMs = 20 * 60 * 1000, options = {}) { |
| 77 | const startedAt = Date.now(); |
| 78 | const poller = createAdaptivePoller({ |
| 79 | baseIntervalMs: Number(options.baseIntervalMs || 1200), |
| 80 | maxIntervalMs: Number(options.maxIntervalMs || 12000), |
| 81 | }); |
| 82 | let lastError = null; |
| 83 | |
| 84 | while (Date.now() - startedAt < maxWaitMs) { |
| 85 | try { |
| 86 | const task = await fetchTask(); |
| 87 | poller.recordSuccess(); |
| 88 | |
| 89 | if (typeof onUpdate === 'function') { |
| 90 | onUpdate(task); |
| 91 | } |
| 92 | |
| 93 | const status = String(task?.status || '').toLowerCase(); |
| 94 | if (TASK_TERMINAL_STATUSES.has(status)) { |
| 95 | return task; |
| 96 | } |
| 97 | } catch (error) { |
| 98 | lastError = error; |
| 99 | const statusCode = Number(error?.response?.status || 0); |
| 100 | if (statusCode === 404) { |
| 101 | throw error; |
| 102 | } |
| 103 | poller.recordError(); |
| 104 | } |
| 105 | |
| 106 | await sleep(poller.nextDelay({ forceSlow: !api.networkOnline })); |
| 107 | } |
| 108 | |
| 109 | if (lastError && lastError.message) { |
| 110 | throw new Error(`任务等待超时: ${lastError.message}`); |
| 111 | } |
| 112 | throw new Error('任务等待超时,请稍后刷新查看结果'); |
| 113 | } |
| 114 | |
| 115 | async function watchAccountTask(taskId, onUpdate, maxWaitMs = 20 * 60 * 1000) { |
| 116 | return watchDomainTask( |
no test coverage detected