fix(通知): 麦象不可读不再折叠成「无异常」、探活 4xx 不冒充正常、交付不完整独立成 scene,前端入队拒绝按机器码决定重试
- MaixiangConsoleClient 新增 probeReadable/classifyReadout,把四种「读不出来」的形状与
「确实无异常」分开;MaixiangAnomalyScanner 在六项扫描前先过这道闸,读不通就喊一条
天级去重的管理员异常并退出本轮——此前后台挂掉时六项一致报「无异常」,服务探测还是绿的。
- NotificationScanScheduler 的探活改三态 ALIVE/UNVERIFIED/DOWN:401/403/404 说明请求被
挡在业务之外,拿不到健康证据,不能再按 <500 判成「服务正常」。
- task_partial(交付不完整)从 task_failed 里拆出来独立成 scene,仍归任务异常大类;
NotificationDispatchService 的管理员行改用 SCENE_TASK_PARTIAL。
- 前端 dispatch-guard 用机器码 shop_busy/queue_full 判定「等一会儿再推就会成」,
暂缓 20s×15 封顶;此前页面靠 includes('当前店铺正在执行中') 匹配中文,而文案中间
插了店铺名,永远匹配不到⇒同店互斥被写成任务失败。
护栏:后端 notification 六支用例(含 MaixiangConsoleClientTest 15 例、
MaixiangAnomalyScannerTest 24 例)、前端 dispatch-guard 与 enqueue-rejection-wiring 两支;
后端全量 3827 例 0 红、前端 717 例 0 失败;变异 M1-M7、M12-M14 共 10/10 杀。
This commit is contained in:
@@ -126,6 +126,7 @@ import { createCategorizedTimers } from '@/shared/utils/categorized-timers'
|
||||
import { saveUrlWithProgress } from '@/shared/utils/download-progress'
|
||||
import {
|
||||
checkQueuePayload,
|
||||
classifyQueueRejection,
|
||||
guardBlocked,
|
||||
} from '@/shared/dispatch-guard'
|
||||
import { passGuard } from '@/shared/dispatch-guard-ui'
|
||||
@@ -1187,11 +1188,6 @@ function scheduleAutoAdvanceRetry(delayMs = 3000) {
|
||||
}, delayMs)
|
||||
}
|
||||
|
||||
function isQueueBusyError(message?: string) {
|
||||
if (!message) return false
|
||||
return message.includes('当前店铺正在执行中')
|
||||
}
|
||||
|
||||
function isPythonQueueBusy() {
|
||||
for (const task of sessionTasks.value) {
|
||||
for (const item of task.items) {
|
||||
@@ -1303,7 +1299,9 @@ async function runItem(item: DeleteBrandResultItem, options?: { auto?: boolean }
|
||||
qpr = `启动任务失败:${message}`
|
||||
queuePushResult.value = qpr
|
||||
saveSessionTask(taskId, sessionTask.items, qpr, queuePayloadText.value)
|
||||
if (options?.auto && isQueueBusyError(message)) {
|
||||
// 同店互斥/队列满只是"客户端这会儿接不下",交给自动续排稍后再推。
|
||||
// 页面过去自己匹配中文提示,而客户端把店名插在提示中间,那条判据从来没命中过。
|
||||
if (options?.auto && classifyQueueRejection(pushResult).deferrable) {
|
||||
return { success: false, retryable: true }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -234,8 +234,12 @@ import { getTaskPollIntervalMs } from '@/shared/task-progress-config'
|
||||
import { createCategorizedTimers } from '@/shared/utils/categorized-timers'
|
||||
import { saveUrlWithProgress } from '@/shared/utils/download-progress'
|
||||
import {
|
||||
ENQUEUE_DEFERRAL_ATTEMPTS,
|
||||
ENQUEUE_DEFERRAL_RETRY_MS,
|
||||
EXCEL_CSV_EXTENSIONS,
|
||||
checkQueuePayload,
|
||||
classifyQueueRejection,
|
||||
shouldDeferQueuePush,
|
||||
} from '@/shared/dispatch-guard'
|
||||
import { passGuard } from '@/shared/dispatch-guard-ui'
|
||||
import { useZiniaoVersion } from '@/shared/utils/ziniao-version'
|
||||
@@ -1061,20 +1065,30 @@ async function enqueueCreatedTask(api: PywebviewApi, taskId: number, queuePayloa
|
||||
await compensateDispatchFailure(taskId, reason)
|
||||
throw new Error(reason)
|
||||
}
|
||||
let pushResult: Awaited<ReturnType<NonNullable<PywebviewApi['enqueue_json']>>>
|
||||
try {
|
||||
pushResult = await api.enqueue_json(queuePayload)
|
||||
} catch (error) {
|
||||
const reason = `任务队列调用异常:${error instanceof Error ? error.message : String(error || '未知错误')}`
|
||||
await compensateDispatchFailure(taskId, reason)
|
||||
throw new Error(reason)
|
||||
let attempt = 0
|
||||
let reason = '任务队列拒绝接收任务'
|
||||
while (true) {
|
||||
if (disposed) throw new Error('component disposed')
|
||||
let pushResult: Awaited<ReturnType<NonNullable<PywebviewApi['enqueue_json']>>>
|
||||
try {
|
||||
pushResult = await api.enqueue_json(queuePayload)
|
||||
} catch (error) {
|
||||
reason = `任务队列调用异常:${error instanceof Error ? error.message : String(error || '未知错误')}`
|
||||
await compensateDispatchFailure(taskId, reason)
|
||||
throw new Error(reason)
|
||||
}
|
||||
if (pushResult?.success) return pushResult
|
||||
const rejection = classifyQueueRejection(pushResult)
|
||||
reason = pushResult?.error || '任务队列拒绝接收任务'
|
||||
// 同店互斥/队列满不是「任务失败」:客户端那边跑完就腾出位置了。等到预算用尽
|
||||
// 才落 FAILED —— 否则点重一次就把任务和已扣的次数一起判死,还多投一条任务失败通知。
|
||||
if (!shouldDeferQueuePush(rejection, attempt, ENQUEUE_DEFERRAL_ATTEMPTS)) break
|
||||
attempt += 1
|
||||
queuePushResult.value = `${reason},${ENQUEUE_DEFERRAL_RETRY_MS / 1000} 秒后重试(${attempt}/${ENQUEUE_DEFERRAL_ATTEMPTS})`
|
||||
await sleep(ENQUEUE_DEFERRAL_RETRY_MS)
|
||||
}
|
||||
if (!pushResult?.success) {
|
||||
const reason = pushResult?.error || '任务队列拒绝接收任务'
|
||||
await compensateDispatchFailure(taskId, reason)
|
||||
throw new Error(reason)
|
||||
}
|
||||
return pushResult
|
||||
await compensateDispatchFailure(taskId, reason)
|
||||
throw new Error(reason)
|
||||
}
|
||||
|
||||
async function compensateDispatchFailure(taskId: number, reason: string) {
|
||||
|
||||
@@ -2,11 +2,12 @@ import { get, post, type JavaApiResponse, unwrapJavaResponse } from '../../http.
|
||||
import { buildJavaUrl } from '../../url.ts'
|
||||
import { API_ENDPOINTS } from '../../endpoints.ts'
|
||||
|
||||
/** 通知场景:密钥欠费 / 密钥失效 / 任务失败 / 服务异常 / 麦象异常 / 系统。 */
|
||||
/** 通知场景:密钥欠费 / 密钥失效 / 任务失败 / 交付不完整 / 服务异常 / 麦象异常 / 系统。 */
|
||||
export type NotificationScene =
|
||||
| 'secret_balance'
|
||||
| 'secret_invalid'
|
||||
| 'task_failed'
|
||||
| 'task_partial'
|
||||
| 'service_down'
|
||||
| 'maixiang_anomaly'
|
||||
| 'system'
|
||||
|
||||
@@ -170,7 +170,7 @@ export interface PywebviewApi {
|
||||
}>;
|
||||
enqueue_json?: (
|
||||
data: unknown,
|
||||
) => Promise<{ success: boolean; queue_size?: number; error?: string }>;
|
||||
) => Promise<{ success: boolean; queue_size?: number; error?: string; code?: string }>;
|
||||
// —— ASIN 变体采集(VariantCollectionAPI,vc_* 前缀)——
|
||||
vc_select_input_files?: () => Promise<string[]>;
|
||||
vc_start_collect?: (params: {
|
||||
|
||||
@@ -694,3 +694,51 @@ export function findUnsafeJsonPaths(root: unknown): UnsafeJsonPath[] {
|
||||
walk(root, 'payload', 0, false)
|
||||
return found
|
||||
}
|
||||
|
||||
/** 客户端 enqueue_json 拒绝原因的分类。见 {@link classifyQueueRejection}。 */
|
||||
export type QueueRejectionKind = 'shop_busy' | 'queue_full' | 'other'
|
||||
|
||||
export interface QueueRejection {
|
||||
kind: QueueRejectionKind
|
||||
/** 等一会儿再推就可能成功(店铺跑完 / 队列腾出位置),不该落「任务失败」。 */
|
||||
deferrable: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* 把 enqueue_json 的失败返回分成「可以等」和「真失败」。
|
||||
*
|
||||
* 为什么要有这一步:三条推送路径过去只看 `success===false`,一律按失败收尾(后端标 FAILED、
|
||||
* 扣掉的次数不再回来),可「当前店铺 X 正在执行中」只是同店互斥——稍后重试就会成功。
|
||||
* 跟价页一度想按中文文案识别互斥,但用的是 `includes('当前店铺正在执行中')`,
|
||||
* 客户端的文案在中间插了店铺名(`当前店铺 鲍丽明-US 正在执行中`),永远匹配不到,
|
||||
* 于是那个"重试"闸门形同虚设。
|
||||
*
|
||||
* 判定顺序:新版客户端直接给 `code`(不依赖文案),老客户端没有 code 只能按文案兜底——
|
||||
* app-web 是服务端加载的,页面新版与客户端旧版会长期共存。文案匹配放宽中间内容,
|
||||
* 但不放宽到整段自由匹配,避免把「Excel 解析失败」这类真失败误判成可等待。
|
||||
*/
|
||||
export function classifyQueueRejection(
|
||||
result: { code?: unknown; error?: unknown } | null | undefined,
|
||||
): QueueRejection {
|
||||
const code = typeof result?.code === 'string' ? result.code : ''
|
||||
const message = typeof result?.error === 'string' ? result.error : ''
|
||||
let kind: QueueRejectionKind = 'other'
|
||||
if (code === 'shop_busy' || /当前店铺[\s\S]{0,200}正在执行中/.test(message)) {
|
||||
kind = 'shop_busy'
|
||||
} else if (code === 'queue_full' || message.includes('任务队列已满')) {
|
||||
kind = 'queue_full'
|
||||
}
|
||||
return { kind, deferrable: kind !== 'other' }
|
||||
}
|
||||
|
||||
/** 可等待拒绝的重试预算:20 秒一次、最多 15 次(约 5 分钟),用尽后按失败收尾。 */
|
||||
export const ENQUEUE_DEFERRAL_RETRY_MS = 20000
|
||||
export const ENQUEUE_DEFERRAL_ATTEMPTS = 15
|
||||
|
||||
/**
|
||||
* 「这次先等一等、别把任务判死」的判定:可等待 且 还没用尽预算。
|
||||
* 预算用尽后照旧落失败并带上原因——同店一直不放行不能无限期吊着页面。
|
||||
*/
|
||||
export function shouldDeferQueuePush(rejection: QueueRejection, attempt: number, maxAttempts: number): boolean {
|
||||
return rejection.deferrable && attempt < maxAttempts
|
||||
}
|
||||
|
||||
@@ -10,6 +10,8 @@ import {
|
||||
collectDistinctErrors,
|
||||
extensionOf,
|
||||
findUnsafeJsonPaths,
|
||||
classifyQueueRejection,
|
||||
shouldDeferQueuePush,
|
||||
guardPassed,
|
||||
type GuardResult,
|
||||
} from '../src/shared/dispatch-guard.ts'
|
||||
@@ -490,3 +492,38 @@ test('test_task_101_dispatch_guard_dependency_failure_does_not_throw', () => {
|
||||
// 修好之后同一入口可以继续用,说明模块没有内部状态残留
|
||||
assert.equal(checkParseResult({ taskId: 1, totalRows: 5, acceptedRows: 5 }).ok, true)
|
||||
})
|
||||
|
||||
// ------------------------------------------------------------ 队列拒绝分类
|
||||
|
||||
test('test_queue_rejection_shop_busy_is_caught_even_with_shop_name_inside_message', () => {
|
||||
// 互斥文案把店铺名插在「当前店铺」和「正在执行中」中间(app_client/main.py enqueue_json),
|
||||
// 旧页面用 includes('当前店铺正在执行中') 判互斥,带店名时永远匹配不到 ⇒ 同店互斥被写成任务失败。
|
||||
const legacy = classifyQueueRejection({ error: '当前店铺 鲍丽明-US 正在执行中,请等待店铺完成之后重试' })
|
||||
assert.equal(legacy.kind, 'shop_busy')
|
||||
assert.equal(legacy.deferrable, true)
|
||||
// 新版客户端直接给机器码,判定不再依赖中文文案
|
||||
assert.equal(classifyQueueRejection({ code: 'shop_busy', error: '文案随版本改' }).kind, 'shop_busy')
|
||||
})
|
||||
|
||||
test('test_queue_rejection_queue_full_is_deferrable_but_real_failures_are_not', () => {
|
||||
assert.equal(classifyQueueRejection({ code: 'queue_full' }).kind, 'queue_full')
|
||||
assert.equal(classifyQueueRejection({ code: 'queue_full' }).deferrable, true)
|
||||
assert.equal(classifyQueueRejection({ error: '任务队列已满(上限 50),请等待正在执行的任务完成后再提交' }).deferrable, true)
|
||||
// 数据本身不对:重试没有意义,必须照旧落失败并给出原因
|
||||
assert.equal(classifyQueueRejection({ error: '仅支持 JSON 对象或数组' }).kind, 'other')
|
||||
assert.equal(classifyQueueRejection({ error: '仅支持 JSON 对象或数组' }).deferrable, false)
|
||||
assert.equal(classifyQueueRejection({ code: 'parse_error', error: 'Excel 解析失败' }).deferrable, false)
|
||||
assert.equal(classifyQueueRejection(null).kind, 'other')
|
||||
assert.equal(classifyQueueRejection(undefined).deferrable, false)
|
||||
})
|
||||
|
||||
test('test_queue_deferral_budget_decision', () => {
|
||||
const busy = classifyQueueRejection({ code: 'shop_busy' })
|
||||
const hard = classifyQueueRejection({ code: 'parse_error', error: 'Excel 解析失败' })
|
||||
// 可以等 + 还没用尽预算才重试;两者任缺其一都落回「按失败收尾」
|
||||
assert.equal(shouldDeferQueuePush(busy, 0, 10), true)
|
||||
assert.equal(shouldDeferQueuePush(busy, 9, 10), true)
|
||||
assert.equal(shouldDeferQueuePush(busy, 10, 10), false, '预算用尽不得再等')
|
||||
assert.equal(shouldDeferQueuePush(hard, 0, 10), false, '真失败没有重试一说')
|
||||
assert.equal(shouldDeferQueuePush(busy, 0, 0), false)
|
||||
})
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { resolve, dirname } from 'node:path'
|
||||
|
||||
/**
|
||||
* 入队被拒后的收尾接线(A4 同店互斥不再被写成任务失败)。
|
||||
*
|
||||
* 判定逻辑本体在 src/shared/dispatch-guard.ts,那边有真单元测试;这里钉两个组件里的
|
||||
* 接线——.vue 组件无法在本套里挂载,只能按源码形状钉(readSource 约定)。
|
||||
* 要防的回归形状只有一个:把「等一会儿再推」的分支删掉,回到 success=false 就
|
||||
* markPriceTrackDispatchFailed(→FAILED + 扣次数不回 + 通知面多一条任务失败)。
|
||||
*/
|
||||
const here = dirname(fileURLToPath(import.meta.url))
|
||||
const priceTrack = readFileSync(resolve(here, '../src/pages/brand/components/BrandPriceTrackTab.vue'), 'utf8')
|
||||
const deleteBrand = readFileSync(resolve(here, '../src/pages/brand/components/BrandDeleteBrandTab.vue'), 'utf8')
|
||||
|
||||
function bodyOf(source: string, startMarker: string, endMarker: string, label: string) {
|
||||
const start = source.indexOf(startMarker)
|
||||
assert.notEqual(start, -1, `${label} 里找不到 ${startMarker}`)
|
||||
const end = source.indexOf(endMarker, start)
|
||||
assert.notEqual(end, -1, `${label} 里找不到 ${endMarker}`)
|
||||
return source.slice(start, end)
|
||||
}
|
||||
|
||||
test('price_track_enqueue_defers_shop_busy_before_marking_failed', () => {
|
||||
const fn = bodyOf(
|
||||
priceTrack,
|
||||
'async function enqueueCreatedTask',
|
||||
'async function compensateDispatchFailure',
|
||||
'BrandPriceTrackTab.vue',
|
||||
)
|
||||
assert.match(fn, /classifyQueueRejection\(pushResult\)/, '没有对拒绝原因分类就直接收尾')
|
||||
assert.match(fn, /shouldDeferQueuePush\(rejection, attempt, /, '没有重试预算判定')
|
||||
const deferStart = fn.indexOf('shouldDeferQueuePush')
|
||||
const sleepAt = fn.indexOf('await sleep(', deferStart)
|
||||
assert.notEqual(sleepAt, -1, '可等待分支没有真的等')
|
||||
assert.notEqual(fn.indexOf('compensateDispatchFailure', sleepAt), -1, '预算用尽后仍应落失败并带上原因')
|
||||
// 等待分支必须自己回到循环,不能"等完还是判失败"——那等于把误判推迟一轮
|
||||
assert.doesNotMatch(fn.slice(deferStart, sleepAt), /compensateDispatchFailure/, '可等待分支不该直接收尾')
|
||||
assert.match(fn, /while \(true\)/, '重试要回到同一个推送入口')
|
||||
})
|
||||
|
||||
test('price_track_still_compensates_hard_failures', () => {
|
||||
// 反向钉:数据校验没过、桥调用抛异常、以及"不可等待/预算用尽"这三类都要照旧落失败带原因,
|
||||
// 别把「不再误判互斥」做成「什么都不判失败」。
|
||||
const fn = bodyOf(
|
||||
priceTrack,
|
||||
'async function enqueueCreatedTask',
|
||||
'async function compensateDispatchFailure',
|
||||
'BrandPriceTrackTab.vue',
|
||||
)
|
||||
assert.match(fn, /passGuard\(guard\)/, '入队前的数据闸还在')
|
||||
assert.match(fn, /catch \(error\)[\s\S]{0,240}compensateDispatchFailure\(taskId, reason\)/,
|
||||
'桥调用异常仍须立即落失败')
|
||||
assert.equal((fn.match(/compensateDispatchFailure\(taskId, reason\)/g) || []).length, 3,
|
||||
'三处收尾:数据闸 / 桥异常 / 拒绝且不可等待')
|
||||
})
|
||||
|
||||
test('delete_brand_tab_uses_the_shared_classifier', () => {
|
||||
// 旧写法 includes('当前店铺正在执行中') 匹配不到带店名的文案(客户端两个互斥出口都带名),
|
||||
// 自动续跑的"重试"闸因此从未生效过。
|
||||
assert.doesNotMatch(deleteBrand, /includes\('当前店铺正在执行中'\)/, '仍在用匹配不到的中文串判互斥')
|
||||
assert.doesNotMatch(deleteBrand, /function isQueueBusyError/, '旧判定函数应随之删除,别留死代码')
|
||||
assert.match(deleteBrand, /classifyQueueRejection\(pushResult\)/, '没有改用共享分类器')
|
||||
})
|
||||
|
||||
test('both_tabs_import_the_classifier', () => {
|
||||
for (const [name, src] of [['BrandPriceTrackTab.vue', priceTrack], ['BrandDeleteBrandTab.vue', deleteBrand]] as const) {
|
||||
assert.match(src, /classifyQueueRejection/, `${name} 未引用分类器`)
|
||||
assert.match(src, /from '@\/shared\/dispatch-guard'/, `${name} 未从 dispatch-guard 导入`)
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user