增加失败原因和优化进度条
This commit is contained in:
@@ -109,6 +109,9 @@ export function cancelLocalRebill(taskId) {
|
||||
})
|
||||
}
|
||||
|
||||
/** One in-flight poll per taskId — avoids stacked loops that flood XHR and freeze the page. */
|
||||
const localRebillPollControllers = new Map()
|
||||
|
||||
/**
|
||||
* Poll local rebill task until terminal status.
|
||||
* @param {string} taskId
|
||||
@@ -117,22 +120,48 @@ export function cancelLocalRebill(taskId) {
|
||||
export async function pollLocalRebillTask(taskId, onProgress) {
|
||||
const id = String(taskId || '')
|
||||
if (!id) throw new Error('missing local rebill taskId')
|
||||
|
||||
const prev = localRebillPollControllers.get(id)
|
||||
if (prev) prev.aborted = true
|
||||
const ctrl = { aborted: false }
|
||||
localRebillPollControllers.set(id, ctrl)
|
||||
|
||||
const started = Date.now()
|
||||
const maxMs = 30 * 60 * 1000
|
||||
while (Date.now() - started < maxMs) {
|
||||
const res = await getLocalRebillTask(id)
|
||||
const task = res.data || {}
|
||||
if (typeof onProgress === 'function') onProgress(task)
|
||||
if (task.status === 'SUCCESS') return { task }
|
||||
if (task.status === 'CANCELLED') return { task, cancelled: true }
|
||||
if (task.status === 'FAIL') {
|
||||
const err = new Error(task.error || task.message || 'local rebill failed')
|
||||
err.task = task
|
||||
throw err
|
||||
let lastSig = ''
|
||||
try {
|
||||
while (Date.now() - started < maxMs) {
|
||||
if (ctrl.aborted) {
|
||||
const err = new Error('local rebill poll superseded')
|
||||
err.superseded = true
|
||||
throw err
|
||||
}
|
||||
// Hidden tab: slow down; no need to hammer Redis while user is away.
|
||||
const hidden = typeof document !== 'undefined' && document.visibilityState === 'hidden'
|
||||
const res = await getLocalRebillTask(id)
|
||||
const task = res.data || {}
|
||||
const st = String(task.status || '').toUpperCase()
|
||||
const sig = [st, task.percent, task.success, task.fail, task.total].join('|')
|
||||
if (typeof onProgress === 'function' && sig !== lastSig) {
|
||||
lastSig = sig
|
||||
onProgress(task)
|
||||
}
|
||||
if (st === 'SUCCESS') return { task }
|
||||
if (st === 'CANCELLED') return { task, cancelled: true }
|
||||
if (st === 'FAIL') {
|
||||
const err = new Error(task.error || task.message || 'local rebill failed')
|
||||
err.task = task
|
||||
throw err
|
||||
}
|
||||
// 1s visible / 2.5s hidden — dense 400ms polls were freezing the list page.
|
||||
await new Promise(resolve => setTimeout(resolve, hidden ? 2500 : 1000))
|
||||
}
|
||||
throw new Error('local rebill timeout')
|
||||
} finally {
|
||||
if (localRebillPollControllers.get(id) === ctrl) {
|
||||
localRebillPollControllers.delete(id)
|
||||
}
|
||||
await new Promise(resolve => setTimeout(resolve, 800))
|
||||
}
|
||||
throw new Error('local rebill timeout')
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user