feat(task-interrupt): 任务「中断+继续」——在跑可停、部分表可下载、已中断可续跑
用户口径(2026-11-03,33179 卡 80%「正在提交检测」的救援出口):任务卡片加
「中断/继续」;中断后已完成检测的数据生成结果文件供下载,未完成行不再检测;
继续在原任务上续跑剩余行(断点续传,已检测行不重检)。
中断(POST /api/tasks/{taskId}/interrupt,鉴权镜像 retry:resolveOwnershipUserId
+归属校验,普通账号只能中断自己名下的任务;终态重按幂等 no-op):
- TaskHeartbeatService.markCancelledByUser:ACTIVE→CANCELLED 条件更新(词表走
TaskStatuses.FileTask 常量),写「用户中断任务,已完成部分可下载」+finished_at,
syncTerminalState 同步模块缓存与进度快照;客户端心跳拿到 notAlive 即协作停止
(app_client/maixiang 零改动,≤60s 自停)。
- LLM 批循环批间查终态即停:当前批跑完不再发新批(isOwningTaskTerminal)。
- 中断任务的组装跳过候选申领/LLM/补检,直接按已有行账出部分表(allowPartial),
状态保持 CANCELLED 不改写,只补行账缺口文案;cleanupResultFileJob 对 CANCELLED
任务保留分片/范围态/瞬态载荷(这就是「继续」的续跑点),常规交付照旧清理。
继续(retry 链路放宽 CANCELLED,全部在原任务上):
- retryInPlace 条件更新放宽 FAILED/CANCELLED;CAS 前经 ResultFileJobHandler
新钩子 onTaskResumed 清模块补检预算(AP:删 __recheck__ 哨兵键;不清则继续后
第一轮就被「预算耗尽」直接按部分表收尾,钩子抛错即中止不留半态)。
- 中断已交付过的:清 result.result_file_url/size(让位给新表)+把 SUCCESS 的
ASSEMBLE_RESULT 作业复位 PENDING(retry/stuck 计数清零、清错误与 finished_at);
FAILED 仍走既有 resetTerminalFailedForRecovery。
- 校验文案「只有失败或已中断的任务可以继续」;成功任务不可继续。
前端(用户端外观专利 Tab 先行,后端端点本身通用):中断按钮进当前/历史两个
动作槽(确认框说明「未完成行不再检测、已完成检测的数据会生成结果文件供下载」);
CANCELLED 显示「已中断」(判定先于 isResultPreparing,不掉「等待中」兜底);
重试按钮文案 CANCELLED→「继续」,继续的确认框警示部分结果文件将被替换请先下载;
轮询收敛集合与 keepPendingFileTask 补 CANCELLED。棘轮:前端 catch 块 310→311
(interruptTaskRecord 一支,两处 ElMessageBox 确认走 .then 两臂不新增 catch);
状态词表全走 TaskStatuses 常量。
测试:TaskHeartbeatServiceTest +4(CAS 落库+缓存同步/终态幂等/非本人 403/
任务不存在明确报错)、TaskInPlaceRetryServiceTest +3(继续复位+作业复位+
补检钩子只对 CANCELLED 触发/成功不可继续)、新增 AppearancePatentInterrupted
ResumeTest(3)(保留分片=续跑点/常规交付照旧清理/中断收尾不改写状态)与
AppearancePatentLlmBatchPipelineInterruptTest(2)(批间终态即停);后端定向
79+22 全绿。前端新增 appearance-patent-interrupt-continue.test.ts(9);npm test
1003/1003、test:mount 55/55、vue-tsc 零错;ops 快检 overall=pass(grep 需
Git usr/bin 在 PATH,两红格系本机 shell 环境非代码红)。
并发边界:同工作树另一会话的在飞件(TaskFileJobService 卡死归属守卫、
TaskResultFileJobWorker、DeliverySupport 补检闸及其 3 支测试、useLiveTaskBoard、
task-delete 三件)未卷入本提交;本提交不依赖其中任何符号(appendDeliveryGapMessage/
resetTerminalFailedForRecovery/registry.asMap 均在 HEAD 已在)。
This commit is contained in:
+125
@@ -0,0 +1,125 @@
|
||||
package com.nanri.aiimage.modules.appearancepatent.service.support;
|
||||
|
||||
import com.nanri.aiimage.modules.appearancepatent.service.AppearancePatentTaskCacheService;
|
||||
import com.nanri.aiimage.modules.task.mapper.FileResultMapper;
|
||||
import com.nanri.aiimage.modules.task.mapper.FileTaskMapper;
|
||||
import com.nanri.aiimage.modules.task.mapper.TaskChunkMapper;
|
||||
import com.nanri.aiimage.modules.task.mapper.TaskScopeStateMapper;
|
||||
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
|
||||
import com.nanri.aiimage.modules.task.model.entity.TaskFileJobEntity;
|
||||
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 java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.contains;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* 用户中断(2026-11-03「中断+继续」)的两条承重墙:
|
||||
*
|
||||
* <ol>
|
||||
* <li><b>中断交付保留分片</b>:结果行只存在分片载荷里,作业 SUCCESS 的常规清理会把
|
||||
* 分片删光——删了它,「继续」就没有续跑点(已检结论在分片里,剩余行靠它们重新入候选)。
|
||||
* 中断交付的任务必须跳过清理,保留到任务被删除或继续跑完整表为止。</li>
|
||||
* <li><b>中断收尾不改任务结论</b>:出表不得把用户亲手落的 CANCELLED 改写成 SUCCESS/FAILED
|
||||
* (铁律 14 同一口径),只把行账缺口补进消息;下载按钮挂 result_file_url,与状态无关。</li>
|
||||
* </ol>
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class AppearancePatentInterruptedResumeTest {
|
||||
|
||||
@Mock private FileTaskMapper fileTaskMapper;
|
||||
@Mock private FileResultMapper fileResultMapper;
|
||||
@Mock private TaskScopeStateMapper taskScopeStateMapper;
|
||||
@Mock private TaskChunkMapper taskChunkMapper;
|
||||
@Mock private AppearancePatentPayloadSupport payloadSupport;
|
||||
@Mock private AppearancePatentDeliverySupport deliverySupport;
|
||||
@Mock private AppearancePatentProgressSupport progressSupport;
|
||||
@Mock private AppearancePatentTaskCacheService taskCacheService;
|
||||
|
||||
@Test
|
||||
void 中断交付的任务保留分片作为继续的续跑点() {
|
||||
AppearancePatentResultJobSupport support = support();
|
||||
when(fileTaskMapper.selectById(33179L)).thenReturn(task("CANCELLED"));
|
||||
|
||||
support.cleanupResultFileJob(job());
|
||||
|
||||
verify(taskChunkMapper, never()).delete(any());
|
||||
verify(taskScopeStateMapper, never()).delete(any());
|
||||
verify(payloadSupport, never()).deleteTransientTaskPayloads(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 常规交付仍照旧清理分片() {
|
||||
AppearancePatentResultJobSupport support = support();
|
||||
when(fileTaskMapper.selectById(33179L)).thenReturn(task("RUNNING"));
|
||||
|
||||
support.cleanupResultFileJob(job());
|
||||
|
||||
verify(payloadSupport).deleteTransientTaskPayloads(eq(33179L));
|
||||
verify(taskChunkMapper).delete(any());
|
||||
verify(taskScopeStateMapper).delete(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 中断收尾保持CANCELLED并补行账缺口文案() {
|
||||
AppearancePatentResultJobSupport support = support();
|
||||
FileTaskEntity task = task("CANCELLED");
|
||||
task.setErrorMessage("用户中断任务,已完成部分可下载");
|
||||
// (expected=5, judged=3, noData=1, 缺口键 2):与 RecheckProgressMessageTest 的构造口径一致
|
||||
AppearancePatentRowOutcome.Facts facts =
|
||||
new AppearancePatentRowOutcome.Facts(5, 3, 1, List.of("k1", "k2"));
|
||||
when(deliverySupport.appendDeliveryGapMessage(any(), eq(facts))).thenReturn(
|
||||
"用户中断任务,已完成部分可下载;有 2 行未完成检测、有 1 行未采集到数据(结果文件中这些行已单独标注)");
|
||||
|
||||
Boolean delivered = support.finalizeInterruptedDelivery(
|
||||
task, job(), 6, "用户中断任务,已完成部分可下载", facts);
|
||||
|
||||
assertTrue(delivered, "中断交付按部分表收尾,作业走 SUCCESS");
|
||||
ArgumentCaptor<FileTaskEntity> saved = ArgumentCaptor.forClass(FileTaskEntity.class);
|
||||
verify(fileTaskMapper).updateById(saved.capture());
|
||||
assertEquals("CANCELLED", saved.getValue().getStatus(), "出表不得改写用户亲手落的终态");
|
||||
assertTrue(saved.getValue().getErrorMessage().contains("有 2 行未完成检测"));
|
||||
assertTrue(saved.getValue().getErrorMessage().contains("有 1 行未采集到数据"));
|
||||
verify(progressSupport).saveFileBuildProgress(eq(task), eq(job()), eq(6), eq(6), contains("结果文件已生成"));
|
||||
verify(taskCacheService).deleteTaskCache(eq(33179L));
|
||||
}
|
||||
|
||||
private AppearancePatentResultJobSupport support() {
|
||||
// 仅本类三格涉及的协作者给 mock,其余传 null(构造器只赋值,与 RecheckProgressMessageTest 同口径)
|
||||
return new AppearancePatentResultJobSupport(
|
||||
"APPEARANCE_PATENT", 0L, null, null,
|
||||
fileTaskMapper, fileResultMapper, taskScopeStateMapper, taskChunkMapper,
|
||||
taskCacheService, null, null,
|
||||
payloadSupport, null, null, deliverySupport, progressSupport, null,
|
||||
null, null, null, null, null, null, null);
|
||||
}
|
||||
|
||||
private static FileTaskEntity task(String status) {
|
||||
FileTaskEntity task = new FileTaskEntity();
|
||||
task.setId(33179L);
|
||||
task.setModuleType("APPEARANCE_PATENT");
|
||||
task.setStatus(status);
|
||||
return task;
|
||||
}
|
||||
|
||||
private static TaskFileJobEntity job() {
|
||||
TaskFileJobEntity job = new TaskFileJobEntity();
|
||||
job.setId(23815L);
|
||||
job.setTaskId(33179L);
|
||||
job.setResultId(36102L);
|
||||
job.setModuleType("APPEARANCE_PATENT");
|
||||
job.setStatus("RUNNING");
|
||||
return job;
|
||||
}
|
||||
}
|
||||
+149
@@ -0,0 +1,149 @@
|
||||
package com.nanri.aiimage.modules.appearancepatent.service.support;
|
||||
|
||||
import com.nanri.aiimage.config.AppearancePatentProperties;
|
||||
import com.nanri.aiimage.modules.appearancepatent.client.AppearancePatentLlmClient;
|
||||
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentResultRowDto;
|
||||
import com.nanri.aiimage.modules.appearancepatent.service.AppearancePatentTaskCacheService;
|
||||
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
|
||||
import com.nanri.aiimage.modules.task.model.entity.TaskChunkEntity;
|
||||
import com.nanri.aiimage.modules.task.model.entity.TaskFileJobEntity;
|
||||
import com.nanri.aiimage.modules.task.service.TaskFileJobService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyList;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.lenient;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* 用户中断(2026-11-03)后 LLM 批循环必须收手:当前批跑完、结果合并,下一批不再发——
|
||||
* 继续发只会白扣用户密钥次数。两条:批循环开跑前已终态 ⇒ 零批发;批间变终态 ⇒ 只发已进入的那批。
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class AppearancePatentLlmBatchPipelineInterruptTest {
|
||||
|
||||
private static final Long TASK_ID = 33179L;
|
||||
|
||||
@Mock private AppearancePatentProperties properties;
|
||||
@Mock private AppearancePatentLlmClient llmClient;
|
||||
@Mock private AppearancePatentTaskCacheService taskCacheService;
|
||||
@Mock private TaskFileJobService taskFileJobService;
|
||||
|
||||
private AppearancePatentLlmBatchPipeline pipeline;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
lenient().when(properties.getLlmBatchSize()).thenReturn(2);
|
||||
pipeline = new AppearancePatentLlmBatchPipeline(
|
||||
properties,
|
||||
llmClient,
|
||||
taskCacheService,
|
||||
taskFileJobService,
|
||||
AppearancePatentLlmBatchPipelineInterruptTest::rowKey,
|
||||
row -> true,
|
||||
chunk -> chunkRows(),
|
||||
taskId -> List.of(chunk()),
|
||||
taskId -> true,
|
||||
taskId -> { },
|
||||
task -> "prompt",
|
||||
task -> "api-key",
|
||||
task -> null,
|
||||
(taskId, scopeHash, chunkIndex, rows) -> List.of(),
|
||||
(preferred, fallback) -> preferred == null || preferred.isBlank() ? fallback : preferred,
|
||||
(task, message) -> { });
|
||||
lenient().when(llmClient.inspectRows(anyList(), anyString(), anyString()))
|
||||
.thenAnswer(invocation -> new ArrayList<>(invocation.getArgument(0)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 已终态的任务一批都不再发() {
|
||||
FileTaskEntity task = task("CANCELLED");
|
||||
when(taskFileJobService.isOwningTaskTerminal(TASK_ID)).thenReturn(true);
|
||||
// 候选必须真实存在:不 stub 申领则候选为空、循环根本进不去,断言会空转
|
||||
lenient().when(taskCacheService.beginRowInspection(eq(TASK_ID), anyString())).thenReturn(true);
|
||||
|
||||
pipeline.submitLlmBatches(task, null, job(), List.of(chunk()));
|
||||
|
||||
verify(llmClient, never()).inspectRows(anyList(), anyString(), anyString());
|
||||
verify(taskCacheService, never()).touchRowInspection(anyLong(), anyList());
|
||||
verify(taskCacheService, never()).countRowInspectionRound(anyLong(), anyList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 批间变终态只发已进入的那一批() {
|
||||
FileTaskEntity task = task("CANCELLED");
|
||||
// 第 1 次检查(批 1 前)未终态,第 2 次检查(批 2 前)已中断
|
||||
when(taskFileJobService.isOwningTaskTerminal(TASK_ID)).thenReturn(false, true);
|
||||
// 4 行、批 2 行 ⇒ 两个候选批;只有批 1 真发出去
|
||||
Map<String, AppearancePatentResultRowDto> rows = new LinkedHashMap<>();
|
||||
for (int i = 1; i <= 4; i++) {
|
||||
AppearancePatentResultRowDto row = new AppearancePatentResultRowDto();
|
||||
row.setRowToken("r" + i);
|
||||
row.setAsin("B0A000000" + i);
|
||||
rows.put("r" + i, row);
|
||||
}
|
||||
lenient().when(taskCacheService.beginRowInspection(eq(TASK_ID), anyString())).thenReturn(true);
|
||||
|
||||
pipeline.submitLlmBatches(task, null, job(), List.of(chunk()));
|
||||
|
||||
verify(llmClient, times(1)).inspectRows(anyList(), anyString(), anyString());
|
||||
// 没落库的行撤销处理中标记(批 1 的 keysToRetry),下一轮/继续时能重新申领
|
||||
verify(taskCacheService).releaseRowInspection(eq(TASK_ID), anyList());
|
||||
}
|
||||
|
||||
private static String rowKey(AppearancePatentResultRowDto row) {
|
||||
return row == null || row.getRowToken() == null ? "" : row.getRowToken();
|
||||
}
|
||||
|
||||
private static FileTaskEntity task(String status) {
|
||||
FileTaskEntity task = new FileTaskEntity();
|
||||
task.setId(TASK_ID);
|
||||
task.setModuleType("APPEARANCE_PATENT");
|
||||
task.setUserId(1293L);
|
||||
task.setStatus(status);
|
||||
return task;
|
||||
}
|
||||
|
||||
private static TaskFileJobEntity job() {
|
||||
TaskFileJobEntity job = new TaskFileJobEntity();
|
||||
job.setId(23815L);
|
||||
job.setTaskId(TASK_ID);
|
||||
job.setStatus("RUNNING");
|
||||
return job;
|
||||
}
|
||||
|
||||
private static TaskChunkEntity chunk() {
|
||||
TaskChunkEntity chunk = new TaskChunkEntity();
|
||||
chunk.setTaskId(TASK_ID);
|
||||
chunk.setScopeHash("scope-hash-1");
|
||||
chunk.setChunkIndex(1);
|
||||
return chunk;
|
||||
}
|
||||
|
||||
/** 单片载荷反序列化产物:本测试用同一份行集代表所有分片(行键 r1…r4)。 */
|
||||
private static Map<String, AppearancePatentResultRowDto> chunkRows() {
|
||||
Map<String, AppearancePatentResultRowDto> rows = new LinkedHashMap<>();
|
||||
for (int i = 1; i <= 4; i++) {
|
||||
AppearancePatentResultRowDto row = new AppearancePatentResultRowDto();
|
||||
row.setRowToken("r" + i);
|
||||
row.setAsin("B0A000000" + i);
|
||||
rows.put("r" + i, row);
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user