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 d68f30e4e..7dc37d27b 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 @@ -54,6 +54,14 @@ public class NotificationScanScheduler { private static final int REASON_MAX_LENGTH = 120; private static final int PROBE_READ_TIMEOUT_MILLIS = 8_000; + /** + * 18960 的探针路径。根路径没有路由,恒 404 ⇒ 这一格只能读成「未验证」, + * 现网每轮只打一条 WARN,服务真死了也不响。/healthz 是 18960 专为探针加的一条: + * 不写心跳、不碰 Redis/MySQL——换成 /batch/status 会让服务用自己的健康检查 + * 刷新采集机心跳,正是 2026-10-04 变体采集停摆 12 小时没被发现的形状。 + */ + static final String PRICE_TRACK_PROBE_PATH = "/healthz"; + private static final DateTimeFormatter DAY_FORMAT = DateTimeFormatter.ofPattern("yyyyMMdd"); /** @@ -190,7 +198,7 @@ public class NotificationScanScheduler { } if (hasText(properties.getPriceTrackApiUrl())) { reportProbe("price-track-api", "跟价任务 API", - probeHttpWithRetry(joinUrl(properties.getPriceTrackApiUrl(), "/"))); + probeHttpWithRetry(priceTrackProbeUrl())); } else { log.info("[notification-scan] 跟价任务 API 探测地址未配置,跳过"); } @@ -335,6 +343,11 @@ public class NotificationScanScheduler { return base + path; } + /** 组装跟价 API 的探针地址;包内可见,判据要在不联网的情况下钉住路径。 */ + String priceTrackProbeUrl() { + return joinUrl(properties.getPriceTrackApiUrl(), PRICE_TRACK_PROBE_PATH); + } + private String rootCauseMessage(Throwable throwable) { Throwable current = throwable; while (current.getCause() != null && current.getCause() != current) { 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 cbc72c0d3..2de71fc69 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 @@ -263,6 +263,75 @@ class NotificationScanSchedulerTest { verify(dispatch, never()).pushToAdmins(anyString(), anyString(), anyString(), anyString(), anyString(), any()); } + /** + * 跟价 API 的探针必须打在 /healthz 上。反例一就是本笔之前的 "/":18960 根路径没有路由, + * 恒 404 ⇒ 那一格结构上永远只能读成「未验证」,服务真死了也不响。 + * 反例二是 /batch/status?task_id=watchdog:它会 beat("vc"),等于让服务用自己的健康检查 + * 刷新采集机心跳(2026-10-04 变体采集停摆 12 小时未被发现就是这个形状)。 + */ + @Test + void priceTrackProbeTargetsHealthzAndNeverTheHeartbeatEndpoint() { + properties.setPriceTrackApiUrl("http://192.168.0.170:18960"); + assertThat(newScheduler().priceTrackProbeUrl()) + .isEqualTo("http://192.168.0.170:18960/healthz"); + + properties.setPriceTrackApiUrl("http://127.0.0.1:18960/"); + assertThat(newScheduler().priceTrackProbeUrl()) + .as("登记好的地址带尾斜杠,也不能拼出 //healthz") + .isEqualTo("http://127.0.0.1:18960/healthz"); + + properties.setPriceTrackApiUrl("http://127.0.0.1:18960"); + assertThat(newScheduler().priceTrackProbeUrl()) + .doesNotEndWith(":18960") + .doesNotContain("batch/status"); + } + + /** 端到端一格:真打出去的请求路径必须就是 /healthz,只看拼好的字符串不算验过。 */ + @Test + void healthzAnswersAliveAndProbeServicesActuallyRequestsThatPath() throws Exception { + java.util.List requested = + java.util.Collections.synchronizedList(new java.util.ArrayList<>()); + com.sun.net.httpserver.HttpServer server = com.sun.net.httpserver.HttpServer.create( + new java.net.InetSocketAddress("127.0.0.1", 0), 0); + server.createContext("/healthz", exchange -> { + requested.add(exchange.getRequestURI().getPath()); + byte[] body = "{\"ok\":true}".getBytes(java.nio.charset.StandardCharsets.UTF_8); + exchange.sendResponseHeaders(200, body.length); + exchange.getResponseBody().write(body); + exchange.close(); + }); + server.createContext("/", exchange -> { + requested.add(exchange.getRequestURI().getPath()); + byte[] body = "no route".getBytes(java.nio.charset.StandardCharsets.UTF_8); + exchange.sendResponseHeaders(404, body.length); + exchange.getResponseBody().write(body); + exchange.close(); + }); + server.setExecutor(null); + server.start(); + try { + String base = "http://127.0.0.1:" + server.getAddress().getPort(); + properties.setPriceTrackApiUrl(base); + properties.setBrandServiceUrl(""); + properties.setJikipProbeEnabled(false); + NotificationScanScheduler scheduler = newScheduler(); + + assertThat(scheduler.probeHttpWithRetry(scheduler.priceTrackProbeUrl()).verdict()) + .isEqualTo(NotificationScanScheduler.ProbeVerdict.ALIVE); + assertThat(scheduler.probeHttpWithRetry(base + "/").verdict()) + .as("本笔之前打的就是根路径,它只能判「未验证」") + .isEqualTo(NotificationScanScheduler.ProbeVerdict.UNVERIFIED); + + requested.clear(); + scheduler.probeServices(); + assertThat(requested) + .as("探针实际请求的那一条路径——调用点被改回根路径时这一格必须红") + .containsExactly("/healthz"); + } finally { + server.stop(0); + } + } + 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 =