fix(brand): 更新后「在跑的任务列表」不再静默变空——桥迟到自动重试、拉取失败不删本地态
现场(用户 2026-10-22):点「立即更新」→ 闸门确认并等待 → 进程被 update.exe 替换、 WebView 整页重载。重载那一刻 pywebview 桥还没注入,getPywebviewApi() 返回的降级桥里 vc_ 方法数为 0,而变体采集页把「桥不在」画成「暂无变体采集任务」:loadTasks 四条出口 全是静默 return(无桥/并发中/无 uid/!success),空 catch 也一样;首拉只发一次, 60 秒轮询又被「列表里有活跃任务」反锁 ⇒ 撞空一次就永久空,除手点刷新没有出路。 另外两页是另一种"没掉":跟价 loadHistory 一次 catch 把列表清空,紧接的 syncPollingIdsWithHistory 把「history 没有 + 无快照」的 id 连 localStorage 快照一起删 (不可恢复);商品风险同型且更狠,连快照豁免都没有。更新的几十秒里进程已退、 新进程未起,任何一次 /history 失败或 401 都踩得到。 改法:新增 shared/bridges/bridge-list-state.ts(失败分类/可见文案/首拉重试驱动, 退避复用 getTaskPollBackoffMs,8 轮约 35 秒);变体采集页桥未就绪→重试到成功或次数 用尽,失败态写进空列表文案并给一次 ElMessage;跟价/商品风险加 historyLoadOk 守卫, 拉取失败不清列表、不结算、不删本地 id。静默 catch 棘轮 83→81(实测;本轮减 1, 另 1 处是此前台账未缩,已一并拉平并逐文件改小)。 门禁:npx vue-tsc --noEmit rc=0;npm test 905/905 全绿(含新增 7 例 tests/bridge-list-first-load-retry.test.ts,改前对旧代码真红)。
This commit is contained in:
@@ -776,16 +776,24 @@ async function loadDashboard() {
|
||||
}
|
||||
}
|
||||
|
||||
/** 上一轮 /history 是真拉到的还是失败的:失败轮没有任何资格判定任务「已不存在」 */
|
||||
let historyLoadOk = false
|
||||
|
||||
async function loadHistory() {
|
||||
try {
|
||||
const vo = await getPriceTrackHistory()
|
||||
historyItems.value = vo.items || []
|
||||
historyLoadOk = true
|
||||
} catch {
|
||||
historyItems.value = []
|
||||
// 失败保留上一轮列表:清成空会让紧接着的结算把在跑的任务判成「已不存在」,
|
||||
// 连带删掉 localStorage 里的轮询 id 与快照——一次抖动(或客户端更新的那几十秒)
|
||||
// 就能把「当前任务」整个抹没,用户看到的就是任务凭空消失。
|
||||
historyLoadOk = false
|
||||
}
|
||||
}
|
||||
|
||||
function syncPollingIdsWithHistory() {
|
||||
if (!historyLoadOk) return
|
||||
for (const taskId of [...pollingTaskIds.value]) {
|
||||
const any = historyItems.value.some((r) => r.taskId === taskId)
|
||||
if (!any && !taskSnapshots.value[taskId]) {
|
||||
|
||||
@@ -522,8 +522,13 @@ async function removeMatchedRow(row: ProductRiskShopQueueItem) {
|
||||
}
|
||||
}
|
||||
|
||||
/** 上一轮 /history 是真拉到的还是失败的:失败轮没有任何资格判定任务「已不存在」 */
|
||||
let historyLoadOk = false
|
||||
|
||||
/** 历史列表中已无该 taskId 时,停止轮询并清本地快照 */
|
||||
function syncPollingIdsWithHistory() {
|
||||
// 本轮 /history 没真拉到就不结算:否则一次拉取失败等于把每个在跑的任务判成「已不存在」
|
||||
if (!historyLoadOk) return
|
||||
for (const taskId of [...pollingTaskIds.value]) {
|
||||
const rows = historyItems.value.filter((r) => r.taskId === taskId)
|
||||
if (!rows.length || rows.some((row) => isHistoryItemTerminal(row))) {
|
||||
@@ -630,8 +635,11 @@ async function loadCandidates() {
|
||||
async function loadHistory() {
|
||||
try {
|
||||
historyItems.value = (await getProductRiskHistory()).items || []
|
||||
historyLoadOk = true
|
||||
} catch {
|
||||
historyItems.value = []
|
||||
// 失败保留上一轮列表:清成空会让下面的结算把每一个在跑的任务都判成「已不存在」,
|
||||
// 连带删光本地轮询 id——客户端更新/网络抖动那几十秒就足以把「当前任务」整个抹没。
|
||||
historyLoadOk = false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -92,7 +92,7 @@
|
||||
<button type="button" class="btn-refresh" @click="refreshList">刷新任务状态</button>
|
||||
</div>
|
||||
<div class="queue-body">
|
||||
<div v-if="!currentTasks.length" class="empty-tasks">暂无变体采集任务</div>
|
||||
<div v-if="!currentTasks.length" class="empty-tasks" :class="{ 'empty-warn': Boolean(listNotice) }">{{ listNotice || '暂无变体采集任务' }}</div>
|
||||
<div v-else class="queue-grid">
|
||||
<div v-for="item in currentTasks" :key="`vc-${item.task_id}`" class="task-card">
|
||||
<div class="task-card-head">
|
||||
@@ -171,6 +171,11 @@ import { saveUrlWithProgress } from '@/shared/utils/download-progress'
|
||||
import { isHttpUrl, safeHttpUrl } from '@/shared/utils/safe-url'
|
||||
import { createCategorizedTimers } from '@/shared/utils/categorized-timers'
|
||||
import { useHistoryPolling } from '@/shared/composables/useHistoryPolling'
|
||||
import {
|
||||
bridgeListHint,
|
||||
runBridgeListFirstLoad,
|
||||
type BridgeListAttempt,
|
||||
} from '@/shared/bridges/bridge-list-state'
|
||||
import { taskStatusClass, taskStatusText, type TaskStatusOptions } from '@/shared/utils/task-status-text'
|
||||
|
||||
/** 本地任务记录:桥接返回的列表项 + 前端维护字段 */
|
||||
@@ -206,6 +211,8 @@ const currentPage = ref(1)
|
||||
const totalPages = ref(1)
|
||||
const totalCount = ref(0)
|
||||
const isLoading = ref(false)
|
||||
/** 最近一次列表取回的结局:桥未就绪/登录态未恢复/请求失败都不得再伪装成「暂无任务」 */
|
||||
const listState = ref<BridgeListAttempt>('bridge')
|
||||
|
||||
const detailVisible = ref(false)
|
||||
const detailItem = ref<VariantEntry | null>(null)
|
||||
@@ -215,6 +222,8 @@ const asinLines = computed(() => asinText.value.split(/\r?\n/).map((s) => s.trim
|
||||
const currentTasks = computed(() =>
|
||||
currentTaskIds.value.map((id) => tasks[id]).filter((item): item is VariantEntry => Boolean(item)),
|
||||
)
|
||||
/** 列表为空时才给失败文案;有任务时不抢话 */
|
||||
const listNotice = computed(() => (currentTasks.value.length ? '' : bridgeListHint(listState.value)))
|
||||
const canSubmit = computed(() =>
|
||||
inputMode.value === 'asin' ? asinLines.value.length > 0 : selectedFiles.value.length > 0,
|
||||
)
|
||||
@@ -474,16 +483,25 @@ async function refreshTask(item: VariantTaskListItem) {
|
||||
}
|
||||
}
|
||||
|
||||
async function loadTasks(page?: number) {
|
||||
async function loadTasks(page?: number): Promise<BridgeListAttempt> {
|
||||
if (isLoading.value) return listState.value
|
||||
const bridge = getPywebviewApi()
|
||||
if (!bridge?.vc_list_tasks) return
|
||||
if (isLoading.value) return
|
||||
if (!uid()) return
|
||||
if (!bridge?.vc_list_tasks) {
|
||||
listState.value = 'bridge'
|
||||
return 'bridge'
|
||||
}
|
||||
if (!uid()) {
|
||||
listState.value = 'uid'
|
||||
return 'uid'
|
||||
}
|
||||
isLoading.value = true
|
||||
try {
|
||||
const targetPage = page || currentPage.value || 1
|
||||
const res = await bridge.vc_list_tasks({ uid: uid(), page: targetPage, page_size: PAGE_SIZE })
|
||||
if (!res?.success) return
|
||||
if (!res?.success) {
|
||||
listState.value = 'failed'
|
||||
return 'failed'
|
||||
}
|
||||
currentPage.value = res.page || targetPage || 1
|
||||
totalPages.value = Math.max(1, res.total_page || 1)
|
||||
totalCount.value = res.total || (res.tasks || []).length
|
||||
@@ -510,8 +528,12 @@ async function loadTasks(page?: number) {
|
||||
ids.push(item.task_id)
|
||||
})
|
||||
currentTaskIds.value = ids
|
||||
listState.value = 'ok'
|
||||
return 'ok'
|
||||
} catch {
|
||||
/* 静默:列表加载失败时保留现状 */
|
||||
// 失败不清列表(保留上一轮读数),但必须让用户看见「这次没取到」而不是「没有任务」
|
||||
listState.value = 'failed'
|
||||
return 'failed'
|
||||
} finally {
|
||||
isLoading.value = false
|
||||
}
|
||||
@@ -738,9 +760,23 @@ function cleanup() {
|
||||
timers.clearScope()
|
||||
}
|
||||
|
||||
/**
|
||||
* 首拉引导:客户端更新或启动的头几秒桥还没注入(getPywebviewApi() 退化成不含 vc_ 方法的
|
||||
* 降级桥),此前「onMounted 只发一次 + 轮询要等有活跃任务」两件事咬死——撞空一次就永久
|
||||
* 停在「暂无变体采集任务」,用户看到在跑的任务凭空没了。这里重试到成功或次数用尽。
|
||||
*/
|
||||
async function bootstrapListLoad() {
|
||||
const result = await runBridgeListFirstLoad(() => loadTasks(1), {
|
||||
sleep: (ms) => timers.sleep('list-bootstrap', ms),
|
||||
isCancelled: () => disposed,
|
||||
})
|
||||
if (result.cancelled || disposed || result.attempt === 'ok') return
|
||||
ElMessage.warning(`${bridgeListHint(result.attempt)},可点「刷新任务状态」再试`)
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
// 首发只发一次:此前 startPolling() 的立即 loadTasks(1) 与 scheduleAutoRefresh() 各打一遍同一页
|
||||
void loadTasks(1)
|
||||
// 只起这一条链:此前立即 loadTasks(1) 与 scheduleAutoRefresh() 各打一遍同一页
|
||||
void bootstrapListLoad()
|
||||
})
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
@@ -809,6 +845,7 @@ onBeforeUnmount(() => {
|
||||
.page-btn:disabled { opacity: 0.4; cursor: not-allowed; }
|
||||
.page-now { min-width: 22px; text-align: center; color: #c8d2e2; font-size: 12px; }
|
||||
.empty-tasks { color: #5e6878; font-size: 13px; padding: 12px 0; text-align: center; }
|
||||
.empty-warn { color: #e0b341; }
|
||||
.detail-body { max-height: 60vh; overflow: auto; }
|
||||
.kv-row { display: flex; gap: 12px; padding: 6px 0; font-size: 13px; }
|
||||
.kv-key { flex-shrink: 0; width: 110px; color: #5e6878; }
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
/**
|
||||
* 桌面桥任务列表的取回判据(客户端更新后「在跑的任务列表没掉」修复)。
|
||||
*
|
||||
* 客户端更新=进程被 update.exe 替换 + WebView 整页重载。这一瞬 `window.pywebview` 还没注入,
|
||||
* `getPywebviewApi()` 退化成不含任何 `vc_` 方法的降级桥,于是页面里那句
|
||||
* `if (!bridge?.vc_list_tasks) return` 把「桥还没起来」渲染成「暂无任务」;而列表轮询又被
|
||||
* 「列表里有活跃任务才启动」反锁——首拉撞上这个窗口 ⇒ 列表空 ⇒ 活跃任务恒 false ⇒
|
||||
* 轮询永不启动 ⇒ 这一页被钉死在空列表,除了手点刷新没有出路。
|
||||
*
|
||||
* 这里只收敛**无副作用的判据与首拉重试驱动**:败在哪一环、有没有可见文案、要不要接着试。
|
||||
* 计时器与页面状态由调用方注入,本模块不碰 window。
|
||||
*/
|
||||
|
||||
import { getTaskPollBackoffMs } from '../task-progress-config.ts'
|
||||
|
||||
/** 一次列表取回的结局:成功 / 桥未就绪 / 登录态未恢复 / 请求或后端失败 */
|
||||
export type BridgeListAttempt = 'ok' | 'bridge' | 'uid' | 'failed'
|
||||
|
||||
/** 首拉最多试几轮(含首次)。退避曲线复用任务轮询那一套:0.5s→1s→2s→4s→8s→10s 封顶,
|
||||
* 8 轮累计约 35 秒,覆盖客户端进程替换到页面可打桥的实测窗口。 */
|
||||
export const BRIDGE_LIST_FIRST_LOAD_MAX_ATTEMPTS = 8
|
||||
|
||||
/** 每种结局的用户可见文案;非 ok 一律不许空白呈现(静默 catch 棘轮同源口径)。 */
|
||||
const BRIDGE_LIST_HINTS: Record<Exclude<BridgeListAttempt, 'ok'>, string> = {
|
||||
bridge: '客户端正在启动或更新,任务列表暂时连不上,会自动重试…',
|
||||
uid: '登录态还没恢复,任务列表暂时取不到,请重新登录后再看',
|
||||
failed: '任务列表拉取失败,会自动重试',
|
||||
}
|
||||
|
||||
export function bridgeListHint(attempt: BridgeListAttempt): string {
|
||||
return attempt === 'ok' ? '' : BRIDGE_LIST_HINTS[attempt]
|
||||
}
|
||||
|
||||
/** 只有成功不必再试;其余三种都值得再试(桥会起来、登录态会补上、网络会恢复)。 */
|
||||
export function isRetryableBridgeListAttempt(attempt: BridgeListAttempt): boolean {
|
||||
return attempt !== 'ok'
|
||||
}
|
||||
|
||||
/** 第 attempts 次尝试(从 1 计)之后该等多久。 */
|
||||
export function bridgeListRetryDelayMs(attempts: number): number {
|
||||
return getTaskPollBackoffMs(Math.max(0, attempts - 1))
|
||||
}
|
||||
|
||||
export interface BridgeListFirstLoadOptions {
|
||||
/** 已尝试次数(含首次)的上限 */
|
||||
maxAttempts?: number
|
||||
/** 注入退避曲线(测试用) */
|
||||
delayMs?: (attempts: number) => number
|
||||
/** 注入等待:页面用 categorized-timers 的 sleep,卸载时随 scope 一起清掉 */
|
||||
sleep: (ms: number) => Promise<void>
|
||||
/** 组件已卸载/用户已切换:立即停止,不再续链 */
|
||||
isCancelled?: () => boolean
|
||||
/** 每轮结局回调(页面据此更新可见提示) */
|
||||
onAttempt?: (result: { attempt: BridgeListAttempt; attempts: number }) => void
|
||||
}
|
||||
|
||||
export interface BridgeListFirstLoadResult {
|
||||
attempt: BridgeListAttempt
|
||||
attempts: number
|
||||
/** 被调用方取消(组件卸载)时为 true:此时不代表列表真的取不到 */
|
||||
cancelled: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* 首拉驱动:把「只发一次就放弃」换成「重试到成功或次数用尽」。
|
||||
*
|
||||
* load() 必须自己把失败归成 'bridge' | 'uid' | 'failed' 并返回,不得抛(抛了这里不兜,
|
||||
* 免得把真 bug 吞成一次重试)。成功即停;失败按退避重试;取消即停且不算失败。
|
||||
*/
|
||||
export async function runBridgeListFirstLoad(
|
||||
load: () => Promise<BridgeListAttempt>,
|
||||
options: BridgeListFirstLoadOptions,
|
||||
): Promise<BridgeListFirstLoadResult> {
|
||||
const maxAttempts = options.maxAttempts ?? BRIDGE_LIST_FIRST_LOAD_MAX_ATTEMPTS
|
||||
const delayMs = options.delayMs ?? bridgeListRetryDelayMs
|
||||
let attempts = 0
|
||||
let attempt: BridgeListAttempt = 'bridge'
|
||||
while (attempts < maxAttempts) {
|
||||
if (options.isCancelled?.()) {
|
||||
return { attempt, attempts, cancelled: true }
|
||||
}
|
||||
attempt = await load()
|
||||
attempts += 1
|
||||
options.onAttempt?.({ attempt, attempts })
|
||||
if (!isRetryableBridgeListAttempt(attempt)) break
|
||||
if (attempts >= maxAttempts) break
|
||||
await options.sleep(delayMs(attempts))
|
||||
}
|
||||
return { attempt, attempts, cancelled: false }
|
||||
}
|
||||
Reference in New Issue
Block a user