ruanwenjun commented on code in PR #16327: URL: https://github.com/apache/dolphinscheduler/pull/16327#discussion_r1729040623
########## dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/executor/workflow/RecoverSuspendedWorkflowInstanceExecutorDelegate.java: ########## @@ -0,0 +1,87 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.dolphinscheduler.api.executor.workflow; + +import org.apache.dolphinscheduler.api.exceptions.ServiceException; +import org.apache.dolphinscheduler.common.enums.CommandType; +import org.apache.dolphinscheduler.dao.entity.Command; +import org.apache.dolphinscheduler.dao.entity.ProcessInstance; +import org.apache.dolphinscheduler.dao.entity.User; +import org.apache.dolphinscheduler.dao.repository.CommandDao; + +import java.util.Date; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class RecoverSuspendedWorkflowInstanceExecutorDelegate + implements + IExecutorDelegate<RecoverSuspendedWorkflowInstanceExecutorDelegate.RecoverSuspendedWorkflowInstanceOperation, Void> { + + @Autowired + private CommandDao commandDao; + + @Override + public Void execute(RecoverSuspendedWorkflowInstanceOperation workflowInstanceControlRequest) { Review Comment: Since the execute should have a response we need to use `Void` here. ```java public interface IExecutorDelegate<T, R> { R execute(T t); } ``` ########## dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/executor/workflow/PauseWorkflowInstanceExecutorDelegate.java: ########## @@ -0,0 +1,128 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.dolphinscheduler.api.executor.workflow; + +import org.apache.dolphinscheduler.api.exceptions.ServiceException; +import org.apache.dolphinscheduler.common.enums.WorkflowExecutionStatus; +import org.apache.dolphinscheduler.dao.entity.ProcessInstance; +import org.apache.dolphinscheduler.dao.entity.User; +import org.apache.dolphinscheduler.dao.repository.ProcessInstanceDao; +import org.apache.dolphinscheduler.extract.base.client.SingletonJdkDynamicRpcClientProxyFactory; +import org.apache.dolphinscheduler.extract.master.IWorkflowInstanceController; +import org.apache.dolphinscheduler.extract.master.transportor.WorkflowInstancePauseRequest; +import org.apache.dolphinscheduler.extract.master.transportor.WorkflowInstancePauseResponse; + +import lombok.extern.slf4j.Slf4j; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Slf4j +@Component +public class PauseWorkflowInstanceExecutorDelegate + implements + IExecutorDelegate<PauseWorkflowInstanceExecutorDelegate.PauseWorkflowInstanceOperation, Void> { + + @Autowired + private ProcessInstanceDao workflowInstanceDao; + + @Override + public Void execute(PauseWorkflowInstanceOperation workflowInstanceControlRequest) { + final ProcessInstance workflowInstance = workflowInstanceControlRequest.workflowInstance; + exceptionIfWorkflowInstanceCannotPause(workflowInstance); + if (ifWorkflowInstanceCanDirectPauseInDB(workflowInstance)) { + directPauseInDB(workflowInstance); + } else { + pauseInMaster(workflowInstance); + } + return null; + } + + private void exceptionIfWorkflowInstanceCannotPause(ProcessInstance workflowInstance) { + WorkflowExecutionStatus workflowInstanceState = workflowInstance.getState(); + if (workflowInstanceState.canPause()) { + return; + } + throw new ServiceException( + "The workflow instance: " + workflowInstance.getName() + " status is " + workflowInstanceState + + ", can not pause"); + } + + private boolean ifWorkflowInstanceCanDirectPauseInDB(ProcessInstance workflowInstance) { + return workflowInstance.getState().canDirectPauseInDB(); + } + + private void directPauseInDB(ProcessInstance workflowInstance) { + workflowInstanceDao.updateWorkflowInstanceState( + workflowInstance.getId(), + workflowInstance.getState(), + WorkflowExecutionStatus.PAUSE); + log.info("Update workflow instance {} state from: {} to {} success", + workflowInstance.getName(), + workflowInstance.getState().name(), + WorkflowExecutionStatus.PAUSE.name()); + } + + private void pauseInMaster(ProcessInstance workflowInstance) { + try { + final WorkflowInstancePauseResponse pauseResponse = SingletonJdkDynamicRpcClientProxyFactory + .withService(IWorkflowInstanceController.class) + .withHost(workflowInstance.getHost()) + .pauseWorkflowInstance(new WorkflowInstancePauseRequest(workflowInstance.getId())); + + if (pauseResponse != null && pauseResponse.isSuccess()) { + log.info("WorkflowInstance: {} pause success", workflowInstance.getName()); + } else { + throw new ServiceException( + "WorkflowInstance: " + workflowInstance.getName() + " stop failed: " + pauseResponse); Review Comment: Done -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
