From 7887e7ee37acd257a4837c0af0be3a2e67188ae5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E8=87=AA=E8=BE=BE?= <980324341@qq.com> Date: Tue, 6 Oct 2026 11:46:54 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E9=80=9A=E7=9F=A5):=20=E9=BA=A6=E8=B1=A1?= =?UTF-8?q?=E4=B8=8D=E5=8F=AF=E8=AF=BB=E4=B8=8D=E5=86=8D=E6=8A=98=E5=8F=A0?= =?UTF-8?q?=E6=88=90=E3=80=8C=E6=97=A0=E5=BC=82=E5=B8=B8=E3=80=8D=E3=80=81?= =?UTF-8?q?=E6=8E=A2=E6=B4=BB=204xx=20=E4=B8=8D=E5=86=92=E5=85=85=E6=AD=A3?= =?UTF-8?q?=E5=B8=B8=E3=80=81=E4=BA=A4=E4=BB=98=E4=B8=8D=E5=AE=8C=E6=95=B4?= =?UTF-8?q?=E7=8B=AC=E7=AB=8B=E6=88=90=20scene=EF=BC=8C=E5=89=8D=E7=AB=AF?= =?UTF-8?q?=E5=85=A5=E9=98=9F=E6=8B=92=E7=BB=9D=E6=8C=89=E6=9C=BA=E5=99=A8?= =?UTF-8?q?=E7=A0=81=E5=86=B3=E5=AE=9A=E9=87=8D=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 杀。 --- .../client/MaixiangConsoleClient.java | 67 ++++++++++++- .../service/MaixiangAnomalyScanner.java | 16 +++- .../service/NotificationDispatchService.java | 16 ++-- .../service/NotificationScanScheduler.java | 67 ++++++++++--- .../service/NotificationService.java | 4 + .../client/MaixiangConsoleClientTest.java | 23 +++++ .../service/MaixiangAnomalyScannerTest.java | 44 +++++++++ ...otificationDispatchServiceRecheckTest.java | 6 +- .../NotificationDispatchServiceTest.java | 24 +++++ .../NotificationScanSchedulerTest.java | 95 +++++++++++++++++++ .../service/NotificationServiceTest.java | 16 ++++ .../brand/components/BrandDeleteBrandTab.vue | 10 +- .../brand/components/BrandPriceTrackTab.vue | 40 +++++--- .../shared/api/types/modules/notification.ts | 3 +- frontend-vue/src/shared/bridges/pywebview.ts | 2 +- frontend-vue/src/shared/dispatch-guard.ts | 48 ++++++++++ frontend-vue/tests/dispatch-guard.test.ts | 37 ++++++++ .../tests/enqueue-rejection-wiring.test.ts | 74 +++++++++++++++ 18 files changed, 545 insertions(+), 47 deletions(-) create mode 100644 frontend-vue/tests/enqueue-rejection-wiring.test.ts diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/notification/client/MaixiangConsoleClient.java b/backend-java/src/main/java/com/nanri/aiimage/modules/notification/client/MaixiangConsoleClient.java index 33659dc88..29d8595ba 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/notification/client/MaixiangConsoleClient.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/notification/client/MaixiangConsoleClient.java @@ -44,6 +44,37 @@ public class MaixiangConsoleClient { return hasText(properties.getPriceTrackApiUrl()) && hasText(properties.getMaixiangConsoleToken()); } + /** + * 后台数据可读性探测:真拉一次队列状态接口(最轻的一个),把「读不到」与「读到但没异常」分开。 + * + *

异常扫描的六项检查全靠后台接口取数,读不通时它们只会一致地报「一切正常」;而 HTTP 200 + + * code!=1(令牌失效 / 接口未部署 / 契约变更)连服务探测都是绿的 —— 那就是长期的静默失效面。 + */ + public ConsoleReadout probeReadable() { + Attempt attempt = attempt(buildUrl("/api/console/queue/status")); + return classifyReadout(attempt.body(), attempt.httpError()); + } + + /** 判定一次取数结果的可读性(包内可见,供单测直接喂四种失败形态的样例)。 */ + ConsoleReadout classifyReadout(String body, String httpError) { + if (httpError != null) { + return ConsoleReadout.unreadable("接口请求失败:" + httpError); + } + if (!hasText(body)) { + return ConsoleReadout.unreadable("接口返回空响应"); + } + JsonNode root; + try { + root = objectMapper.readTree(body); + } catch (Exception ex) { + return ConsoleReadout.unreadable("响应不是合法 JSON(可能被网关拦截):" + ex.getMessage()); + } + if (root.path("code").asInt(-1) != 1) { + return ConsoleReadout.unreadable("接口返回失败:" + root.path("mes").asText("")); + } + return ConsoleReadout.OK; + } + /** * 批量任务列表(all_task)。updateTimeBefore 非空时只取「最后更新」不晚于该时刻的任务 * (SQL 字符串比较)。用 update_time 过滤而不是 create_time:desc(id) 分页下按创建时间过滤 @@ -112,22 +143,30 @@ public class MaixiangConsoleClient { /** 拉取并展开统一信封 {code,mes,data};失败返回 null(已记日志)。 */ private JsonNode fetch(String url, String scene) { - String body; + Attempt attempt = attempt(url); + if (attempt.httpError() != null) { + log.warn("[maixiang-console] 请求失败 scene={} err={}", scene, attempt.httpError()); + return null; + } + return parseEnvelope(attempt.body(), scene); + } + + /** 发一次只读 GET:body 与 httpError 恰有一个非空,异常语义保留给调用方判定。 */ + private Attempt attempt(String url) { try { // 用 URI 对象提交:URL 已由 buildUrl 编码过一次,不能再经 RestClient 模板再次编码 // (字符串形态会被二次编码,%20 变 %2520,服务端 DATETIME 参数直接报错) - body = RestClient.builder() + String body = RestClient.builder() .requestFactory(HttpClientPool.requestFactory(READ_TIMEOUT_MILLIS)) .build() .get() .uri(URI.create(url)) .retrieve() .body(String.class); + return new Attempt(body, null); } catch (Exception ex) { - log.warn("[maixiang-console] 请求失败 scene={} err={}", scene, ex.getMessage()); - return null; + return new Attempt(null, ex.getMessage() == null ? ex.getClass().getSimpleName() : ex.getMessage()); } - return parseEnvelope(body, scene); } /** 组装后台接口 URL(含 token 与查询参数,统一编码一次;包内可见供单测校验编码行为)。 */ @@ -272,6 +311,24 @@ public class MaixiangConsoleClient { // ======================== 数据模型 ======================== + /** + * 后台可读性探测结果。 + * + * @param readable false = 本轮所有后台读数都不可信(≠「没有异常」) + * @param reason 不可读原因,含服务端 mes,直接进告警文案 + */ + public record ConsoleReadout(boolean readable, String reason) { + public static final ConsoleReadout OK = new ConsoleReadout(true, ""); + + public static ConsoleReadout unreadable(String reason) { + return new ConsoleReadout(false, reason); + } + } + + /** 一次取数的原始结果:{@code body} 与 {@code httpError} 互斥。 */ + private record Attempt(String body, String httpError) { + } + /** 批量任务(all_task)单条;{@code resMes} 是进度/结果文字(旧版接口缺失时为空串)。 */ public record BatchTaskItem(String taskId, String taskType, String uid, int status, LocalDateTime createTime, LocalDateTime updateTime, diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/notification/service/MaixiangAnomalyScanner.java b/backend-java/src/main/java/com/nanri/aiimage/modules/notification/service/MaixiangAnomalyScanner.java index 742cec422..35251e73f 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/notification/service/MaixiangAnomalyScanner.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/notification/service/MaixiangAnomalyScanner.java @@ -6,6 +6,7 @@ import com.nanri.aiimage.config.NotificationProperties; import com.nanri.aiimage.modules.notification.client.MaixiangConsoleClient; import com.nanri.aiimage.modules.notification.client.MaixiangConsoleClient.BatchTaskItem; import com.nanri.aiimage.modules.notification.client.MaixiangConsoleClient.BatchTaskPage; +import com.nanri.aiimage.modules.notification.client.MaixiangConsoleClient.ConsoleReadout; import com.nanri.aiimage.modules.notification.client.MaixiangConsoleClient.QueueStateItem; import com.nanri.aiimage.modules.notification.client.MaixiangConsoleClient.SingleTaskItem; import com.nanri.aiimage.modules.notification.client.MaixiangConsoleClient.SingleTaskPage; @@ -26,7 +27,7 @@ import java.util.stream.Collectors; /** * 麦象(18960 任务调度)异常扫描:把「采集端出了事但没人知道」的情况推给管理员。 * - *

六类检查(全部只读,通过 {@link MaixiangConsoleClient} 的后台接口取数): + *

一道「后台可读性」闸门 + 六类检查(全部只读,通过 {@link MaixiangConsoleClient} 的后台接口取数): *

    *
  1. 采集机掉线:采集端打点的机器级静默超龄 —— 唯一能直接反映「整机停摆」的信号;
  2. *
  3. 批量任务停滞:status=0/1 且超过阈值无更新(如消费端 worker 掉线中断采集);
  4. @@ -36,6 +37,9 @@ import java.util.stream.Collectors; *
  5. 队列积压:task:queue 堆积且无人消费 / task:processing 超过阈值。
  6. *
* + *

「后台可读性」闸门为什么要排在最前:六项检查依赖同一批接口,接口整体读不通时它们的一致结果是 + * 「查无异常」而不是「扫不出」;HTTP 200 + code!=1(令牌失效、接口未部署、契约变更)连服务探测都是绿的。 + * *

「采集端静默」为什么要单列:其余五类都靠「任务 update_time 不新鲜」间接推断, * 一次整机停摆会被摊成几十条"任务失败",用户和运维看到的是大面积业务失败而不是 * "采集机掉线"(2026-09-29 停摆 6 小时即如此,且当时 210 条 pending 没到 300 的阈值, @@ -97,6 +101,16 @@ public class MaixiangAnomalyScanner { } NotificationDispatchService.AdminAudience audience = notificationDispatchService.prepareAdminAudience(); LocalDateTime now = LocalDateTime.now(); + ConsoleReadout readout = consoleClient.probeReadable(); + if (!readout.readable()) { + // 后台读不通时下面六项只会一致地报「无异常」(服务探测甚至还是绿的),先喊出来再退出本轮 + push(audience, "麦象后台数据不可读", + "麦象后台接口取不到数据,本轮异常扫描已跳过(掉线/停滞/滞留/失败/空结果/队列积压六项都不会报," + + "而 18960 服务探测可能仍是正常)。原因:" + readout.reason() + + "。请检查后台令牌 maixiang-console-token 是否失效、麦象是否升级换了接口契约。", + "maixiang_console_unreadable:" + now.format(DAY_FORMAT)); + return; + } runCheck("采集端静默", () -> checkCollectorSilent(audience, now)); runCheck("批量任务停滞", () -> checkStuckBatchTasks(audience, now)); runCheck("单任务滞留", () -> checkStuckSingleTasks(audience, now)); diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/notification/service/NotificationDispatchService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/notification/service/NotificationDispatchService.java index 674f34da8..9563179c4 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/notification/service/NotificationDispatchService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/notification/service/NotificationDispatchService.java @@ -109,9 +109,12 @@ public class NotificationDispatchService { * 「任务成功,但有一部分行没跑出来」通知:整任务没失败、铃铛里的 task_failed 只覆盖终态失败, * 这类部分缺失过去对用户完全不可见(品牌检测的查询失败行只躺在结果文件的一张子表里)。 * - *

场景沿用 {@code task_failed}(大类仍是「任务异常」),去重键按任务维度:同一任务重复 - * 组装结果时刷新内容而不是刷屏。不写 task.errorMessage —— 结果组装的失败判据读那个字段, - * 写了会把任务翻成 FAILED,与「状态保持成功、只提示交付不完整」的产品口径相悖。 + *

场景用独立的 {@code task_partial}(大类仍是「任务异常」,用户看到的分类没变):过去它 + * 沿用 task_failed,巡检按 task_failed 独立桶数报「窗口内 N 条独立失败」时会把交付不完整一起 + * 数成任务失败(2026-10-05 值班:窗口 22 行里 20 行是这种),既误报也压住了真失败的读数。 + * 去重键按任务维度不变:同一任务重复组装结果时刷新内容而不是刷屏。不写 task.errorMessage —— + * 结果组装的失败判据读那个字段,写了会把任务翻成 FAILED,与「状态保持成功、只提示交付不完整」 + * 的产品口径相悖。 */ public void pushPartialResult(Long userId, Long taskId, String moduleType, String taskNo, String detail) { if (userId == null || userId <= 0 || taskId == null || detail == null || detail.isBlank()) { @@ -120,10 +123,10 @@ public class NotificationDispatchService { String title = TaskModuleRegistry.labelOf(moduleType) + "任务部分结果缺失"; String displayTaskNo = taskNo == null || taskNo.isBlank() ? "#" + taskId : taskNo.trim(); notificationService.pushOrRefresh(userId, NotificationService.AUDIENCE_USER, - NotificationService.SCENE_TASK_FAILED, NotificationService.LEVEL_WARNING, + NotificationService.SCENE_TASK_PARTIAL, NotificationService.LEVEL_WARNING, title, "任务 " + displayTaskNo + ":" + detail + "。任务已完成,其余结果正常,可在结果文件里查看缺失清单。", "task_partial:" + taskId); - pushToAdmins(NotificationService.SCENE_TASK_FAILED, NotificationService.LEVEL_WARNING, + pushToAdmins(NotificationService.SCENE_TASK_PARTIAL, NotificationService.LEVEL_WARNING, "用户" + title, "用户 " + displayNameOf(userId) + "(uid=" + userId + ")任务 " + displayTaskNo + ":" + detail, "task_partial_admin:" + taskId, userId); @@ -135,6 +138,7 @@ public class NotificationDispatchService { * *

只推用户不推管理员:这是部分缺失(task_partial)的自动修复跟进,管理员侧 * 已经在缺失发生时知晓,补测成功属于正向收敛——再扇出一份只放大铃铛噪声。 + * 场景与缺失那条同为 task_partial:它说的还是同一件「交付不完整」的事,不是任务失败。 * 同任务同自然日一条(dedupe_key 带日期):补测按轮推进,同一轮内多品牌合并成一条。 */ public void pushRecheckOutcome(Long userId, Long taskId, String moduleType, String taskNo, @@ -165,7 +169,7 @@ public class NotificationDispatchService { String level = fixed && pendingRows <= 0 ? NotificationService.LEVEL_INFO : NotificationService.LEVEL_WARNING; notificationService.pushOrRefresh(userId, NotificationService.AUDIENCE_USER, - NotificationService.SCENE_TASK_FAILED, level, title, content, + NotificationService.SCENE_TASK_PARTIAL, level, title, content, "collect_recheck:" + taskId + ":" + java.time.LocalDate.now() .format(java.time.format.DateTimeFormatter.BASIC_ISO_DATE)); } diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/notification/service/NotificationScanScheduler.java b/backend-java/src/main/java/com/nanri/aiimage/modules/notification/service/NotificationScanScheduler.java index c1fa8a3a2..d68f30e4e 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/notification/service/NotificationScanScheduler.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/notification/service/NotificationScanScheduler.java @@ -200,9 +200,10 @@ public class NotificationScanScheduler { } /** HTTP 探测:失败立即重试一次,两次都失败才算故障(过滤瞬时抖动)。 */ - private ProbeOutcome probeHttpWithRetry(String url) { + ProbeOutcome probeHttpWithRetry(String url) { ProbeOutcome first = probeHttpOnce(url); - if (first.ok()) { + if (first.verdict() != ProbeVerdict.DOWN) { + // ALIVE 无需重试;UNVERIFIED 是确定性结果,再打一次只多一份日志 return first; } log.warn("[notification-scan] 服务探测第一次失败,重试一次 url={} detail={}", url, first.detail()); @@ -219,14 +220,13 @@ public class NotificationScanScheduler { .uri(url) .exchange((request, response) -> response.getStatusCode().value()); long latency = System.currentTimeMillis() - startMillis; - // 任意 HTTP < 500 视为存活(4xx 说明服务在线,仅路径/鉴权问题) - if (status != null && status < 500) { - return new ProbeOutcome(true, "HTTP " + status + "(" + latency + "ms)"); + if (status == null) { + return ProbeOutcome.unverified("无响应状态码(" + latency + "ms)"); } - return new ProbeOutcome(false, "HTTP " + status + "(" + latency + "ms)"); + return probeOutcomeFor(status, latency); } catch (Exception ex) { long latency = System.currentTimeMillis() - startMillis; - return new ProbeOutcome(false, rootCauseMessage(ex) + "(" + latency + "ms)"); + return ProbeOutcome.down(rootCauseMessage(ex) + "(" + latency + "ms)"); } } @@ -235,7 +235,7 @@ public class NotificationScanScheduler { try { ProxyBalancePort.ProxyBalanceSnapshot balance = proxyBalancePort.fetchBalance(); if (balance.available()) { - return new ProbeOutcome(true, "余额接口可用 surplus=" + balance.surplus() + return ProbeOutcome.alive("余额接口可用 surplus=" + balance.surplus() + " balance=" + balance.balance()); } String message = balance.message() == null ? "" : balance.message(); @@ -243,9 +243,9 @@ public class NotificationScanScheduler { log.info("[notification-scan] jikip 探测跳过:{}", message); return null; } - return new ProbeOutcome(false, message.isEmpty() ? "余额接口不可用" : message); + return ProbeOutcome.down(message.isEmpty() ? "余额接口不可用" : message); } catch (Exception ex) { - return new ProbeOutcome(false, rootCauseMessage(ex)); + return ProbeOutcome.down(rootCauseMessage(ex)); } } @@ -254,6 +254,13 @@ public class NotificationScanScheduler { if (outcome == null) { return; } + if (outcome.verdict() == ProbeVerdict.UNVERIFIED) { + // 服务回了个「证明不了业务可用」的状态码(401/403/404):既不能报正常, + // 也不能报不可用——报出去就是把探活脚本的问题写成下游事故。 + log.warn("[notification-scan] 服务探测未验证业务可用性 service={} detail={}" + + "(不发服务不可用通知,请核对探测地址与鉴权)", serviceLabel, outcome.detail()); + return; + } if (outcome.ok()) { log.info("[notification-scan] 服务探测正常 service={} detail={}", serviceLabel, outcome.detail()); return; @@ -349,7 +356,45 @@ public class NotificationScanScheduler { LocalDateTime updatedAt) { } + /** 探测三态:ALIVE=业务可用;UNVERIFIED=服务回了话但证不了业务可用;DOWN=故障。 */ + enum ProbeVerdict { ALIVE, UNVERIFIED, DOWN } + + /** + * 状态码归类。 + * + * 旧口径是「任意 HTTP < 500 视为存活」,于是 404(探测路径没人接)、401/403 + * (探活侧没带鉴权)都被写成「服务探测正常」——现网品牌检测服务就是这样绿了三个小时。 + * 这三个码只说明「对端有 HTTP 服务」,不说明被测业务可用,所以单独一态:不报正常、 + * 也不推「服务不可用」(推出去就是把探活脚本的问题写成下游事故)。其余 4xx 与 5xx 仍算故障。 + */ + static ProbeOutcome probeOutcomeFor(int status, long latencyMillis) { + String detail = "HTTP " + status + "(" + latencyMillis + "ms)"; + if (status >= 200 && status < 400) { + return ProbeOutcome.alive(detail); + } + if (status == 401 || status == 403 || status == 404) { + return ProbeOutcome.unverified(detail + ",未验证业务可用性"); + } + return ProbeOutcome.down(detail); + } + /** 探测结果。 */ - record ProbeOutcome(boolean ok, String detail) { + record ProbeOutcome(ProbeVerdict verdict, String detail) { + + static ProbeOutcome alive(String detail) { + return new ProbeOutcome(ProbeVerdict.ALIVE, detail); + } + + static ProbeOutcome unverified(String detail) { + return new ProbeOutcome(ProbeVerdict.UNVERIFIED, detail); + } + + static ProbeOutcome down(String detail) { + return new ProbeOutcome(ProbeVerdict.DOWN, detail); + } + + boolean ok() { + return verdict == ProbeVerdict.ALIVE; + } } } diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/notification/service/NotificationService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/notification/service/NotificationService.java index 6912291d6..fd2017db5 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/notification/service/NotificationService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/notification/service/NotificationService.java @@ -36,6 +36,9 @@ public class NotificationService { public static final String SCENE_SECRET_BALANCE = "secret_balance"; public static final String SCENE_SECRET_INVALID = "secret_invalid"; public static final String SCENE_TASK_FAILED = "task_failed"; + /** 任务本身成功、但有一部分行没跑出来(含其自动补测跟进)。大类与 task_failed 同为「任务异常」, + * scene 拆开是为了让「交付不完整」不再被按「任务失败」计数。 */ + public static final String SCENE_TASK_PARTIAL = "task_partial"; public static final String SCENE_SERVICE_DOWN = "service_down"; /** 麦象(18960 任务调度)异常:任务停滞/失败/队列积压。 */ public static final String SCENE_MAIXIANG_ANOMALY = "maixiang_anomaly"; @@ -58,6 +61,7 @@ public class NotificationService { SCENE_SERVICE_DOWN, CATEGORY_SYSTEM_ERROR, SCENE_MAIXIANG_ANOMALY, CATEGORY_SYSTEM_ERROR, SCENE_TASK_FAILED, CATEGORY_TASK_ERROR, + SCENE_TASK_PARTIAL, CATEGORY_TASK_ERROR, SCENE_SECRET_BALANCE, CATEGORY_CONFIG_ERROR, SCENE_SECRET_INVALID, CATEGORY_CONFIG_ERROR, SCENE_SYSTEM, CATEGORY_SYSTEM_NOTICE); diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/notification/client/MaixiangConsoleClientTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/notification/client/MaixiangConsoleClientTest.java index 244fad6fe..995128468 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/notification/client/MaixiangConsoleClientTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/notification/client/MaixiangConsoleClientTest.java @@ -3,6 +3,7 @@ package com.nanri.aiimage.modules.notification.client; import com.fasterxml.jackson.databind.ObjectMapper; import com.nanri.aiimage.config.NotificationProperties; import com.nanri.aiimage.modules.notification.client.MaixiangConsoleClient.BatchTaskPage; +import com.nanri.aiimage.modules.notification.client.MaixiangConsoleClient.ConsoleReadout; import com.nanri.aiimage.modules.notification.client.MaixiangConsoleClient.QueueStateItem; import com.nanri.aiimage.modules.notification.client.MaixiangConsoleClient.SingleTaskPage; import com.nanri.aiimage.modules.notification.client.MaixiangConsoleClient.WorkerSnapshot; @@ -142,6 +143,28 @@ class MaixiangConsoleClientTest { assertThat(client.parseEnvelope("not-json", "test")).isNull(); } + @Test + void classifyReadoutNamesEveryUnreadableShape() { + // 这四种形态原先都被折叠成「空数据」,扫描器看不出自己已经瞎了 + assertThat(client.classifyReadout(null, "Connection refused: connect").readable()).isFalse(); + assertThat(client.classifyReadout(null, "Connection refused: connect").reason()) + .contains("Connection refused"); + assertThat(client.classifyReadout("", null).readable()).isFalse(); + assertThat(client.classifyReadout("", null).reason()).contains("空"); + assertThat(client.classifyReadout("

502 Bad Gateway

", null).readable()).isFalse(); + assertThat(client.classifyReadout("

502 Bad Gateway

", null).reason()).contains("JSON"); + // 令牌失效 / 接口未部署 / 契约变更:HTTP 200 但 code!=1,mes 必须进告警文案 + ConsoleReadout rejected = client.classifyReadout("{\"code\":-1,\"mes\":\"令牌无效\",\"data\":null}", null); + assertThat(rejected.readable()).isFalse(); + assertThat(rejected.reason()).contains("令牌无效"); + } + + @Test + void classifyReadoutAcceptsSuccessEnvelope() { + assertThat(client.classifyReadout("{\"code\":1,\"mes\":\"成功\",\"data\":{\"queues\":[]}}", null).readable()) + .isTrue(); + } + @Test void parseToleratesNullUidAndBadTime() { BatchTaskPage page = client.parseBatchTaskPage(client.parseEnvelope( diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/notification/service/MaixiangAnomalyScannerTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/notification/service/MaixiangAnomalyScannerTest.java index 16cf003e5..83eac2276 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/notification/service/MaixiangAnomalyScannerTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/notification/service/MaixiangAnomalyScannerTest.java @@ -5,6 +5,7 @@ import com.nanri.aiimage.config.NotificationProperties; import com.nanri.aiimage.modules.notification.client.MaixiangConsoleClient; import com.nanri.aiimage.modules.notification.client.MaixiangConsoleClient.BatchTaskItem; import com.nanri.aiimage.modules.notification.client.MaixiangConsoleClient.BatchTaskPage; +import com.nanri.aiimage.modules.notification.client.MaixiangConsoleClient.ConsoleReadout; import com.nanri.aiimage.modules.notification.client.MaixiangConsoleClient.QueueStateItem; import com.nanri.aiimage.modules.notification.client.MaixiangConsoleClient.SingleTaskItem; import com.nanri.aiimage.modules.notification.client.MaixiangConsoleClient.SingleTaskPage; @@ -72,6 +73,48 @@ class MaixiangAnomalyScannerTest { verify(consoleClient, never()).queueStatus(); } + // ======================== 后台可读性 ======================== + + @Test + void scanAlertsOnceAndSkipsChecksWhenConsoleUnreadable() { + // 六项检查全靠后台接口取数:读不到时它们只会「查无异常」,整轮扫描静默失效 + when(consoleClient.configured()).thenReturn(true); + when(dispatch.prepareAdminAudience()).thenReturn(audience); + when(consoleClient.probeReadable()) + .thenReturn(ConsoleReadout.unreadable("接口返回失败:token 无效")); + + newScanner().scan(); + + ArgumentCaptor content = ArgumentCaptor.forClass(String.class); + ArgumentCaptor key = ArgumentCaptor.forClass(String.class); + verify(dispatch).pushToAdmins(eq(audience), eq(NotificationService.SCENE_MAIXIANG_ANOMALY), + eq(NotificationService.LEVEL_WARNING), eq("麦象异常:麦象后台数据不可读"), + content.capture(), key.capture(), isNull()); + assertThat(content.getValue()).contains("token 无效"); + // scan() 自己取当前时间(NOW 常量注入不进来),这里钉住「按自然日去重」的形状而不是某一天 + assertThat(key.getValue()).matches("maixiang_console_unreadable:\\d{8}"); + verify(consoleClient, never()).batchTasks(anyInt(), any(), anyInt()); + verify(consoleClient, never()).singleTasks(anyInt(), any(), anyInt()); + verify(consoleClient, never()).queueStatus(); + verify(consoleClient, never()).workers(); + } + + @Test + void scanRunsChecksWhenConsoleReadable() { + when(consoleClient.configured()).thenReturn(true); + when(dispatch.prepareAdminAudience()).thenReturn(audience); + when(consoleClient.probeReadable()).thenReturn(ConsoleReadout.OK); + when(consoleClient.workers()).thenReturn(WorkerSnapshot.EMPTY); + when(consoleClient.queueStatus()).thenReturn(List.of()); + when(consoleClient.batchTasks(anyInt(), any(), anyInt())).thenReturn(BatchTaskPage.EMPTY); + when(consoleClient.singleTasks(anyInt(), any(), anyInt())).thenReturn(SingleTaskPage.EMPTY); + + newScanner().scan(); + + verify(consoleClient, times(1)).queueStatus(); + verify(dispatch, never()).pushToAdmins(any(), anyString(), anyString(), anyString(), anyString(), anyString(), any()); + } + // ======================== 批量任务停滞 ======================== @Test @@ -373,6 +416,7 @@ class MaixiangAnomalyScannerTest { properties.setMaixiangConsoleToken("token"); when(consoleClient.configured()).thenReturn(true); when(dispatch.prepareAdminAudience()).thenReturn(audience); + when(consoleClient.probeReadable()).thenReturn(ConsoleReadout.OK); // 仅批量任务停滞检查(status=0 拉取)抛异常 doThrow(new RuntimeException("麦象接口超时")).when(consoleClient).batchTasks(eq(0), any(), anyInt()); doReturn(BatchTaskPage.EMPTY).when(consoleClient).batchTasks(eq(1), any(), anyInt()); diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/notification/service/NotificationDispatchServiceRecheckTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/notification/service/NotificationDispatchServiceRecheckTest.java index 2c37f1c91..a94a27117 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/notification/service/NotificationDispatchServiceRecheckTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/notification/service/NotificationDispatchServiceRecheckTest.java @@ -40,7 +40,7 @@ class NotificationDispatchServiceRecheckTest { void pushRecheckOutcomeNotifiesUserWithInfoLevelWhenAllResolved() { service.pushRecheckOutcome(961L, 29842L, "COLLECT_DATA", "COLLECT_DATA-29842", 3, 1, 0, 0); - verify(notificationService).pushOrRefresh(eq(961L), eq("user"), eq("task_failed"), + verify(notificationService).pushOrRefresh(eq(961L), eq("user"), eq("task_partial"), eq("info"), contains("补测完成"), contains("新增结果 3 行"), startsWith("collect_recheck:29842:")); } @@ -48,7 +48,7 @@ class NotificationDispatchServiceRecheckTest { void pushRecheckOutcomeUsesWarningWhileRowsStillPending() { service.pushRecheckOutcome(961L, 29842L, "COLLECT_DATA", "COLLECT_DATA-29842", 3, 0, 5, 0); - verify(notificationService).pushOrRefresh(eq(961L), eq("user"), eq("task_failed"), + verify(notificationService).pushOrRefresh(eq(961L), eq("user"), eq("task_partial"), eq("warning"), contains("补测完成"), contains("仍有 5 行"), anyString()); } @@ -66,7 +66,7 @@ class NotificationDispatchServiceRecheckTest { void pushRecheckOutcomeAdvisesManualRerunWhenAllExhausted() { service.pushRecheckOutcome(961L, 29842L, "COLLECT_DATA", "COLLECT_DATA-29842", 0, 0, 0, 4); - verify(notificationService).pushOrRefresh(eq(961L), eq("user"), eq("task_failed"), + verify(notificationService).pushOrRefresh(eq(961L), eq("user"), eq("task_partial"), eq("warning"), contains("未能完成"), contains("共 4 行"), startsWith("collect_recheck:29842:")); } diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/notification/service/NotificationDispatchServiceTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/notification/service/NotificationDispatchServiceTest.java index fa77f8f52..ef85cc850 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/notification/service/NotificationDispatchServiceTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/notification/service/NotificationDispatchServiceTest.java @@ -12,6 +12,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.contains; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -159,6 +160,29 @@ class NotificationDispatchServiceTest { assertThat(pushed).isEqualTo(3); } + /** + * 「部分结果缺失」必须落在自己的 scene:它和终态失败共用 task_failed 时,巡检按 + * task_failed 独立桶数报「窗口内 N 条独立失败」会把「任务成功、少跑几行」也算成任务失败 + * (2026-10-05 值班:窗口 22 行里 20 行是这种)。去重键与大类都不变,只拆 scene。 + */ + @Test + void pushPartialResultUsesTaskPartialScene() { + AdminUserEntity superAdmin = admin(1L, "超管甲"); + when(adminUserMapper.selectList(any())).thenReturn(List.of(superAdmin)); + when(adminAuthSupport.currentRole(superAdmin)).thenReturn("super_admin"); + when(notificationService.pushOrRefresh(anyLong(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString())) + .thenReturn(true); + + service.pushPartialResult(20L, 3301L, "BRAND", "BRAND-3301", "12 个品牌查询失败"); + + verify(notificationService).pushOrRefresh(eq(20L), eq(NotificationService.AUDIENCE_USER), + eq(NotificationService.SCENE_TASK_PARTIAL), eq(NotificationService.LEVEL_WARNING), + contains("任务部分结果缺失"), contains("12 个品牌查询失败"), eq("task_partial:3301")); + verify(notificationService).pushOrRefresh(eq(1L), eq(NotificationService.AUDIENCE_ADMIN), + eq(NotificationService.SCENE_TASK_PARTIAL), eq(NotificationService.LEVEL_WARNING), + contains("用户"), anyString(), eq("task_partial_admin:3301:1")); + } + @Test void pushToUserUsesUserAudience() { when(notificationService.pushOrRefresh(anyLong(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString())) diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/notification/service/NotificationScanSchedulerTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/notification/service/NotificationScanSchedulerTest.java index cb95babf7..cbc72c0d3 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/notification/service/NotificationScanSchedulerTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/notification/service/NotificationScanSchedulerTest.java @@ -24,6 +24,7 @@ import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -184,6 +185,100 @@ class NotificationScanSchedulerTest { assertThat(keyBase.getValue()).matches("service_down:brand-service:\\d{8}"); } + /** 状态码三态口径:4xx 里只有 401/403/404 是「探活不可信」,其余按故障算。 */ + @Test + void httpStatusClassifiesIntoThreeProbeVerdicts() { + assertThat(NotificationScanScheduler.probeOutcomeFor(200, 5).verdict()) + .isEqualTo(NotificationScanScheduler.ProbeVerdict.ALIVE); + assertThat(NotificationScanScheduler.probeOutcomeFor(302, 5).verdict()) + .isEqualTo(NotificationScanScheduler.ProbeVerdict.ALIVE); + + for (int status : new int[]{401, 403, 404}) { + NotificationScanScheduler.ProbeOutcome outcome = + NotificationScanScheduler.probeOutcomeFor(status, 5); + assertThat(outcome.verdict()) + .as("HTTP %d 不能再被当成「服务存活」:路径没人接/鉴权失败都证不了业务可用", status) + .isEqualTo(NotificationScanScheduler.ProbeVerdict.UNVERIFIED); + assertThat(outcome.detail()).contains("HTTP " + status).contains("未验证"); + assertThat(outcome.ok()).isFalse(); + } + + for (int status : new int[]{400, 405, 429, 500, 502, 503}) { + assertThat(NotificationScanScheduler.probeOutcomeFor(status, 5).verdict()) + .as("HTTP %d 是故障", status) + .isEqualTo(NotificationScanScheduler.ProbeVerdict.DOWN); + } + } + + /** 真发一次 HTTP:404 判「探活不可信」,并且不重试(确定性结果打两次只多一份日志)。 */ + @Test + void probeAgainstHttp404IsUnverifiedAndNotRetried() throws Exception { + java.util.concurrent.atomic.AtomicInteger hits = new java.util.concurrent.atomic.AtomicInteger(); + com.sun.net.httpserver.HttpServer server = startFixedStatusServer(404, hits); + NotificationScanScheduler.ProbeOutcome outcome; + try { + outcome = newScheduler().probeHttpWithRetry( + "http://127.0.0.1:" + server.getAddress().getPort() + "/api/version"); + } finally { + server.stop(0); + } + + assertThat(outcome.verdict()).isEqualTo(NotificationScanScheduler.ProbeVerdict.UNVERIFIED); + assertThat(outcome.detail()).contains("未验证"); + assertThat(hits.get()).isEqualTo(1); + } + + /** 反向对照:真故障仍然「失败立即重试一次,两次都失败才算故障」,别把瞬时抖动报出去。 */ + @Test + void probeOnHttp503RetriesOnceThenDeclaresOutage() throws Exception { + java.util.concurrent.atomic.AtomicInteger hits = new java.util.concurrent.atomic.AtomicInteger(); + com.sun.net.httpserver.HttpServer server = startFixedStatusServer(503, hits); + NotificationScanScheduler.ProbeOutcome outcome; + try { + outcome = newScheduler().probeHttpWithRetry( + "http://127.0.0.1:" + server.getAddress().getPort() + "/api/version"); + } finally { + server.stop(0); + } + + assertThat(outcome.verdict()).isEqualTo(NotificationScanScheduler.ProbeVerdict.DOWN); + assertThat(hits.get()).isEqualTo(2); + } + + /** 404 不推「服务不可用」通知——三态改造不许顺手把它升级成误报(现网最大一类误报源)。 */ + @Test + void probeOnHttp404DoesNotPushServiceDown() throws Exception { + com.sun.net.httpserver.HttpServer server = startFixedStatusServer(404, + new java.util.concurrent.atomic.AtomicInteger()); + try { + properties.setBrandServiceUrl("http://127.0.0.1:" + server.getAddress().getPort()); + properties.setPriceTrackApiUrl(""); + properties.setJikipProbeEnabled(false); + + newScheduler().probeServices(); + } finally { + server.stop(0); + } + + verify(dispatch, never()).pushToAdmins(anyString(), anyString(), anyString(), anyString(), anyString(), any()); + } + + private com.sun.net.httpserver.HttpServer startFixedStatusServer( + int status, java.util.concurrent.atomic.AtomicInteger hits) throws java.io.IOException { + com.sun.net.httpserver.HttpServer server = + com.sun.net.httpserver.HttpServer.create(new java.net.InetSocketAddress("127.0.0.1", 0), 0); + server.createContext("/", exchange -> { + hits.incrementAndGet(); + byte[] body = "probe".getBytes(java.nio.charset.StandardCharsets.UTF_8); + exchange.sendResponseHeaders(status, body.length); + exchange.getResponseBody().write(body); + exchange.close(); + }); + server.setExecutor(null); + server.start(); + return server; + } + private void stubAdminAudience() { when(dispatch.prepareAdminAudience()).thenReturn( new NotificationDispatchService.AdminAudience(List.of(), Set.of(), Map.of())); diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/notification/service/NotificationServiceTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/notification/service/NotificationServiceTest.java index 2272a5da3..b60968f46 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/notification/service/NotificationServiceTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/notification/service/NotificationServiceTest.java @@ -260,6 +260,22 @@ class NotificationServiceTest { .isEqualTo(NotificationService.CATEGORY_SYSTEM_NOTICE); } + /** + * task_partial(交付不完整)与 task_failed(终态失败)是两个 scene,但用户可见大类都必须是 + * 「任务异常」——拆开 scene 是为了让巡检/看板不再把「少跑了几行」数成「任务失败」, + * 不是为了改铃铛的分类。漏登记会兜底进「系统通知」,用户按「任务异常」筛就凭空少一批。 + */ + @Test + void taskPartialAndTaskFailedAreSeparateScenesInSameCategory() { + assertThat(NotificationService.SCENE_TASK_PARTIAL).isNotEqualTo(NotificationService.SCENE_TASK_FAILED); + assertThat(NotificationService.categoryOf(NotificationService.SCENE_TASK_PARTIAL)) + .isEqualTo(NotificationService.CATEGORY_TASK_ERROR); + assertThat(NotificationService.categoryOf(NotificationService.SCENE_TASK_FAILED)) + .isEqualTo(NotificationService.CATEGORY_TASK_ERROR); + assertThat(NotificationService.scenesOfCategory(NotificationService.CATEGORY_TASK_ERROR)) + .contains(NotificationService.SCENE_TASK_FAILED, NotificationService.SCENE_TASK_PARTIAL); + } + /** 类型筛选落到 SQL 上是 scene IN (...);空/未知分类不追加条件,避免前端传错值就查不到数据。 */ @Test @SuppressWarnings("unchecked") diff --git a/frontend-vue/src/pages/brand/components/BrandDeleteBrandTab.vue b/frontend-vue/src/pages/brand/components/BrandDeleteBrandTab.vue index 22684f47c..ead5b5eda 100644 --- a/frontend-vue/src/pages/brand/components/BrandDeleteBrandTab.vue +++ b/frontend-vue/src/pages/brand/components/BrandDeleteBrandTab.vue @@ -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 } } } diff --git a/frontend-vue/src/pages/brand/components/BrandPriceTrackTab.vue b/frontend-vue/src/pages/brand/components/BrandPriceTrackTab.vue index 9cf7517b5..f1f90b98b 100644 --- a/frontend-vue/src/pages/brand/components/BrandPriceTrackTab.vue +++ b/frontend-vue/src/pages/brand/components/BrandPriceTrackTab.vue @@ -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>> - 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>> + 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) { diff --git a/frontend-vue/src/shared/api/types/modules/notification.ts b/frontend-vue/src/shared/api/types/modules/notification.ts index 89b8e9a81..075c9fd1a 100644 --- a/frontend-vue/src/shared/api/types/modules/notification.ts +++ b/frontend-vue/src/shared/api/types/modules/notification.ts @@ -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' diff --git a/frontend-vue/src/shared/bridges/pywebview.ts b/frontend-vue/src/shared/bridges/pywebview.ts index 669e2a87c..7f63290e2 100644 --- a/frontend-vue/src/shared/bridges/pywebview.ts +++ b/frontend-vue/src/shared/bridges/pywebview.ts @@ -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; vc_start_collect?: (params: { diff --git a/frontend-vue/src/shared/dispatch-guard.ts b/frontend-vue/src/shared/dispatch-guard.ts index eb7a1f8bf..249a928d3 100644 --- a/frontend-vue/src/shared/dispatch-guard.ts +++ b/frontend-vue/src/shared/dispatch-guard.ts @@ -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 +} diff --git a/frontend-vue/tests/dispatch-guard.test.ts b/frontend-vue/tests/dispatch-guard.test.ts index c4f14c2f4..bb386c357 100644 --- a/frontend-vue/tests/dispatch-guard.test.ts +++ b/frontend-vue/tests/dispatch-guard.test.ts @@ -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) +}) diff --git a/frontend-vue/tests/enqueue-rejection-wiring.test.ts b/frontend-vue/tests/enqueue-rejection-wiring.test.ts new file mode 100644 index 000000000..1ecaf2f7e --- /dev/null +++ b/frontend-vue/tests/enqueue-rejection-wiring.test.ts @@ -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 导入`) + } +})