From 412a7e5c91a2b409fe7f0eb410a3c5db895e6bc6 Mon Sep 17 00:00:00 2001 From: dengqichen Date: Tue, 10 Dec 2024 12:27:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=8E=89=E6=89=80=E6=9C=89?= =?UTF-8?q?=E6=B2=A1=E7=94=A8=E7=9A=84=E5=B7=A5=E4=BD=9C=E6=B5=81=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E7=9A=84=E7=B1=BB=EF=BC=8C=E9=87=8D=E6=96=B0=E5=BC=80?= =?UTF-8?q?=E5=8F=91=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../workflow/delegate/ShellTaskDelegate.java | 7 - .../workflow/dto/WorkflowExecutionDTO.java | 15 -- .../impl/WorkflowDefinitionServiceImpl.java | 229 ++++++++---------- 3 files changed, 95 insertions(+), 156 deletions(-) diff --git a/backend/src/main/java/com/qqchen/deploy/backend/workflow/delegate/ShellTaskDelegate.java b/backend/src/main/java/com/qqchen/deploy/backend/workflow/delegate/ShellTaskDelegate.java index 8037e686..03199832 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/workflow/delegate/ShellTaskDelegate.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/workflow/delegate/ShellTaskDelegate.java @@ -126,10 +126,6 @@ public class ShellTaskDelegate implements JavaDelegate { output.append(line).append("\n"); } log.info("Shell output: {}", line); - // 使用RuntimeService更新变量 - runtimeService.setVariable(execution.getProcessInstanceId(), - executionId + "_shellOutput", output.toString()); - Thread.sleep(500); } } catch (Exception e) { log.error("Error reading process output", e); @@ -146,9 +142,6 @@ public class ShellTaskDelegate implements JavaDelegate { error.append(line).append("\n"); } log.error("Shell error: {}", line); - // 使用RuntimeService更新变量 - runtimeService.setVariable(execution.getProcessInstanceId(), - executionId + "_shellError", error.toString()); } } catch (IOException e) { log.error("Error reading process error", e); diff --git a/backend/src/main/java/com/qqchen/deploy/backend/workflow/dto/WorkflowExecutionDTO.java b/backend/src/main/java/com/qqchen/deploy/backend/workflow/dto/WorkflowExecutionDTO.java index c51f6799..6d6deb81 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/workflow/dto/WorkflowExecutionDTO.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/workflow/dto/WorkflowExecutionDTO.java @@ -104,20 +104,5 @@ public class WorkflowExecutionDTO { * 结束时间 */ private Date endTime; - - /** - * Shell任务的输出(如果是 SHELL_TASK 类型) - */ - private String output; - - /** - * Shell任务的错误信息(如果是 SHELL_TASK 类型) - */ - private String error; - - /** - * Shell任务的退出码(如果是 SHELL_TASK 类型) - */ - private Integer exitCode; } } diff --git a/backend/src/main/java/com/qqchen/deploy/backend/workflow/service/impl/WorkflowDefinitionServiceImpl.java b/backend/src/main/java/com/qqchen/deploy/backend/workflow/service/impl/WorkflowDefinitionServiceImpl.java index bf142ef9..ff9df6b4 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/workflow/service/impl/WorkflowDefinitionServiceImpl.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/workflow/service/impl/WorkflowDefinitionServiceImpl.java @@ -10,6 +10,8 @@ import com.qqchen.deploy.backend.workflow.repository.IWorkflowDefinitionReposito import com.qqchen.deploy.backend.workflow.service.IWorkflowDefinitionService; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; +import org.flowable.bpmn.model.SequenceFlow; +import org.flowable.bpmn.model.StartEvent; import org.flowable.engine.HistoryService; import org.flowable.engine.RepositoryService; import org.flowable.engine.RuntimeService; @@ -17,6 +19,7 @@ import org.flowable.engine.history.HistoricActivityInstance; import org.flowable.engine.history.HistoricProcessInstance; import org.flowable.engine.repository.Deployment; import org.flowable.bpmn.model.BpmnModel; +import org.flowable.engine.runtime.Execution; import org.flowable.engine.runtime.ProcessInstance; import org.flowable.bpmn.model.Process; import org.flowable.bpmn.model.FlowElement; @@ -133,11 +136,14 @@ public class WorkflowDefinitionServiceImpl extends BaseServiceImpl { - log.info("节点信息 - ID: {}, 名称: {}, 类型: {}", - element.getId(), - element.getName(), - element.getClass().getSimpleName()); - }); - // 3. 获取所有历史活动实例 List historicActivities = historyService.createHistoricActivityInstanceQuery() .processInstanceId(processInstanceId) @@ -172,34 +169,38 @@ public class WorkflowDefinitionServiceImpl extends BaseServiceImpl { - log.info("历史活动 - ID: {}, 名称: {}, 类型: {}, 开始时间: {}, 结束时间: {}", + for (HistoricActivityInstance activity : historicActivities) { + log.info("节点ID: {}, 名称: {}, 类型: {}, 开始时间: {}, 结束时间: {}, 执行ID: {}", activity.getActivityId(), activity.getActivityName(), activity.getActivityType(), activity.getStartTime(), - activity.getEndTime()); - }); - - // 4. 获取当前活动的节点 - List activeActivityIds = new ArrayList<>(); - if (runningInstance != null) { - activeActivityIds = runtimeService.getActiveActivityIds(processInstanceId); - log.info("=== 当前活动节点 ==="); - log.info("活动节点IDs: {}", activeActivityIds); + activity.getEndTime(), + activity.getExecutionId()); } - // 5. 获取所有流程变量 - List variables = historyService.createHistoricVariableInstanceQuery() - .processInstanceId(processInstanceId) - .list(); - - log.info("=== 流程变量信息 ==="); - Map variableMap = new HashMap<>(); - variables.forEach(variable -> { - variableMap.put(variable.getVariableName(), variable.getValue()); - log.info("变量 - 名称: {}, 值: {}", variable.getVariableName(), variable.getValue()); - }); + // 4. 获取当前正在执行的活动节点 + List activeActivityIds = new ArrayList<>(); + if (runningInstance != null) { + List executions = runtimeService.createExecutionQuery() + .processInstanceId(processInstanceId) + .list(); + + for (Execution execution : executions) { + if (execution.getActivityId() != null) { + activeActivityIds.add(execution.getActivityId()); + } + } + log.info("当前活动节点IDs: {}", activeActivityIds); + } + + // 5. 构建历史活动实例映射 + Map historicActivityMap = historicActivities.stream() + .collect(Collectors.toMap( + HistoricActivityInstance::getActivityId, + activity -> activity, + (existing, replacement) -> existing + )); // 6. 构建执行状态DTO WorkflowExecutionDTO executionDTO = new WorkflowExecutionDTO(); @@ -210,128 +211,88 @@ public class WorkflowDefinitionServiceImpl extends BaseServiceImpl historicActivityMap = historicActivities.stream() - .collect(Collectors.toMap( - HistoricActivityInstance::getActivityId, - activity -> activity, - (existing, replacement) -> existing - )); - - // 8. 获取所有流程节点并构建阶段列表 + // 7. 构建阶段列表 List stages = new ArrayList<>(); - Collection flowElements = process.getFlowElements(); - - // 记录已执行过的节点ID - Set executedActivityIds = historicActivities.stream() - .map(HistoricActivityInstance::getActivityId) - .collect(Collectors.toSet()); + List flowElements = process.getFlowElements().stream() + .filter(element -> !(element instanceof SequenceFlow)) + .collect(Collectors.toList()); - // 获取所有节点的列表(按照流程定义的顺序) - List orderedElements = new ArrayList<>(); + log.info("=== 开始构建节点状态 ==="); for (FlowElement element : flowElements) { - if (element instanceof Task || element instanceof Event || element instanceof ServiceTask) { - orderedElements.add(element); - } - } - - // 遍历所有节点 - for (int i = 0; i < orderedElements.size(); i++) { - FlowElement element = orderedElements.get(i); WorkflowExecutionDTO.StageDTO stage = new WorkflowExecutionDTO.StageDTO(); stage.setId(element.getId()); stage.setName(element.getName()); - - // 设置节点类型 - if (element instanceof ServiceTask) { - stage.setType("serviceTask"); - } else if (element instanceof Event) { - stage.setType("startEvent"); - if (element.getId().equals("end")) { - stage.setType("endEvent"); - } - } else { - stage.setType(element.getClass().getSimpleName()); - } + stage.setType(element.getClass().getSimpleName()); - // 获取历史活动实例 HistoricActivityInstance historicActivity = historicActivityMap.get(element.getId()); - - // 设置开始和结束时间 - if (historicActivity != null) { - stage.setStartTime(historicActivity.getStartTime()); - stage.setEndTime(historicActivity.getEndTime()); - } - - // 设置节点状态 - if (historicActivity != null) { - if (historicActivity.getEndTime() != null) { - // 已完成的节点 - stage.setStatus(historicActivity.getDeleteReason() == null ? - WorkflowInstanceStatus.COMPLETED : WorkflowInstanceStatus.FAILED); - } else if (activeActivityIds.contains(element.getId())) { - // 正在执行的节点 - stage.setStatus(WorkflowInstanceStatus.RUNNING); - } else { - // 已开始但未完成的节点 - stage.setStatus(WorkflowInstanceStatus.RUNNING); - } - } else { - // 未执行的节点 - if (i == 0 && executedActivityIds.isEmpty()) { - // 如果是第一个节点且没有任何节点执行过,设置为RUNNING - stage.setStatus(WorkflowInstanceStatus.RUNNING); - } else if (i > 0 && stages.get(i-1).getStatus() == WorkflowInstanceStatus.COMPLETED) { - // 如果前一个节点已完成,当前节点设置为RUNNING - stage.setStatus(WorkflowInstanceStatus.RUNNING); - } else { - // 其他情况设置为NOT_STARTED - stage.setStatus(WorkflowInstanceStatus.NOT_STARTED); - } - } - - // 如果是Shell任务,获取执行结果 - if (element instanceof ServiceTask) { - ServiceTask serviceTask = (ServiceTask) element; - if ("shell".equals(serviceTask.getType()) && historicActivity != null) { - String executionId = historicActivity.getExecutionId(); - stage.setOutput((String) variableMap.get(executionId + "_shellOutput")); - stage.setError((String) variableMap.get(executionId + "_shellError")); - stage.setExitCode((Integer) variableMap.get(executionId + "_exitCode")); - } - } + + // 确定节点状态 + determineStageStatus(stage, element, historicActivity, activeActivityIds); + + log.info("节点[{}] - 名称: {}, 类型: {}, 状态: {}, 开始时间: {}, 结束时间: {}", + stage.getId(), + stage.getName(), + stage.getType(), + stage.getStatus(), + stage.getStartTime(), + stage.getEndTime()); stages.add(stage); } executionDTO.setStages(stages); - - // 10. 设置当前活动节点 - if (!activeActivityIds.isEmpty()) { - String currentActivityId = activeActivityIds.get(0); - FlowElement currentElement = process.getFlowElement(currentActivityId); - if (currentElement != null) { - executionDTO.setCurrentActivityId(currentActivityId); - executionDTO.setCurrentActivityName(currentElement.getName()); - executionDTO.setCurrentActivityType(currentElement.getClass().getSimpleName()); - } - } - return executionDTO; } catch (Exception e) { - log.error("Failed to get workflow execution: {}", processInstanceId, e); - throw new RuntimeException("Failed to get workflow execution", e); + log.error("获取工作流执行状态失败", e); + throw new RuntimeException("Failed to get workflow execution status", e); + } + } + + /** + * 确定节点状态 + */ + private void determineStageStatus( + WorkflowExecutionDTO.StageDTO stage, + FlowElement element, + HistoricActivityInstance historicActivity, + List activeActivityIds + ) { + if (historicActivity == null) { + // 节点未执行过 + stage.setStatus(WorkflowInstanceStatus.NOT_STARTED); + stage.setStartTime(null); + stage.setEndTime(null); + return; + } + + // 设置时间 + stage.setStartTime(historicActivity.getStartTime()); + stage.setEndTime(historicActivity.getEndTime()); + + // 确定状态 + if (historicActivity.getEndTime() != null) { + // 节点已结束 + stage.setStatus(historicActivity.getDeleteReason() == null ? + WorkflowInstanceStatus.COMPLETED : WorkflowInstanceStatus.FAILED); + } else if (element instanceof StartEvent) { + // 开始节点特殊处理:有开始时间就视为完成 + stage.setStatus(WorkflowInstanceStatus.COMPLETED); + } else if (activeActivityIds.contains(element.getId())) { + // 节点正在执行 + stage.setStatus(WorkflowInstanceStatus.RUNNING); + } else { + // 其他情况:节点已开始但未完成,且不在当前活动节点列表中 + stage.setStatus(WorkflowInstanceStatus.NOT_STARTED); } }