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:
2026-10-06 11:46:54 +08:00
parent 4596e7d840
commit 7887e7ee37
18 changed files with 545 additions and 47 deletions
@@ -44,6 +44,37 @@ public class MaixiangConsoleClient {
return hasText(properties.getPriceTrackApiUrl()) && hasText(properties.getMaixiangConsoleToken());
}
/**
* 后台数据可读性探测:真拉一次队列状态接口(最轻的一个),把「读不到」与「读到但没异常」分开。
*
* <p>异常扫描的六项检查全靠后台接口取数,读不通时它们只会一致地报「一切正常」;而 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,
@@ -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 任务调度)异常扫描:把「采集端出了事但没人知道」的情况推给管理员。
*
* <p>六类检查(全部只读,通过 {@link MaixiangConsoleClient} 的后台接口取数):
* <p>一道「后台可读性」闸门 + 六类检查(全部只读,通过 {@link MaixiangConsoleClient} 的后台接口取数):
* <ol>
* <li>采集机掉线:采集端打点的机器级静默超龄 —— 唯一能直接反映「整机停摆」的信号;</li>
* <li>批量任务停滞:status=0/1 且超过阈值无更新(如消费端 worker 掉线中断采集);</li>
@@ -36,6 +37,9 @@ import java.util.stream.Collectors;
* <li>队列积压:task:queue 堆积且无人消费 / task:processing 超过阈值。</li>
* </ol>
*
* <p>「后台可读性」闸门为什么要排在最前:六项检查依赖同一批接口,接口整体读不通时它们的一致结果是
* 「查无异常」而不是「扫不出」;HTTP 200 + code!=1(令牌失效、接口未部署、契约变更)连服务探测都是绿的。
*
* <p>「采集端静默」为什么要单列:其余五类都靠「任务 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));
@@ -109,9 +109,12 @@ public class NotificationDispatchService {
* 「任务成功,但有一部分行没跑出来」通知:整任务没失败、铃铛里的 task_failed 只覆盖终态失败,
* 这类部分缺失过去对用户完全不可见(品牌检测的查询失败行只躺在结果文件的一张子表里)。
*
* <p>场景沿用 {@code task_failed}(大类仍是「任务异常」),去重键按任务维度:同一任务重复
* 组装结果时刷新内容而不是刷屏。不写 task.errorMessage —— 结果组装的失败判据读那个字段,
* 写了会把任务翻成 FAILED,与「状态保持成功、只提示交付不完整」的产品口径相悖。
* <p>场景用独立的 {@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 {
*
* <p>只推用户不推管理员:这是部分缺失(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));
}
@@ -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 &lt; 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;
}
}
}
@@ -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);