Jelajahi Sumber

fix: stop nested workflow

Echo009 3 tahun lalu
induk
melakukan
8488a10465

+ 20 - 4
powerjob-server/powerjob-server-core/src/main/java/tech/powerjob/server/core/workflow/WorkflowInstanceService.java

@@ -45,7 +45,27 @@ public class WorkflowInstanceService {
     private WorkflowInstanceManager workflowInstanceManager;
     @Resource
     private WorkflowInfoRepository workflowInfoRepository;
+    @Resource
+    private WorkflowInstanceService self;
 
+    /**
+     * 停止工作流实例(入口)
+     *
+     * @param wfInstanceId 工作流实例ID
+     * @param appId        所属应用ID
+     */
+    public void stopWorkflowInstanceEntrance(Long wfInstanceId, Long appId) {
+        WorkflowInstanceInfoDO wfInstance = fetchWfInstance(wfInstanceId, appId);
+        if (!WorkflowInstanceStatus.GENERALIZED_RUNNING_STATUS.contains(wfInstance.getStatus())) {
+            throw new PowerJobException("workflow instance already stopped");
+        }
+        // 如果这是一个被嵌套的工作流,则终止父工作流
+        if (wfInstance.getParentWfInstanceId() != null) {
+            self.stopWorkflowInstance(wfInstance.getParentWfInstanceId(), appId);
+            return;
+        }
+        self.stopWorkflowInstance(wfInstanceId, appId);
+    }
 
     /**
      * 停止工作流实例
@@ -60,10 +80,6 @@ public class WorkflowInstanceService {
         if (!WorkflowInstanceStatus.GENERALIZED_RUNNING_STATUS.contains(wfInstance.getStatus())) {
             throw new PowerJobException("workflow instance already stopped");
         }
-        // 先终止父工作流
-        if (wfInstance.getParentWfInstanceId() != null) {
-            stopWorkflowInstance(wfInstance.getParentWfInstanceId(),appId);
-        }
         // 停止所有已启动且未完成的服务
         PEWorkflowDAG dag = JSON.parseObject(wfInstance.getDag(), PEWorkflowDAG.class);
         // 遍历所有节点,终止正在运行的

+ 1 - 1
powerjob-server/powerjob-server-starter/src/main/java/tech/powerjob/server/web/controller/OpenAPIController.java

@@ -204,7 +204,7 @@ public class OpenAPIController {
 
     @PostMapping(OpenAPIConstant.STOP_WORKFLOW_INSTANCE)
     public ResultDTO<Void> stopWorkflowInstance(Long wfInstanceId, Long appId) {
-        workflowInstanceService.stopWorkflowInstance(wfInstanceId, appId);
+        workflowInstanceService.stopWorkflowInstanceEntrance(wfInstanceId, appId);
         return ResultDTO.success(null);
     }
 

+ 1 - 1
powerjob-server/powerjob-server-starter/src/main/java/tech/powerjob/server/web/controller/WorkflowInstanceController.java

@@ -39,7 +39,7 @@ public class WorkflowInstanceController {
 
     @GetMapping("/stop")
     public ResultDTO<Void> stopWfInstance(Long wfInstanceId, Long appId) {
-        workflowInstanceService.stopWorkflowInstance(wfInstanceId, appId);
+        workflowInstanceService.stopWorkflowInstanceEntrance(wfInstanceId, appId);
         return ResultDTO.success(null);
     }