From 22b5456752fd3b240ab272b43fc504400ed26c11 Mon Sep 17 00:00:00 2001 From: lanyuanxiaoyao Date: Sun, 7 Sep 2025 12:21:13 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=90=AF=E5=8A=A8=E6=97=B6=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E6=9C=AA=E5=AE=8C=E6=88=90=E7=9A=84=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E4=B8=BA=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leopard/server/repository/TaskRepository.java | 5 +++++ .../leopard/server/service/TaskService.java | 12 ++++++++++++ 2 files changed, 17 insertions(+) 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);