feat(任务判死): 心跳正常但 180 分钟无结果上报的二次判死线(13 模块)+ 同期待发改动

判死线(治 28131 型「主线程卡死、心跳线程照发」):
- 判据改看 biz_task_scope_state.last_chunk_at(HTTP 心跳不刷新它);从未上报跳过不判
- 中央线覆盖 DELETE_BRAND/PRODUCT_RISK_RESOLVE/PRICE_TRACK/SHOP_MATCH/PATROL_DELETE/QUERY_ASIN/WITHDRAW
- 自带线接入 COLLECT_DATA/SIMILAR_ASIN/APPEARANCE_PATENT/SHOP_DATA_CRAWL/PUBLISH/BRAND
- 客户端心跳带处理位置 progressText,判死文案含最后位置;no-result-upload-timeout-minutes 默认 180(0 关闭)

同期带上另一工作流的待发改动:跟价换 IP 重试、品牌检测重试上限与 LLM 并发下调、
教程包后台管理页与 V126 迁移、admin-vue 教程记录页。
This commit is contained in:
2026-09-14 17:56:20 +08:00
parent b2b9983b07
commit 792b5f8bce
36 changed files with 1538 additions and 57 deletions
@@ -0,0 +1,114 @@
package com.nanri.aiimage.modules.task.service;
import com.nanri.aiimage.modules.task.model.dto.TaskHeartbeatRequest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.time.Duration;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* 心跳「当前处理位置」存储:写读往返、空位置不写、Redis 异常只降级不抛出。
* 位置仅用于判死文案与排查,任何异常都不得影响心跳本身。
*/
@ExtendWith(MockitoExtension.class)
class TaskHeartbeatPositionServiceTest {
@Mock private StringRedisTemplate stringRedisTemplate;
@Mock private HashOperations<String, Object, Object> hashOperations;
private TaskHeartbeatPositionService service() {
return new TaskHeartbeatPositionService(stringRedisTemplate);
}
@Test
void recordWritesHashWithProgressText() {
when(stringRedisTemplate.opsForHash()).thenReturn(hashOperations);
TaskHeartbeatRequest request = new TaskHeartbeatRequest();
request.setPhase("crawling");
request.setCurrent(2);
request.setTotal(10);
request.setProgressText("店铺 魏振峰(2/10)");
service().record(123L, request);
@SuppressWarnings("unchecked")
ArgumentCaptor<Map<String, String>> values = ArgumentCaptor.forClass(Map.class);
verify(hashOperations).putAll(eq("task:heartbeat:position:123"), values.capture());
assertEquals("店铺 魏振峰(2/10)", values.getValue().get("progressText"));
assertEquals("crawling", values.getValue().get("phase"));
assertEquals("2", values.getValue().get("current"));
assertEquals("10", values.getValue().get("total"));
verify(stringRedisTemplate).expire(eq("task:heartbeat:position:123"), any(Duration.class));
}
@Test
void recordSkipsWhenNoPosition() {
TaskHeartbeatRequest request = new TaskHeartbeatRequest();
service().record(123L, request);
verify(stringRedisTemplate, never()).opsForHash();
}
@Test
void recordSwallowsRedisError() {
when(stringRedisTemplate.opsForHash()).thenReturn(hashOperations);
doThrow(new RuntimeException("redis down")).when(hashOperations).putAll(any(), any());
TaskHeartbeatRequest request = new TaskHeartbeatRequest();
request.setProgressText("店铺 A(1/2)");
service().record(123L, request);
verify(stringRedisTemplate, never()).expire(any(), any(Duration.class));
}
@Test
void describePrefersProgressText() {
when(stringRedisTemplate.opsForHash()).thenReturn(hashOperations);
when(hashOperations.entries("task:heartbeat:position:123"))
.thenReturn(Map.of("progressText", "店铺 魏振峰(2/10)", "current", "2", "total", "10"));
assertEquals("店铺 魏振峰(2/10)", service().describe(123L));
}
@Test
void describeBuildsFromPhaseAndCountersWhenNoText() {
when(stringRedisTemplate.opsForHash()).thenReturn(hashOperations);
when(hashOperations.entries("task:heartbeat:position:123"))
.thenReturn(Map.of("phase", "crawling", "current", "2", "total", "10"));
assertEquals("阶段=crawling,2/10", service().describe(123L));
}
@Test
void describeReturnsNullWhenMissing() {
when(stringRedisTemplate.opsForHash()).thenReturn(hashOperations);
when(hashOperations.entries("task:heartbeat:position:123")).thenReturn(Map.of());
assertNull(service().describe(123L));
}
@Test
void describeReturnsNullOnRedisError() {
when(stringRedisTemplate.opsForHash()).thenReturn(hashOperations);
when(hashOperations.entries("task:heartbeat:position:123"))
.thenThrow(new RuntimeException("redis down"));
assertNull(service().describe(123L));
}
}