fix: 启动时更新未完成的任务为失败
This commit is contained in:
@@ -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<Task> {
|
||||
@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();
|
||||
}
|
||||
|
||||
@@ -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<Task> {
|
||||
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<String, Object> params) {
|
||||
var template = taskTemplateService.detail(templateId);
|
||||
var context = new TaskMonitorNodes.TaskMonitorContext(template);
|
||||
|
||||
Reference in New Issue
Block a user