diff --git a/leopard-server/src/main/java/com/lanyuanxiaoyao/leopard/server/repository/TaskRepository.java b/leopard-server/src/main/java/com/lanyuanxiaoyao/leopard/server/repository/TaskRepository.java index ae95757..e67df5d 100644 --- a/leopard-server/src/main/java/com/lanyuanxiaoyao/leopard/server/repository/TaskRepository.java +++ b/leopard-server/src/main/java/com/lanyuanxiaoyao/leopard/server/repository/TaskRepository.java @@ -2,6 +2,8 @@ package com.lanyuanxiaoyao.leopard.server.repository; import com.lanyuanxiaoyao.leopard.server.entity.Task; import com.lanyuanxiaoyao.service.template.repository.SimpleRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; /** @@ -10,4 +12,7 @@ import org.springframework.stereotype.Repository; */ @Repository public interface TaskRepository extends SimpleRepository { + @Modifying + @Query("update Task task set task.status = com.lanyuanxiaoyao.leopard.server.entity.Task.Status.FAILURE where task.status = com.lanyuanxiaoyao.leopard.server.entity.Task.Status.RUNNING") + void updateAllRunningTaskToFailure(); } diff --git a/leopard-server/src/main/java/com/lanyuanxiaoyao/leopard/server/service/TaskService.java b/leopard-server/src/main/java/com/lanyuanxiaoyao/leopard/server/service/TaskService.java index 47bbcb1..17566e3 100644 --- a/leopard-server/src/main/java/com/lanyuanxiaoyao/leopard/server/service/TaskService.java +++ b/leopard-server/src/main/java/com/lanyuanxiaoyao/leopard/server/service/TaskService.java @@ -5,8 +5,11 @@ import com.lanyuanxiaoyao.leopard.server.repository.TaskRepository; import com.lanyuanxiaoyao.leopard.server.service.task.TaskMonitorNodes; import com.lanyuanxiaoyao.service.template.service.SimpleServiceSupport; import com.yomahub.liteflow.core.FlowExecutor; +import jakarta.transaction.Transactional; import java.util.Map; import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.context.event.ApplicationReadyEvent; +import org.springframework.context.event.EventListener; import org.springframework.stereotype.Service; /** @@ -16,15 +19,24 @@ import org.springframework.stereotype.Service; @Slf4j @Service public class TaskService extends SimpleServiceSupport { + private final TaskRepository taskRepository; private final TaskTemplateService taskTemplateService; private final FlowExecutor flowExecutor; public TaskService(TaskRepository repository, TaskTemplateService taskTemplateService, @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") FlowExecutor flowExecutor) { super(repository); + this.taskRepository = repository; this.taskTemplateService = taskTemplateService; this.flowExecutor = flowExecutor; } + @Transactional(rollbackOn = Throwable.class) + @EventListener(ApplicationReadyEvent.class) + public void onApplicationReady() { + log.warn("更新所有未完成的任务状态为失败"); + taskRepository.updateAllRunningTaskToFailure(); + } + public void execute(Long templateId, Map params) { var template = taskTemplateService.detail(templateId); var context = new TaskMonitorNodes.TaskMonitorContext(template);