|
@@ -0,0 +1,69 @@
|
|
|
|
|
+package com.jeeplus.assess.programAuditAi.service;
|
|
|
|
|
+
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
|
+import com.jeeplus.assess.programAuditAi.domain.ProgramAuditAiGeneration;
|
|
|
|
|
+import com.jeeplus.assess.programAuditAi.mapper.ProgramAuditAiGenerationMapper;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.boot.ApplicationArguments;
|
|
|
|
|
+import org.springframework.boot.ApplicationRunner;
|
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
+
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 校审 AI 生成任务启动恢复。
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+@Component
|
|
|
|
|
+public class ProgramAuditAiStartupRecovery implements ApplicationRunner {
|
|
|
|
|
+
|
|
|
|
|
+ private static final String STATUS_RUNNING = "running";
|
|
|
|
|
+ private static final String STATUS_FAILED = "failed";
|
|
|
|
|
+ private static final String ERROR_CODE_INTERRUPTED = "AI_INTERRUPTED";
|
|
|
|
|
+ private static final String ERROR_MESSAGE_INTERRUPTED = "服务重启导致生成中断,请重新生成";
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private ProgramAuditAiGenerationMapper generationMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void run(ApplicationArguments args) {
|
|
|
|
|
+ recoverInterruptedTasks();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 将启动前遗留的 running 任务全部置为 failed。
|
|
|
|
|
+ */
|
|
|
|
|
+ public int recoverInterruptedTasks() {
|
|
|
|
|
+ QueryWrapper<ProgramAuditAiGeneration> query = new QueryWrapper<>();
|
|
|
|
|
+ query.eq("status", STATUS_RUNNING)
|
|
|
|
|
+ .isNull("finished_at");
|
|
|
|
|
+ List<ProgramAuditAiGeneration> runningTasks = generationMapper.selectList(query);
|
|
|
|
|
+ if (runningTasks == null || runningTasks.isEmpty()) {
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Date finishedAt = new Date();
|
|
|
|
|
+ int recovered = 0;
|
|
|
|
|
+ for (ProgramAuditAiGeneration task : runningTasks) {
|
|
|
|
|
+ task.setStatus(STATUS_FAILED);
|
|
|
|
|
+ task.setDetailStatus(STATUS_FAILED);
|
|
|
|
|
+ task.setReportStatus(STATUS_FAILED);
|
|
|
|
|
+ task.setRemarksStatus(STATUS_FAILED);
|
|
|
|
|
+ task.setWorkStatus(STATUS_FAILED);
|
|
|
|
|
+ task.setErrorCode(ERROR_CODE_INTERRUPTED);
|
|
|
|
|
+ task.setErrorMessage(ERROR_MESSAGE_INTERRUPTED);
|
|
|
|
|
+ task.setFinishedAt(finishedAt);
|
|
|
|
|
+ if (task.getStartedAt() != null) {
|
|
|
|
|
+ task.setDurationMs(Math.max(0L, finishedAt.getTime() - task.getStartedAt().getTime()));
|
|
|
|
|
+ }
|
|
|
|
|
+ generationMapper.updateById(task);
|
|
|
|
|
+ recovered++;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ log.warn("校审AI启动恢复:已将 {} 条遗留 running 任务置为 failed", recovered);
|
|
|
|
|
+ return recovered;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|