feat(task-retry): 失败任务改为原任务上重试 + 重试按钮(用户拍板)
用户口径(2026-11-01):失败不再自动克隆(一单失败几次就多几行结果),
改成「删除」旁边的「重试」按钮,在原任务上断点续传;自动重试也只复位原任务。
- 新增 TaskInPlaceRetryService:FAILED→PENDING,清错误/终态时间,代数+1,
保留分片/解析载荷/已检测标记;结果行清错误、组装作业复位(含卡死重排计数清零)
- TaskResumeService 自动重试改为调用原地复位(不再 cloneForRetry)
- TaskRetryEligibility 新增 DELIVERY(结果文件生成多次失败/卡死重排不重试)
- 新端点 POST /api/appearance-patent/tasks/{id}/retry;前端失败任务显示重试按钮
- ⚠ 本笔的 BrandAppearancePatentTab.vue 里另含他路在飞的「删除失败任务时一并删任务行」
两小段(同一文件无法分块提交),随本笔一并入库
- 用例:TaskInPlaceRetryServiceTest 2、TaskResumeServiceTest 19、Eligibility 9、
契约 8、前端 catch 棘轮 4,均绿
This commit is contained in:
+17
@@ -36,6 +36,7 @@ import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import com.nanri.aiimage.modules.task.model.dto.TaskProgressLightRequest;
|
||||
import com.nanri.aiimage.modules.task.model.vo.TaskProgressLightBatchVo;
|
||||
import com.nanri.aiimage.modules.task.service.TaskInPlaceRetryService;
|
||||
import com.nanri.aiimage.modules.task.service.TaskProgressOwnershipSupport;
|
||||
|
||||
@RestController
|
||||
@@ -47,6 +48,7 @@ public class AppearancePatentController {
|
||||
|
||||
private final AppearancePatentTaskService service;
|
||||
private final TaskProgressOwnershipSupport progressOwnershipSupport;
|
||||
private final TaskInPlaceRetryService taskRetryService;
|
||||
private final AdminAuthSupport adminAuthSupport;
|
||||
|
||||
@PostMapping("/parse")
|
||||
@@ -153,6 +155,21 @@ public class AppearancePatentController {
|
||||
return ApiResponse.success(null);
|
||||
}
|
||||
|
||||
@PostMapping("/tasks/{taskId}/retry")
|
||||
@Operation(summary = "重试失败任务",
|
||||
description = "在原任务上复位重试:保留已收结果分片、解析载荷与已检测标记(断点续传),"
|
||||
+ "不创建新任务。只有 FAILED 状态可重试。")
|
||||
public ApiResponse<Void> retryTask(
|
||||
@Parameter(description = "外观专利检测任务 ID。", required = true, example = "3938")
|
||||
@PathVariable Long taskId,
|
||||
@Parameter(description = "当前用户 ID,必须与创建任务的用户一致。", required = true, example = "1")
|
||||
@RequestParam("user_id") Long userId,
|
||||
HttpServletRequest httpRequest) {
|
||||
userId = adminAuthSupport.resolveOwnershipUserId(httpRequest, userId);
|
||||
taskRetryService.retryFailedTask(taskId, userId, AppearancePatentTaskService.MODULE_TYPE);
|
||||
return ApiResponse.success(null);
|
||||
}
|
||||
|
||||
@DeleteMapping("/history/{resultId}")
|
||||
@Operation(summary = "删除历史记录", description = "删除当前用户的一条外观专利检测历史记录。只删除 biz_file_result 记录,不主动删除任务主记录。")
|
||||
public ApiResponse<Void> deleteHistory(
|
||||
|
||||
+3
@@ -146,6 +146,9 @@ public class TaskFileJobService {
|
||||
.eq(TaskFileJobEntity::getId, existing.getId())
|
||||
.set(TaskFileJobEntity::getStatus, "PENDING")
|
||||
.set(TaskFileJobEntity::getRetryCount, 0)
|
||||
// 卡死重排计数一并清零:不清的话,上一次「连续 20 次卡死重排」会把刚复位的
|
||||
// 作业立刻再判死(2026-11-01 用户重试按钮的现场就是这条计数打满)。
|
||||
.set(TaskFileJobEntity::getStuckRequeueCount, 0)
|
||||
.set(TaskFileJobEntity::getErrorMessage, null)
|
||||
.set(TaskFileJobEntity::getFinishedAt, null)
|
||||
.set(TaskFileJobEntity::getTerminalCallbackAt, null)
|
||||
|
||||
+131
@@ -0,0 +1,131 @@
|
||||
package com.nanri.aiimage.modules.task.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.nanri.aiimage.common.task.TaskStatuses;
|
||||
import com.nanri.aiimage.modules.task.mapper.FileResultMapper;
|
||||
import com.nanri.aiimage.modules.task.mapper.FileTaskMapper;
|
||||
import com.nanri.aiimage.modules.task.model.entity.FileResultEntity;
|
||||
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 失败任务「在原任务上重试」的唯一出口(2026-11-01 用户拍板)。
|
||||
*
|
||||
* <p>用户口径:失败任务不再克隆重试——克隆会让同一份输入在界面上变成好几行结果
|
||||
* (现网 32967→33001→33017 就是这样:三代克隆、三行「结果文件生成多次失败」)。
|
||||
* 重试必须落在**原任务**上:保留解析载荷、已收分片、已检测标记,把状态从 FAILED 复位成
|
||||
* PENDING 让执行方接着跑——这就是断点续传,而不是从头再采一遍。
|
||||
*
|
||||
* <p>只动三样东西:
|
||||
* <ul>
|
||||
* <li>任务头:FAILED → PENDING,清 error_message / finished_at,代数 +1;</li>
|
||||
* <li>结果行:清上一次的错误文案(结果文件 URL 保留,新文件生成后会覆盖;</li>
|
||||
* <li>组装作业:终态失败的 ASSEMBLE_RESULT 复位成 PENDING(含卡死重排计数清零)。</li>
|
||||
* </ul>
|
||||
* 分片、scope 状态、解析载荷一律不动——它们就是续跑点。
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class TaskInPlaceRetryService {
|
||||
|
||||
private static final String STATUS_FAILED = "FAILED";
|
||||
private static final String STATUS_PENDING = "PENDING";
|
||||
|
||||
private final FileTaskMapper fileTaskMapper;
|
||||
private final FileResultMapper fileResultMapper;
|
||||
private final TaskFileJobService taskFileJobService;
|
||||
|
||||
/**
|
||||
* 把一条失败任务复位成待执行。幂等:只有当前状态确实是 FAILED 才动作。
|
||||
*
|
||||
* @return true=已复位;false=状态已不是 FAILED(并发或已重试过)
|
||||
*/
|
||||
public boolean retryInPlace(FileTaskEntity task) {
|
||||
if (task == null || task.getId() == null) {
|
||||
return false;
|
||||
}
|
||||
int attempt = task.getResumeAttempt() == null ? 0 : task.getResumeAttempt();
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
int updated = fileTaskMapper.update(null, new LambdaUpdateWrapper<FileTaskEntity>()
|
||||
.eq(FileTaskEntity::getId, task.getId())
|
||||
.eq(FileTaskEntity::getStatus, STATUS_FAILED)
|
||||
.set(FileTaskEntity::getStatus, STATUS_PENDING)
|
||||
.set(FileTaskEntity::getErrorMessage, null)
|
||||
.set(FileTaskEntity::getFinishedAt, null)
|
||||
.set(FileTaskEntity::getResumeAttempt, attempt + 1)
|
||||
.set(FileTaskEntity::getUpdatedAt, now));
|
||||
if (updated == 0) {
|
||||
log.info("[task-retry] 状态已不是 FAILED,跳过复位 taskId={}", task.getId());
|
||||
return false;
|
||||
}
|
||||
clearResultError(task.getId(), task.getModuleType());
|
||||
resetAssembleJob(task.getId(), task.getModuleType());
|
||||
log.info("[task-retry] 已在原任务上复位重试 taskId={} moduleType={} 代数={} 上次失败={}",
|
||||
task.getId(), task.getModuleType(), attempt + 1, abbreviate(task.getErrorMessage()));
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户点「重试」的入口:校验归属与状态后原地复位。
|
||||
*
|
||||
* @throws com.nanri.aiimage.common.exception.BusinessException 任务不存在 / 不是失败态 / 状态已变
|
||||
*/
|
||||
public void retryFailedTask(Long taskId, Long userId, String moduleType) {
|
||||
FileTaskEntity task = fileTaskMapper.selectById(taskId);
|
||||
if (task == null || !moduleType.equals(task.getModuleType())
|
||||
|| !java.util.Objects.equals(userId, task.getUserId())) {
|
||||
throw new com.nanri.aiimage.common.exception.BusinessException("任务不存在");
|
||||
}
|
||||
if (!STATUS_FAILED.equals(task.getStatus())) {
|
||||
throw new com.nanri.aiimage.common.exception.BusinessException("只有失败的任务可以重试");
|
||||
}
|
||||
if (!retryInPlace(task)) {
|
||||
throw new com.nanri.aiimage.common.exception.BusinessException("任务状态已变化,请刷新后再试");
|
||||
}
|
||||
}
|
||||
|
||||
/** 结果行只清错误文案:URL 留着(上一份可能已经可用),新文件生成后自然覆盖。 */
|
||||
private void clearResultError(Long taskId, String moduleType) {
|
||||
try {
|
||||
fileResultMapper.update(null, new LambdaUpdateWrapper<FileResultEntity>()
|
||||
.eq(FileResultEntity::getTaskId, taskId)
|
||||
.eq(FileResultEntity::getModuleType, moduleType)
|
||||
.set(FileResultEntity::getErrorMessage, null)
|
||||
.set(FileResultEntity::getUpdatedAt, LocalDateTime.now()));
|
||||
} catch (RuntimeException ex) {
|
||||
// 结果行清不掉不阻断重试:它只影响界面旧文案,任务本身照样能重跑
|
||||
log.warn("[task-retry] 清理结果行错误失败(忽略) taskId={} err={}", taskId, ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/** 组装作业复位:终态失败的那条(retryCount 打满)重新排队,卡死重排计数一并清零。 */
|
||||
private void resetAssembleJob(Long taskId, String moduleType) {
|
||||
try {
|
||||
FileResultEntity result = fileResultMapper.selectOne(new LambdaQueryWrapper<FileResultEntity>()
|
||||
.eq(FileResultEntity::getTaskId, taskId)
|
||||
.eq(FileResultEntity::getModuleType, moduleType)
|
||||
.orderByAsc(FileResultEntity::getId)
|
||||
.last("limit 1"));
|
||||
if (result == null) {
|
||||
return;
|
||||
}
|
||||
taskFileJobService.resetTerminalFailedForRecovery(taskId, moduleType, result.getId());
|
||||
} catch (RuntimeException ex) {
|
||||
log.warn("[task-retry] 复位组装作业失败(忽略) taskId={} err={}", taskId, ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private static String abbreviate(String text) {
|
||||
if (text == null) {
|
||||
return "";
|
||||
}
|
||||
String one = text.replace('\n', ' ').trim();
|
||||
return one.length() <= 80 ? one : one.substring(0, 80) + "…";
|
||||
}
|
||||
}
|
||||
+13
-8
@@ -70,8 +70,10 @@ public class TaskResumeService {
|
||||
* {@link TaskFileJobService#hasUnfinishedAssembleJob(Long)}。
|
||||
*/
|
||||
private final TaskFileJobService taskFileJobs;
|
||||
/** moduleType → 克隆重试实现;只有注册了本 SPI 的模块才会自动重试。 */
|
||||
/** moduleType → 克隆重试实现;只有注册了本 SPI 的模块才会自动重试(SPI 现在只作「支持重试」白名单用)。 */
|
||||
private final Map<String, ClientTaskRetrySpi> retryHandlers;
|
||||
/** 原任务原地复位(2026-11-01 用户拍板:不再克隆,重试必须落在原任务上)。 */
|
||||
private final TaskInPlaceRetryService inPlaceRetry;
|
||||
|
||||
/**
|
||||
* 续跑总开关。默认值必须与 {@code application.yml} 的
|
||||
@@ -93,9 +95,11 @@ public class TaskResumeService {
|
||||
|
||||
public TaskResumeService(FileTaskMapper fileTaskMapper,
|
||||
TaskFileJobService taskFileJobs,
|
||||
TaskInPlaceRetryService inPlaceRetry,
|
||||
List<ClientTaskRetrySpi> retrySpiHandlers) {
|
||||
this.fileTaskMapper = fileTaskMapper;
|
||||
this.taskFileJobs = taskFileJobs;
|
||||
this.inPlaceRetry = inPlaceRetry;
|
||||
Map<String, ClientTaskRetrySpi> retryIndex = new LinkedHashMap<>();
|
||||
if (retrySpiHandlers != null) {
|
||||
for (ClientTaskRetrySpi handler : retrySpiHandlers) {
|
||||
@@ -243,21 +247,22 @@ public class TaskResumeService {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
Long childTaskId = handler.cloneForRetry(original);
|
||||
if (childTaskId == null) {
|
||||
// 2026-11-01 用户拍板:不再克隆新任务,重试落回原任务(保留分片/解析/检测标记
|
||||
// = 断点续传)。克隆会让同一份输入在界面上多出好几行(现网 32967→33001→33017)。
|
||||
if (!inPlaceRetry.retryInPlace(original)) {
|
||||
stats.cloneFailedCount++;
|
||||
log.warn("[task-resume] 克隆返回空(缺解析输入) taskId={} moduleType={}",
|
||||
log.warn("[task-resume] 原任务复位未生效(状态已变) taskId={} moduleType={}",
|
||||
original.getId(), original.getModuleType());
|
||||
continue;
|
||||
}
|
||||
stats.resumedTaskCount++;
|
||||
stats.retriedTaskIds.add(childTaskId);
|
||||
log.warn("[task-resume] 已克隆重试任务 original={} new={} moduleType={} userId={} 代数={}/{} 失败原因={}",
|
||||
original.getId(), childTaskId, original.getModuleType(), original.getUserId(),
|
||||
stats.retriedTaskIds.add(original.getId());
|
||||
log.warn("[task-resume] 已在原任务上复位重试 taskId={} moduleType={} userId={} 代数={}/{} 失败原因={}",
|
||||
original.getId(), original.getModuleType(), original.getUserId(),
|
||||
attempt + 1, maxAttempt, abbreviate(original.getErrorMessage()));
|
||||
} catch (Exception ex) {
|
||||
stats.cloneFailedCount++;
|
||||
log.error("[task-resume] 克隆重试任务失败 taskId={} moduleType={} err={}",
|
||||
log.error("[task-resume] 复位重试原任务失败 taskId={} moduleType={} err={}",
|
||||
original.getId(), original.getModuleType(), ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user