task-157: 任务存在结果缺失巡检(终态任务无 file_result 检出、运行中忽略、只读、可重复)+ 8 条测试
This commit is contained in:
+81
@@ -0,0 +1,81 @@
|
||||
package com.nanri.aiimage.modules.task.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
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.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 任务存在结果缺失巡检(task-157)。
|
||||
*
|
||||
* 只读巡检:终态(SUCCESS/FAILED)任务无对应 file_result 行的清单输出报表;
|
||||
* 运行中/待处理任务忽略;绝不修改任何状态。可重复执行。
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class TaskResultMissingInspector {
|
||||
|
||||
private final FileTaskMapper fileTaskMapper;
|
||||
private final FileResultMapper fileResultMapper;
|
||||
|
||||
public record MissingResultEntry(Long taskId, String moduleType, String status, String updatedAt) {
|
||||
}
|
||||
|
||||
public record MissingResultReport(List<MissingResultEntry> entries) {
|
||||
|
||||
public boolean isEmpty() {
|
||||
return entries == null || entries.isEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public MissingResultReport inspectTasksMissingResult(int limit) {
|
||||
List<FileTaskEntity> tasks = fileTaskMapper.selectList(new LambdaQueryWrapper<FileTaskEntity>()
|
||||
.in(FileTaskEntity::getStatus, List.of("SUCCESS", "FAILED"))
|
||||
.orderByAsc(FileTaskEntity::getUpdatedAt)
|
||||
.last("limit " + Math.max(1, Math.min(limit, 500))));
|
||||
if (tasks == null || tasks.isEmpty()) {
|
||||
return new MissingResultReport(List.of());
|
||||
}
|
||||
Set<Long> taskIds = tasks.stream()
|
||||
.map(FileTaskEntity::getId)
|
||||
.filter(id -> id != null && id > 0)
|
||||
.collect(Collectors.toSet());
|
||||
Map<Long, List<FileResultEntity>> resultsByTask = taskIds.isEmpty() ? Map.of()
|
||||
: fileResultMapper.selectList(new LambdaQueryWrapper<FileResultEntity>()
|
||||
.in(FileResultEntity::getTaskId, taskIds)).stream()
|
||||
.collect(Collectors.groupingBy(FileResultEntity::getTaskId));
|
||||
|
||||
List<MissingResultEntry> entries = new ArrayList<>();
|
||||
for (FileTaskEntity task : tasks) {
|
||||
if (task.getId() == null || task.getId() <= 0) {
|
||||
continue;
|
||||
}
|
||||
List<FileResultEntity> results = resultsByTask.get(task.getId());
|
||||
if (results == null || results.isEmpty()) {
|
||||
entries.add(new MissingResultEntry(
|
||||
task.getId(), task.getModuleType(), task.getStatus(),
|
||||
task.getUpdatedAt() == null ? null : task.getUpdatedAt().toString()));
|
||||
}
|
||||
}
|
||||
MissingResultReport report = new MissingResultReport(List.copyOf(entries));
|
||||
if (!report.isEmpty()) {
|
||||
log.info("task-missing-result report: count={}", report.entries().size());
|
||||
for (MissingResultEntry entry : report.entries()) {
|
||||
log.info("task-missing-result: taskId={} moduleType={} status={} updatedAt={}",
|
||||
entry.taskId(), entry.moduleType(), entry.status(), entry.updatedAt());
|
||||
}
|
||||
}
|
||||
return report;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user