feat(task-retry): 失败任务克隆重试,最多 3 代并跳过欠费/宕机
- 废弃只复制 request_json 的空壳续跑:三类任务的解析输入都在模块侧状态里, 线上历史 19 条续跑因此全部失败;改为由 ClientTaskRetrySpi 克隆任务头 + 采集明细 / 解析 scope state / 源结果行,新任务直接可执行。 - 不可抗力不重试:LLM 密钥或代理 IP 欠费、服务宕机、账号自动登录失败。 - 新增内部回填端点 POST /api/internal/tasks/retry-failed(内部令牌, dryRun 默认 true),用于补扫 30 分钟窗口之外的历史失败。
This commit is contained in:
@@ -66,7 +66,9 @@ class MigratedSqlEquivalenceTest {
|
||||
java.util.Set.of("AdminUserMapper#countLiveWorkFor",
|
||||
// V157 加 token 两列时新写的累加语句;注解时代没有原文可对撞,等价性这支只能跳过它。
|
||||
// 与 increment 分两条是口径要求(计次先于响应、token 只在响应后有读数),不是随手加的第二条 SQL。
|
||||
"UserSecretUsageMapper#addTokens");
|
||||
"UserSecretUsageMapper#addTokens",
|
||||
// 失败任务重试:INSERT ... SELECT 复制采集明细行,非注解迁移件。
|
||||
"CollectDataItemMapper#copyItemsForRetry");
|
||||
|
||||
/** 账本 5 列:接口 / 方法 / @Select 种类 / 是否含常量插值 / 注解原文(Java 反转义后那条串)。 */
|
||||
private static final int LEDGER_COLUMNS = 5;
|
||||
|
||||
@@ -70,7 +70,9 @@ class SqlLayeringConventionTest {
|
||||
// 它同样是「新写」而非「从注解搬进 XML」,所以两处一起改:本格的总数 +
|
||||
// MigratedSqlEquivalenceTest.NOT_MIGRATED 的点名。与 increment 分两条是口径要求——
|
||||
// 计次发生在发请求之前(含重试),token 只有响应回来才有读数,合成一条 upsert 就会漏计超时的那次。
|
||||
private static final int XML_STATEMENT_TOTAL = 98;
|
||||
// 98 → 99:失败任务重试新增 {@code CollectDataItemMapper#copyItemsForRetry}
|
||||
// (INSERT ... SELECT 复制明细行),同样登记进 NOT_MIGRATED。
|
||||
private static final int XML_STATEMENT_TOTAL = 99;
|
||||
|
||||
/**
|
||||
* wrapper 里夹带函数级 SQL 的现状("这不是简单单表"的证据),文件名 → 条数:
|
||||
|
||||
+124
-59
@@ -5,15 +5,13 @@ import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.core.read.ListAppender;
|
||||
import com.nanri.aiimage.modules.task.mapper.FileTaskMapper;
|
||||
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
|
||||
import com.nanri.aiimage.modules.task.spi.ClientTaskPullSpi;
|
||||
import com.nanri.aiimage.modules.task.spi.ClientTaskRetrySpi;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
@@ -26,28 +24,25 @@ import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* 客户端中断任务的自动续跑:保留失败记录 + 重新排队 PENDING 续跑任务。
|
||||
*
|
||||
* <p>背景:2026-09-18 任务 28587 因客户端被更新脚本重启而中断后一直挂 RUNNING;
|
||||
* 中断上报把任务标 FAILED 之后,还需要有人把长任务放回队列,否则一遇客户端更新就整个白跑。
|
||||
* 失败任务的自动重试:克隆重试任务、代数封顶、不可抗力跳过。
|
||||
*/
|
||||
class TaskResumeServiceTest {
|
||||
|
||||
private static ClientTaskPullSpi spi(String moduleType) {
|
||||
return new ClientTaskPullSpi() {
|
||||
private static ClientTaskRetrySpi retrySpi(String moduleType, Long childTaskId) {
|
||||
return new ClientTaskRetrySpi() {
|
||||
@Override
|
||||
public String moduleType() {
|
||||
return moduleType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> buildQueuePayload(FileTaskEntity task) {
|
||||
return Map.of("type", "x-run", "data", Map.of("taskId", task.getId()));
|
||||
public Long cloneForRetry(FileTaskEntity original) {
|
||||
return childTaskId;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static TaskResumeService service(FileTaskMapper mapper, List<ClientTaskPullSpi> handlers,
|
||||
private static TaskResumeService service(FileTaskMapper mapper, List<ClientTaskRetrySpi> handlers,
|
||||
boolean enabled, int maxAttempt) {
|
||||
TaskResumeService service = new TaskResumeService(mapper, handlers);
|
||||
ReflectionTestUtils.setField(service, "enabled", enabled);
|
||||
@@ -57,14 +52,14 @@ class TaskResumeServiceTest {
|
||||
return service;
|
||||
}
|
||||
|
||||
private static FileTaskEntity interruptedTask(Long id, String moduleType) {
|
||||
private static FileTaskEntity failedTask(Long id, String moduleType, String errorMessage) {
|
||||
FileTaskEntity task = new FileTaskEntity();
|
||||
task.setId(id);
|
||||
task.setModuleType(moduleType);
|
||||
task.setUserId(977L);
|
||||
task.setTaskMode("PYTHON_QUEUE");
|
||||
task.setStatus("FAILED");
|
||||
task.setErrorMessage("客户端异常中断: 客户端重启恢复上报(上次任务类型: similar-asin-run)");
|
||||
task.setErrorMessage(errorMessage);
|
||||
task.setRequestJson("{\"items\":[]}");
|
||||
task.setResumeAttempt(0);
|
||||
task.setFinishedAt(LocalDateTime.now());
|
||||
@@ -72,46 +67,82 @@ class TaskResumeServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void 中断任务会被重新排队为待执行的续跑任务() {
|
||||
void 失败任务会被克隆重试() {
|
||||
FileTaskMapper mapper = mock(FileTaskMapper.class);
|
||||
when(mapper.selectList(any())).thenReturn(List.of(interruptedTask(28587L, "SIMILAR_ASIN")));
|
||||
when(mapper.selectList(any())).thenReturn(List.of(
|
||||
failedTask(28587L, "SIMILAR_ASIN", "任务心跳超时,已自动失败")));
|
||||
when(mapper.selectCount(any())).thenReturn(0L);
|
||||
TaskResumeService service = service(mapper, List.of(spi("SIMILAR_ASIN")), true, 3);
|
||||
ClientTaskRetrySpi spi = mock(ClientTaskRetrySpi.class);
|
||||
when(spi.moduleType()).thenReturn("SIMILAR_ASIN");
|
||||
when(spi.cloneForRetry(any())).thenReturn(9001L);
|
||||
TaskResumeService service = service(mapper, List.of(spi), true, 3);
|
||||
|
||||
TaskResumeService.ResumeStats stats = service.resumeInterruptedTasks();
|
||||
|
||||
assertEquals(1, stats.scannedTaskCount);
|
||||
assertEquals(1, stats.resumedTaskCount);
|
||||
ArgumentCaptor<FileTaskEntity> captor = ArgumentCaptor.forClass(FileTaskEntity.class);
|
||||
verify(mapper).insert(captor.capture());
|
||||
FileTaskEntity resumed = captor.getValue();
|
||||
assertEquals("PENDING", resumed.getStatus(), "续跑任务必须是待领取状态,交给客户端兜底拉取");
|
||||
assertEquals(28587L, resumed.getResumeOfTaskId(), "必须回指原任务,用于追溯与幂等");
|
||||
assertEquals(1, resumed.getResumeAttempt());
|
||||
assertEquals(977L, resumed.getUserId());
|
||||
assertEquals("SIMILAR_ASIN", resumed.getModuleType());
|
||||
assertEquals("{\"items\":[]}", resumed.getRequestJson(), "原任务参数必须整体带走");
|
||||
assertTrue(resumed.getTaskNo().startsWith("SIMILAR_ASIN-"), "任务号沿用模块前缀");
|
||||
assertEquals(List.of(9001L), stats.retriedTaskIds);
|
||||
verify(spi).cloneForRetry(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 已有续跑任务时跳过避免重复排队() {
|
||||
void 欠费或服务宕机等不可抗力不重试() {
|
||||
FileTaskMapper mapper = mock(FileTaskMapper.class);
|
||||
when(mapper.selectList(any())).thenReturn(List.of(interruptedTask(28587L, "SIMILAR_ASIN")));
|
||||
when(mapper.selectCount(any())).thenReturn(1L, 1L);
|
||||
TaskResumeService service = service(mapper, List.of(spi("SIMILAR_ASIN")), true, 3);
|
||||
when(mapper.selectList(any())).thenReturn(List.of(
|
||||
failedTask(28587L, "SIMILAR_ASIN", "代理提取失败:余额不足"),
|
||||
failedTask(28588L, "APPEARANCE_PATENT", "上游服务不可用,请稍后再试")));
|
||||
when(mapper.selectCount(any())).thenReturn(0L);
|
||||
ClientTaskRetrySpi spi = mock(ClientTaskRetrySpi.class);
|
||||
when(spi.moduleType()).thenReturn("SIMILAR_ASIN");
|
||||
TaskResumeService service = service(mapper, List.of(spi), true, 3);
|
||||
|
||||
TaskResumeService.ResumeStats stats = service.resumeInterruptedTasks();
|
||||
|
||||
assertEquals(2, stats.scannedTaskCount);
|
||||
assertEquals(2, stats.forceMajeureSkippedCount);
|
||||
assertEquals(0, stats.resumedTaskCount);
|
||||
verify(spi, never()).cloneForRetry(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 已有重试任务时跳过避免重复克隆() {
|
||||
FileTaskMapper mapper = mock(FileTaskMapper.class);
|
||||
when(mapper.selectList(any())).thenReturn(List.of(
|
||||
failedTask(28587L, "SIMILAR_ASIN", "任务心跳超时,已自动失败")));
|
||||
// 第一次是"不支持重试的中断任务数",第二次是"是否已有重试子任务"
|
||||
when(mapper.selectCount(any())).thenReturn(0L, 1L);
|
||||
ClientTaskRetrySpi spi = mock(ClientTaskRetrySpi.class);
|
||||
when(spi.moduleType()).thenReturn("SIMILAR_ASIN");
|
||||
TaskResumeService service = service(mapper, List.of(spi), true, 3);
|
||||
|
||||
TaskResumeService.ResumeStats stats = service.resumeInterruptedTasks();
|
||||
|
||||
assertEquals(1, stats.skippedTaskCount);
|
||||
assertEquals(0, stats.resumedTaskCount);
|
||||
verify(mapper, never()).insert(any(FileTaskEntity.class));
|
||||
verify(spi, never()).cloneForRetry(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 重试代数到上限后跳过() {
|
||||
FileTaskMapper mapper = mock(FileTaskMapper.class);
|
||||
FileTaskEntity task = failedTask(28587L, "SIMILAR_ASIN", "任务心跳超时,已自动失败");
|
||||
task.setResumeAttempt(3);
|
||||
when(mapper.selectList(any())).thenReturn(List.of(task));
|
||||
when(mapper.selectCount(any())).thenReturn(0L);
|
||||
ClientTaskRetrySpi spi = mock(ClientTaskRetrySpi.class);
|
||||
when(spi.moduleType()).thenReturn("SIMILAR_ASIN");
|
||||
TaskResumeService service = service(mapper, List.of(spi), true, 3);
|
||||
|
||||
TaskResumeService.ResumeStats stats = service.resumeInterruptedTasks();
|
||||
|
||||
assertEquals(1, stats.skippedTaskCount);
|
||||
verify(spi, never()).cloneForRetry(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 开关关闭时不扫描() {
|
||||
FileTaskMapper mapper = mock(FileTaskMapper.class);
|
||||
TaskResumeService service = service(mapper, List.of(spi("SIMILAR_ASIN")), false, 3);
|
||||
TaskResumeService service = service(mapper, List.of(retrySpi("SIMILAR_ASIN", 1L)), false, 3);
|
||||
|
||||
TaskResumeService.ResumeStats stats = service.resumeInterruptedTasks();
|
||||
|
||||
@@ -120,7 +151,7 @@ class TaskResumeServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void 没有注册模块兜底实现时不扫描() {
|
||||
void 没有注册克隆实现时不扫描() {
|
||||
FileTaskMapper mapper = mock(FileTaskMapper.class);
|
||||
TaskResumeService service = service(mapper, List.of(), true, 3);
|
||||
|
||||
@@ -131,58 +162,57 @@ class TaskResumeServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void 插入失败只计入跳过不抛出() {
|
||||
void 克隆失败只计入失败数不抛出() {
|
||||
FileTaskMapper mapper = mock(FileTaskMapper.class);
|
||||
when(mapper.selectList(any())).thenReturn(List.of(interruptedTask(28587L, "SIMILAR_ASIN")));
|
||||
when(mapper.selectList(any())).thenReturn(List.of(
|
||||
failedTask(28587L, "SIMILAR_ASIN", "任务心跳超时,已自动失败")));
|
||||
when(mapper.selectCount(any())).thenReturn(0L);
|
||||
doThrow(new RuntimeException("库挂了")).when(mapper).insert(any(FileTaskEntity.class));
|
||||
TaskResumeService service = service(mapper, List.of(spi("SIMILAR_ASIN")), true, 3);
|
||||
ClientTaskRetrySpi spi = mock(ClientTaskRetrySpi.class);
|
||||
when(spi.moduleType()).thenReturn("SIMILAR_ASIN");
|
||||
doThrow(new RuntimeException("库挂了")).when(spi).cloneForRetry(any());
|
||||
TaskResumeService service = service(mapper, List.of(spi), true, 3);
|
||||
|
||||
TaskResumeService.ResumeStats stats = service.resumeInterruptedTasks();
|
||||
|
||||
assertEquals(1, stats.skippedTaskCount);
|
||||
assertEquals(1, stats.cloneFailedCount);
|
||||
assertEquals(0, stats.resumedTaskCount);
|
||||
}
|
||||
|
||||
@Test
|
||||
void 无候选时统计不支持续跑的中断任务数() {
|
||||
void 无候选时统计不支持重试的中断任务数() {
|
||||
FileTaskMapper mapper = mock(FileTaskMapper.class);
|
||||
when(mapper.selectList(any())).thenReturn(List.of());
|
||||
when(mapper.selectCount(any())).thenReturn(3L);
|
||||
TaskResumeService service = service(mapper, List.of(spi("SIMILAR_ASIN")), true, 3);
|
||||
TaskResumeService service = service(mapper, List.of(retrySpi("SIMILAR_ASIN", 1L)), true, 3);
|
||||
|
||||
TaskResumeService.ResumeStats stats = service.resumeInterruptedTasks();
|
||||
|
||||
assertEquals(0, stats.resumedTaskCount);
|
||||
assertEquals(3, stats.unsupportedTaskCount, "不支持续跑的模块也要可见,供运维人工重跑");
|
||||
assertEquals(3, stats.unsupportedTaskCount, "不支持重试的模块也要可见,供运维人工重跑");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 没有归属用户的中断任务跳过() {
|
||||
void 没有归属用户的任务跳过() {
|
||||
FileTaskMapper mapper = mock(FileTaskMapper.class);
|
||||
FileTaskEntity orphan = interruptedTask(28587L, "SIMILAR_ASIN");
|
||||
FileTaskEntity orphan = failedTask(28587L, "SIMILAR_ASIN", "任务心跳超时,已自动失败");
|
||||
orphan.setUserId(null);
|
||||
when(mapper.selectList(any())).thenReturn(List.of(orphan));
|
||||
TaskResumeService service = service(mapper, List.of(spi("SIMILAR_ASIN")), true, 3);
|
||||
when(mapper.selectCount(any())).thenReturn(0L);
|
||||
ClientTaskRetrySpi spi = mock(ClientTaskRetrySpi.class);
|
||||
when(spi.moduleType()).thenReturn("SIMILAR_ASIN");
|
||||
TaskResumeService service = service(mapper, List.of(spi), true, 3);
|
||||
|
||||
TaskResumeService.ResumeStats stats = service.resumeInterruptedTasks();
|
||||
|
||||
assertEquals(1, stats.skippedTaskCount);
|
||||
verify(mapper, never()).insert(any(FileTaskEntity.class));
|
||||
verify(spi, never()).cloneForRetry(any());
|
||||
}
|
||||
|
||||
/**
|
||||
* 开关关着时,统计必须说"本轮没跑",不能长得像"跑了但没活"。
|
||||
*
|
||||
* <p>线上 2026-09-28 实测:{@code resume(s=0 r=0 k=0 u=0)} 在两台连续 48 轮全零,
|
||||
* 而 {@code aiimage.task-resume.enabled} 的真实默认是
|
||||
* {@code ${AIIMAGE_TASK_RESUME_ENABLED:${AIIMAGE_CLIENT_TASK_PULL_ENABLED:false}}}(生产两个键都没设)
|
||||
* —— 也就是说 V129 整套续跑从来没执行过,但从巡检日志上和"恰好没有中断任务"完全无法区分。
|
||||
*/
|
||||
/** 开关关着时,统计必须说"本轮没跑",不能长得像"跑了但没活"。 */
|
||||
@Test
|
||||
void 开关关闭时统计标记本轮未执行() {
|
||||
FileTaskMapper mapper = mock(FileTaskMapper.class);
|
||||
TaskResumeService service = service(mapper, List.of(spi("SIMILAR_ASIN")), false, 3);
|
||||
TaskResumeService service = service(mapper, List.of(retrySpi("SIMILAR_ASIN", 1L)), false, 3);
|
||||
|
||||
TaskResumeService.ResumeStats stats = service.resumeInterruptedTasks();
|
||||
|
||||
@@ -196,7 +226,7 @@ class TaskResumeServiceTest {
|
||||
FileTaskMapper mapper = mock(FileTaskMapper.class);
|
||||
when(mapper.selectList(any())).thenReturn(List.of());
|
||||
when(mapper.selectCount(any())).thenReturn(0L);
|
||||
TaskResumeService service = service(mapper, List.of(spi("SIMILAR_ASIN")), true, 3);
|
||||
TaskResumeService service = service(mapper, List.of(retrySpi("SIMILAR_ASIN", 1L)), true, 3);
|
||||
|
||||
TaskResumeService.ResumeStats stats = service.resumeInterruptedTasks();
|
||||
|
||||
@@ -204,7 +234,7 @@ class TaskResumeServiceTest {
|
||||
assertEquals(0, stats.scannedTaskCount);
|
||||
}
|
||||
|
||||
/** 没有注册兜底实现等于跑不了:同样要标记未执行,不能报成"跑了没活"。 */
|
||||
/** 没有注册克隆实现等于跑不了:同样要标记未执行,不能报成"跑了没活"。 */
|
||||
@Test
|
||||
void 无模块注册时也算本轮未执行() {
|
||||
FileTaskMapper mapper = mock(FileTaskMapper.class);
|
||||
@@ -215,8 +245,43 @@ class TaskResumeServiceTest {
|
||||
assertFalse(stats.ran);
|
||||
}
|
||||
|
||||
@Test
|
||||
void dryRun只列候选不克隆() {
|
||||
FileTaskMapper mapper = mock(FileTaskMapper.class);
|
||||
when(mapper.selectList(any())).thenReturn(List.of(
|
||||
failedTask(28587L, "SIMILAR_ASIN", "任务心跳超时,已自动失败")));
|
||||
when(mapper.selectCount(any())).thenReturn(0L);
|
||||
ClientTaskRetrySpi spi = mock(ClientTaskRetrySpi.class);
|
||||
when(spi.moduleType()).thenReturn("SIMILAR_ASIN");
|
||||
TaskResumeService service = service(mapper, List.of(spi), false, 3);
|
||||
|
||||
TaskResumeService.ResumeStats stats = service.retryFailedTasks(36, 200, true, null);
|
||||
|
||||
assertTrue(stats.dryRun);
|
||||
assertEquals(List.of(28587L), stats.candidateTaskIds);
|
||||
assertEquals(0, stats.resumedTaskCount);
|
||||
verify(spi, never()).cloneForRetry(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 回填不受定时开关限制() {
|
||||
FileTaskMapper mapper = mock(FileTaskMapper.class);
|
||||
when(mapper.selectList(any())).thenReturn(List.of(
|
||||
failedTask(28587L, "SIMILAR_ASIN", "任务心跳超时,已自动失败")));
|
||||
when(mapper.selectCount(any())).thenReturn(0L);
|
||||
ClientTaskRetrySpi spi = mock(ClientTaskRetrySpi.class);
|
||||
when(spi.moduleType()).thenReturn("SIMILAR_ASIN");
|
||||
when(spi.cloneForRetry(any())).thenReturn(9001L);
|
||||
TaskResumeService service = service(mapper, List.of(spi), false, 3);
|
||||
|
||||
TaskResumeService.ResumeStats stats = service.retryFailedTasks(36, 200, false, null);
|
||||
|
||||
assertEquals(1, stats.resumedTaskCount);
|
||||
assertEquals(List.of(9001L), stats.retriedTaskIds);
|
||||
}
|
||||
|
||||
/**
|
||||
* 开机必须把续跑的生效配置打出来。
|
||||
* 开机必须把重试的生效配置打出来。
|
||||
*
|
||||
* <p>续跑是"功能写完了、测试全绿、线上默认关"的典型:光看代码里的
|
||||
* {@code @Value(":true")} 会以为默认开着,实际 yml 把默认压成了 false。
|
||||
@@ -230,7 +295,7 @@ class TaskResumeServiceTest {
|
||||
logger.addAppender(appender);
|
||||
try {
|
||||
FileTaskMapper mapper = mock(FileTaskMapper.class);
|
||||
TaskResumeService service = service(mapper, List.of(spi("SIMILAR_ASIN")), false, 3);
|
||||
TaskResumeService service = service(mapper, List.of(retrySpi("SIMILAR_ASIN", 1L)), false, 3);
|
||||
service.logEffectiveConfig();
|
||||
|
||||
String line = appender.list.stream()
|
||||
|
||||
+156
@@ -0,0 +1,156 @@
|
||||
package com.nanri.aiimage.modules.task.service;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nanri.aiimage.modules.task.mapper.FileResultMapper;
|
||||
import com.nanri.aiimage.modules.task.mapper.TaskScopeStateMapper;
|
||||
import com.nanri.aiimage.modules.task.model.entity.FileResultEntity;
|
||||
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
|
||||
import com.nanri.aiimage.modules.task.model.entity.TaskScopeStateEntity;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class TaskRetryCloneSupportTest {
|
||||
|
||||
private static TaskRetryCloneSupport support(FileResultMapper fileResultMapper,
|
||||
TaskScopeStateMapper scopeMapper) {
|
||||
return new TaskRetryCloneSupport(fileResultMapper, scopeMapper, new ObjectMapper());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 克隆任务头保留输入与代数并清空结果计数() {
|
||||
FileTaskMapperHolder holder = new FileTaskMapperHolder();
|
||||
FileTaskEntity original = holder.original();
|
||||
TaskRetryCloneSupport support = support(mock(FileResultMapper.class), mock(TaskScopeStateMapper.class));
|
||||
|
||||
FileTaskEntity child = support.buildChild(original, "{\"parsedPayloadRef\":\"p\"}");
|
||||
|
||||
assertEquals("PENDING", child.getStatus());
|
||||
assertEquals("SIMILAR_ASIN", child.getModuleType());
|
||||
assertEquals(original.getRequestJson(), child.getRequestJson());
|
||||
assertEquals("{\"parsedPayloadRef\":\"p\"}", child.getResultJson());
|
||||
assertEquals(977L, child.getUserId());
|
||||
assertEquals(original.getDeviceId(), child.getDeviceId());
|
||||
assertEquals(28587L, child.getResumeOfTaskId());
|
||||
assertEquals(1, child.getResumeAttempt());
|
||||
assertEquals(0, child.getSuccessFileCount());
|
||||
assertEquals(0, child.getFailedFileCount());
|
||||
assertEquals(original.getCreatedAt(), child.getCreatedAt(), "继承原创建时间才能立刻被兜底拉取看见");
|
||||
assertTrue(child.getTaskNo().startsWith("SIMILAR_ASIN-"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void resultJson摘掉owner快照() throws Exception {
|
||||
TaskRetryCloneSupport support = support(mock(FileResultMapper.class), mock(TaskScopeStateMapper.class));
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
String result = support.resultJsonWithoutOwner(
|
||||
"{\"parsedPayloadRef\":\"rustfs:x\",\"ownerInstanceId\":\"server-110\"}");
|
||||
|
||||
assertEquals("rustfs:x", mapper.readTree(result).path("parsedPayloadRef").asText());
|
||||
assertTrue(mapper.readTree(result).path("ownerInstanceId").isMissingNode());
|
||||
assertThrows(IllegalStateException.class, () -> support.resultJsonWithoutOwner("not-json"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 复制源结果行不带上次的结果文件() {
|
||||
FileResultMapper mapper = mock(FileResultMapper.class);
|
||||
FileResultEntity source = new FileResultEntity();
|
||||
source.setTaskId(28587L);
|
||||
source.setModuleType("SIMILAR_ASIN");
|
||||
source.setSourceFilename("a.xlsx");
|
||||
source.setSourceFileUrl("u");
|
||||
source.setRowCount(10);
|
||||
source.setResultFilename("result.xlsx");
|
||||
source.setResultFileUrl("https://old");
|
||||
source.setUserId(977L);
|
||||
when(mapper.selectOne(any())).thenReturn(source);
|
||||
TaskRetryCloneSupport support = support(mapper, mock(TaskScopeStateMapper.class));
|
||||
|
||||
support.copySourceResult(28587L, 9001L, "SIMILAR_ASIN");
|
||||
|
||||
ArgumentCaptor<FileResultEntity> captor = ArgumentCaptor.forClass(FileResultEntity.class);
|
||||
verify(mapper).insert(captor.capture());
|
||||
FileResultEntity copy = captor.getValue();
|
||||
assertEquals(9001L, copy.getTaskId());
|
||||
assertEquals("a.xlsx", copy.getSourceFilename());
|
||||
assertEquals(10, copy.getRowCount());
|
||||
assertEquals(1, copy.getSuccess());
|
||||
assertNull(copy.getResultFilename(), "上次的结果文件不能带进新任务");
|
||||
assertNull(copy.getResultFileUrl());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 复制解析scope并重置执行状态() {
|
||||
TaskScopeStateMapper mapper = mock(TaskScopeStateMapper.class);
|
||||
TaskScopeStateEntity empty = new TaskScopeStateEntity();
|
||||
empty.setId(1L);
|
||||
TaskScopeStateEntity parsed = new TaskScopeStateEntity();
|
||||
parsed.setId(2L);
|
||||
parsed.setTaskId(28587L);
|
||||
parsed.setModuleType("SIMILAR_ASIN");
|
||||
parsed.setScopeKey("scope");
|
||||
parsed.setScopeHash("hash");
|
||||
parsed.setParsedPayloadJson("\"rustfs:x\"");
|
||||
parsed.setStateJson("{\"phase\":\"DONE\"}");
|
||||
parsed.setChunkTotal(9);
|
||||
parsed.setCompleted(1);
|
||||
parsed.setLlmStatus("DONE");
|
||||
when(mapper.selectList(any())).thenReturn(List.of(empty, parsed));
|
||||
TaskRetryCloneSupport support = support(mock(FileResultMapper.class), mapper);
|
||||
|
||||
TaskScopeStateEntity copy = support.copyParsedScope(28587L, 9001L, "SIMILAR_ASIN");
|
||||
|
||||
assertEquals(9001L, copy.getTaskId());
|
||||
assertEquals("scope", copy.getScopeKey());
|
||||
assertEquals("\"rustfs:x\"", copy.getParsedPayloadJson());
|
||||
assertEquals("{\"phase\":\"PARSED\"}", copy.getStateJson());
|
||||
assertEquals(0, copy.getChunkTotal());
|
||||
assertEquals(0, copy.getCompleted());
|
||||
assertNull(copy.getLlmStatus());
|
||||
verify(mapper).insert(any(TaskScopeStateEntity.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 没有解析scope时返回空() {
|
||||
TaskScopeStateMapper mapper = mock(TaskScopeStateMapper.class);
|
||||
when(mapper.selectList(any())).thenReturn(List.of());
|
||||
TaskRetryCloneSupport support = support(mock(FileResultMapper.class), mapper);
|
||||
|
||||
assertNull(support.copyParsedScope(28587L, 9001L, "APPEARANCE_PATENT"));
|
||||
}
|
||||
|
||||
/** 仅用于把建单样板集中在一处,避免每个用例重复写字段。 */
|
||||
private static final class FileTaskMapperHolder {
|
||||
FileTaskEntity original() {
|
||||
FileTaskEntity task = new FileTaskEntity();
|
||||
task.setId(28587L);
|
||||
task.setTaskNo("SIMILAR_ASIN-1");
|
||||
task.setModuleType("SIMILAR_ASIN");
|
||||
task.setTaskMode("PYTHON_QUEUE");
|
||||
task.setStatus("FAILED");
|
||||
task.setSourceFileCount(1);
|
||||
task.setSuccessFileCount(3);
|
||||
task.setFailedFileCount(1);
|
||||
task.setRequestJson("{\"files\":[]}");
|
||||
task.setResultJson("{\"parsedPayloadRef\":\"p\"}");
|
||||
task.setCreatedBy("user:977");
|
||||
task.setUserId(977L);
|
||||
task.setDeviceId("dev-1");
|
||||
task.setResumeAttempt(0);
|
||||
task.setCreatedAt(LocalDateTime.now().minusHours(2));
|
||||
return task;
|
||||
}
|
||||
}
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package com.nanri.aiimage.modules.task.service;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
class TaskRetryEligibilityTest {
|
||||
|
||||
@Test
|
||||
void 代理或LLM欠费不重试() {
|
||||
assertEquals(TaskRetryEligibility.Category.BALANCE,
|
||||
TaskRetryEligibility.forceMajeureCategory("代理提取失败:余额不足"));
|
||||
assertEquals(TaskRetryEligibility.Category.BALANCE,
|
||||
TaskRetryEligibility.forceMajeureCategory("套餐不存在,请续费"));
|
||||
assertEquals(TaskRetryEligibility.Category.BALANCE,
|
||||
TaskRetryEligibility.forceMajeureCategory("insufficient_user_quota"));
|
||||
assertEquals(TaskRetryEligibility.Category.BALANCE,
|
||||
TaskRetryEligibility.forceMajeureCategory("Invalid API Key"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 服务不可用不重试() {
|
||||
assertEquals(TaskRetryEligibility.Category.SERVICE_DOWN,
|
||||
TaskRetryEligibility.forceMajeureCategory("503 Service Unavailable"));
|
||||
assertEquals(TaskRetryEligibility.Category.SERVICE_DOWN,
|
||||
TaskRetryEligibility.forceMajeureCategory("对象存储出口不可达"));
|
||||
assertEquals(TaskRetryEligibility.Category.SERVICE_DOWN,
|
||||
TaskRetryEligibility.forceMajeureCategory("Connection refused"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 自动登录失败不重试() {
|
||||
assertEquals(TaskRetryEligibility.Category.CREDENTIAL,
|
||||
TaskRetryEligibility.forceMajeureCategory(
|
||||
"相似ASIN检测结果为空;客户端报告:aiprice 未登录且自动登录失败,货源查询无法采集"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 心跳超时和回传失败仍可重试() {
|
||||
assertNull(TaskRetryEligibility.forceMajeureCategory("任务心跳超时,已自动失败"));
|
||||
assertNull(TaskRetryEligibility.forceMajeureCategory(
|
||||
"中间分批回传失败,终止本次采集以避免服务端数据残缺"));
|
||||
assertNull(TaskRetryEligibility.forceMajeureCategory("任务长期未被领取,已自动失败"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 空原因按可重试处理() {
|
||||
assertNull(TaskRetryEligibility.forceMajeureCategory(null));
|
||||
assertNull(TaskRetryEligibility.forceMajeureCategory(" "));
|
||||
}
|
||||
}
|
||||
@@ -266,13 +266,14 @@ class SourceScanTest {
|
||||
// ---------------------------------------------------------------- N6 moduleSources
|
||||
|
||||
@Test
|
||||
void moduleSources_appearancepatent返回44支含拆分后的support件() {
|
||||
void moduleSources_appearancepatent返回45支含拆分后的support件() {
|
||||
List<Path> files = SourceScan.moduleSources("modules/appearancepatent");
|
||||
|
||||
assertThat(files)
|
||||
.as("真实集成:对真仓目录求值。44 支=1 controller+1 client+1 service 主件+3 spi/cache+"
|
||||
+ "21 service/support+16 model(dto7/vo9),与 find 实测同号(3A 拆薄第十三笔再落 5 支)")
|
||||
.hasSize(44);
|
||||
.as("真实集成:对真仓目录求值。45 支=1 controller+1 client+1 service 主件+4 spi/cache+"
|
||||
+ "21 service/support+16 model(dto7/vo9),与 find 实测同号"
|
||||
+ "(3A 拆薄第十三笔再落 5 支;失败任务重试再落 1 支 RetrySpi)")
|
||||
.hasSize(45);
|
||||
assertThat(files.stream().map(p -> p.toString().replace('\\', '/')).toList())
|
||||
.as("support 目录里的既有件必须在面里——聚合入口存在的理由就是拆分后的新文件要自动被看到")
|
||||
.anyMatch(rel -> rel.endsWith("service/support/AppearancePatentRowNormalizer.java"));
|
||||
|
||||
Reference in New Issue
Block a user