fix(delete-brand): 分片缺失降级组装收尾+失败兜底生成可下载结果

- 结果分片缺失 ≤ missingChunkFallbackLimit(默认20) 时降级组装,缺失行标「未回传」,任务不再整体白跑
- 超阈值失败/组装异常时通过 ResultFileJobHandler.fallbackAssembleOnFailure 兜底生成部分结果文件
- 组装失败文案带缺失分片号明细(如 缺失分片: [247](已收 4283/4284))
- 新增 GET /api/delete-brand/tasks/{taskId}/missing-chunks,补传后自动恢复终态失败组装 job
- stale-check 失败文案改为含最后心跳时间与最近组装失败真实原因(修复误导性「回传长时间无响应」)
- 新增降级合并/缺失分片单测,Handler/Worker 日志审计 stage 白名单补 FALLBACK
This commit is contained in:
2026-09-08 13:18:17 +08:00
parent 12f37ec14e
commit c5341c7138
13 changed files with 581 additions and 15 deletions
@@ -27,6 +27,16 @@ public interface ResultFileJobHandler {
default void onFailure(TaskFileJobEntity job, String message) {
}
/**
* 失败兜底:job 重试耗尽(终态 FAILED)时,用已回传的部分数据尝试生成部分结果文件,
* 让用户仍能下载已做好的部分(缺失行由业务侧降级填充"未回传"等占位状态)。
* 实现应自吞异常(兜底失败只记日志,不影响 job 已终态失败的事实);
* 返回 true 表示已生成可下载的结果文件。
*/
default boolean fallbackAssembleOnFailure(TaskFileJobEntity job, String message) {
return false;
}
default boolean supportsAsyncOffload() {
return false;
}
@@ -6,6 +6,7 @@ import com.nanri.aiimage.modules.task.mapper.TaskFileJobMapper;
import com.nanri.aiimage.modules.task.model.dto.TaskFileJobDispatchEvent;
import com.nanri.aiimage.modules.task.model.entity.TaskFileJobEntity;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
@@ -21,6 +22,7 @@ import java.util.stream.Collectors;
@Service
@RequiredArgsConstructor
@Slf4j
public class TaskFileJobService {
public static final String JOB_TYPE_ASSEMBLE_RESULT = "ASSEMBLE_RESULT";
@@ -88,6 +90,66 @@ public class TaskFileJobService {
return refreshed;
}
/**
* 该 result 是否已有重试耗尽的终态失败组装 job(分片缺失类失败后用于补传恢复判断)。
*/
public boolean isTerminalFailedAssembleJob(Long taskId, String moduleType, Long resultId) {
TaskFileJobEntity existing = findJob(taskId, moduleType, resultId, JOB_TYPE_ASSEMBLE_RESULT);
if (existing == null || !"FAILED".equals(existing.getStatus())) {
return false;
}
int retry = existing.getRetryCount() == null ? 0 : existing.getRetryCount();
return retry >= MAX_RETRY_COUNT;
}
/**
* 补传完整后恢复终态失败的组装 job:重置为 PENDING(retryCount 清零)重新派发。
* 幂等:job 不存在、非 FAILED 或未达重试上限时不动作,返回当前 job。
*/
public TaskFileJobEntity resetTerminalFailedForRecovery(Long taskId, String moduleType, Long resultId) {
TaskFileJobEntity existing = findJob(taskId, moduleType, resultId, JOB_TYPE_ASSEMBLE_RESULT);
if (existing == null || !"FAILED".equals(existing.getStatus())) {
return existing;
}
int retry = existing.getRetryCount() == null ? 0 : existing.getRetryCount();
if (retry < MAX_RETRY_COUNT) {
return existing;
}
LocalDateTime now = LocalDateTime.now();
taskFileJobMapper.update(null, new LambdaUpdateWrapper<TaskFileJobEntity>()
.eq(TaskFileJobEntity::getId, existing.getId())
.set(TaskFileJobEntity::getStatus, "PENDING")
.set(TaskFileJobEntity::getRetryCount, 0)
.set(TaskFileJobEntity::getErrorMessage, null)
.set(TaskFileJobEntity::getFinishedAt, null)
.set(TaskFileJobEntity::getTerminalCallbackAt, null)
.set(TaskFileJobEntity::getUpdatedAt, now));
log.info("[task-file-job] terminal failed assemble job reset for recovery jobId={} taskId={} moduleType={} resultId={}",
existing.getId(), taskId, moduleType, resultId);
TaskFileJobEntity refreshed = taskFileJobMapper.selectById(existing.getId());
publishDispatchEvent(refreshed);
return refreshed;
}
/**
* 最近一次失败组装 job 的错误信息(用于任务超时失败文案里拼接真实原因)。
*/
public String latestFailedAssembleError(Long taskId, String moduleType) {
if (taskId == null || moduleType == null || moduleType.isBlank()) {
return null;
}
TaskFileJobEntity job = taskFileJobMapper.selectOne(new LambdaQueryWrapper<TaskFileJobEntity>()
.eq(TaskFileJobEntity::getTaskId, taskId)
.eq(TaskFileJobEntity::getModuleType, moduleType)
.eq(TaskFileJobEntity::getJobType, JOB_TYPE_ASSEMBLE_RESULT)
.eq(TaskFileJobEntity::getStatus, "FAILED")
.orderByDesc(TaskFileJobEntity::getId)
.last("limit 1"));
return job == null || job.getErrorMessage() == null || job.getErrorMessage().isBlank()
? null
: job.getErrorMessage();
}
public List<TaskFileJobEntity> listRunnableJobs(int limit) {
return taskFileJobMapper.selectList(new LambdaQueryWrapper<TaskFileJobEntity>()
.in(TaskFileJobEntity::getStatus, List.of("PENDING", "FAILED"))
@@ -276,6 +276,16 @@ public class TaskResultFileJobWorker {
ResultFileJobHandler handler = handlerRegistry.asMap().get(job.getModuleType());
if (handler != null) {
handler.onFailure(job, message);
try {
if (handler.fallbackAssembleOnFailure(job, message)) {
log.info("[task-file-job] retry-exhausted fallback assemble produced downloadable result jobId={} taskId={} moduleType={} resultId={} stage=FALLBACK",
job.getId(), job.getTaskId(), job.getModuleType(), job.getResultId());
}
} catch (Exception ex) {
// 兜底失败不影响 job 终态;下次 stuck/exhausted 扫描会重入(幂等),持续失败仅记日志。
log.warn("[task-file-job] fallback assemble threw jobId={} taskId={} moduleType={} resultId={} msg={} stage=FALLBACK",
job.getId(), job.getTaskId(), job.getModuleType(), job.getResultId(), ex.getMessage());
}
}
}