From 0a249161dda3c607acd1a36f700278a88e773efc Mon Sep 17 00:00:00 2001 From: dengqichen Date: Wed, 25 Dec 2024 19:00:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=A7=E5=A3=B0=E9=81=93=E6=92=92=E6=97=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../workflow/constants/WorkFlowConstants.java | 17 +++++++ .../workflow/delegate/BaseNodeDelegate.java | 49 ++++++++++--------- .../workflow/delegate/DeployNodeDelegate.java | 8 +-- .../delegate/NotificationNodeDelegate.java | 12 ++--- .../execution/GlobalExecutionListener.java | 15 +++++- 5 files changed, 67 insertions(+), 34 deletions(-) diff --git a/backend/src/main/java/com/qqchen/deploy/backend/workflow/constants/WorkFlowConstants.java b/backend/src/main/java/com/qqchen/deploy/backend/workflow/constants/WorkFlowConstants.java index d81930c7..7ac1a9d1 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/workflow/constants/WorkFlowConstants.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/workflow/constants/WorkFlowConstants.java @@ -11,4 +11,21 @@ public interface WorkFlowConstants { String SEQUENCE_FLOW_ERROR_PREFIX = "SEQUENCE_FLOW_ERROR_"; String ASYNC_CONTINUATION = "async-continuation"; + + String WORKFLOW_PREVIOUS_NODE_EXECUTION_STATUS_VARIABLE_NAME = "PREVIOUS_NODE_EXECUTION_STATUS"; + + /** + * 执行状态:成功 + */ + String WORKFLOW_NODE_EXECUTION_STATUS_SUCCESS = "SUCCESS"; + + /** + * 执行状态:失败 + */ + String WORKFLOW_NODE_EXECUTION_STATUS_FAILURE = "FAILURE"; + + /** + * 执行状态:中止 + */ + String WORKFLOW_NODE_EXECUTION_STATUS_ABORTED = "ABORTED"; } diff --git a/backend/src/main/java/com/qqchen/deploy/backend/workflow/delegate/BaseNodeDelegate.java b/backend/src/main/java/com/qqchen/deploy/backend/workflow/delegate/BaseNodeDelegate.java index a6571c9f..c89327bd 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/workflow/delegate/BaseNodeDelegate.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/workflow/delegate/BaseNodeDelegate.java @@ -2,12 +2,20 @@ package com.qqchen.deploy.backend.workflow.delegate; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import com.qqchen.deploy.backend.workflow.constants.WorkFlowConstants; import lombok.extern.slf4j.Slf4j; +import org.flowable.engine.delegate.BpmnError; import org.flowable.engine.delegate.DelegateExecution; import org.flowable.engine.delegate.JavaDelegate; import org.flowable.common.engine.api.delegate.Expression; import org.springframework.beans.factory.annotation.Autowired; +import static com.qqchen.deploy.backend.workflow.constants.WorkFlowConstants.WORKFLOW_NODE_EXECUTION_STATUS_FAILURE; +import static com.qqchen.deploy.backend.workflow.constants.WorkFlowConstants.WORKFLOW_NODE_EXECUTION_STATUS_SUCCESS; +import static com.qqchen.deploy.backend.workflow.constants.WorkFlowConstants.WORKFLOW_PREVIOUS_NODE_EXECUTION_STATUS_VARIABLE_NAME; + +import java.util.Optional; + /** * 任务委派者基类 * 负责处理panelVariables和localVariables的转换和注入 @@ -18,25 +26,7 @@ import org.springframework.beans.factory.annotation.Autowired; @Slf4j public abstract class BaseNodeDelegate implements JavaDelegate { - /** - * 执行状态变量名 - */ - protected static final String EXECUTION_STATUS = "executionStatus"; - /** - * 执行状态:成功 - */ - protected static final String STATUS_SUCCESS = "success"; - - /** - * 执行状态:失败 - */ - protected static final String STATUS_FAILURE = "failure"; - - /** - * 执行状态:中止 - */ - protected static final String STATUS_ABORTED = "aborted"; @Autowired private ObjectMapper objectMapper; @@ -68,15 +58,15 @@ public abstract class BaseNodeDelegate implements JavaDelegate { executeInternal(execution, panelVars, localVars); // 如果节点没有设置状态,默认设置成功状态 - if (execution.getVariable(EXECUTION_STATUS) == null) { - setExecutionStatus(execution, STATUS_SUCCESS); + String status = getExecutionStatus(execution); + if (status == null) { + setExecutionStatus(execution, WORKFLOW_NODE_EXECUTION_STATUS_SUCCESS); } } catch (Exception e) { // 设置失败状态 - setExecutionStatus(execution, STATUS_FAILURE); + setExecutionStatus(execution, WORKFLOW_NODE_EXECUTION_STATUS_FAILURE); log.error("Task execution failed", e); - throw new RuntimeException("Task execution failed: " + e.getMessage(), e); } } @@ -87,7 +77,7 @@ public abstract class BaseNodeDelegate implements JavaDelegate { * @param status 状态值 */ protected void setExecutionStatus(DelegateExecution execution, String status) { - execution.setVariable(EXECUTION_STATUS, status); + execution.setVariable(WORKFLOW_PREVIOUS_NODE_EXECUTION_STATUS_VARIABLE_NAME, status); log.debug("Set execution status: {}", status); } @@ -113,4 +103,17 @@ public abstract class BaseNodeDelegate implements JavaDelegate { * @param localVariables 转换后的Local变量 */ protected abstract void executeInternal(DelegateExecution execution, P panelVariables, L localVariables); + + /** + * 获取执行状态 + * + * @param execution 执行上下文 + * @return 执行状态,如果不存在则返回null + */ + protected String getExecutionStatus(DelegateExecution execution) { + return Optional.ofNullable(execution.getVariables()) + .map(vars -> vars.get(WORKFLOW_PREVIOUS_NODE_EXECUTION_STATUS_VARIABLE_NAME)) + .map(Object::toString) + .orElse(null); + } } \ No newline at end of file diff --git a/backend/src/main/java/com/qqchen/deploy/backend/workflow/delegate/DeployNodeDelegate.java b/backend/src/main/java/com/qqchen/deploy/backend/workflow/delegate/DeployNodeDelegate.java index e81f4eb3..48e22b58 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/workflow/delegate/DeployNodeDelegate.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/workflow/delegate/DeployNodeDelegate.java @@ -56,10 +56,10 @@ public class DeployNodeDelegate extends BaseNodeDelegate entity = new HttpEntity<>(format, headers); -// restTemplate.exchange( -// WX_HOOK_API, -// HttpMethod.POST, -// entity, -// String.class -// ); + restTemplate.exchange( + WX_HOOK_API, + HttpMethod.POST, + entity, + String.class + ); } } diff --git a/backend/src/main/java/com/qqchen/deploy/backend/workflow/listener/execution/GlobalExecutionListener.java b/backend/src/main/java/com/qqchen/deploy/backend/workflow/listener/execution/GlobalExecutionListener.java index 5f50ccac..8c568004 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/workflow/listener/execution/GlobalExecutionListener.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/workflow/listener/execution/GlobalExecutionListener.java @@ -3,6 +3,7 @@ package com.qqchen.deploy.backend.workflow.listener.execution; import com.qqchen.deploy.backend.workflow.enums.WorkflowNodeInstanceStatusEnums; import com.qqchen.deploy.backend.workflow.event.WorkflowNodeInstanceStatusChangeEvent; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import org.flowable.bpmn.model.FlowElement; import org.flowable.engine.delegate.DelegateExecution; import org.flowable.engine.delegate.ExecutionListener; @@ -12,6 +13,10 @@ import org.springframework.stereotype.Component; import jakarta.annotation.Resource; import java.time.LocalDateTime; +import java.util.Map; +import java.util.Optional; + +import static com.qqchen.deploy.backend.workflow.constants.WorkFlowConstants.WORKFLOW_PREVIOUS_NODE_EXECUTION_STATUS_VARIABLE_NAME; @Slf4j @Component("globalNodeExecutionListener") @@ -41,7 +46,15 @@ public class GlobalExecutionListener implements ExecutionListener { startTime = now; break; case ExecutionListener.EVENTNAME_END: - status = WorkflowNodeInstanceStatusEnums.COMPLETED; + String nodeExecutionStatus = Optional.ofNullable(execution.getVariables()) + .map(vars -> vars.get(WORKFLOW_PREVIOUS_NODE_EXECUTION_STATUS_VARIABLE_NAME)) + .map(Object::toString) + .orElse(null); + if (StringUtils.isEmpty(nodeExecutionStatus)) { + status = WorkflowNodeInstanceStatusEnums.COMPLETED; + } else { + status = WorkflowNodeInstanceStatusEnums.FAILED; + } endTime = now; break; default: