Skip to content

Commit 4748df9

Browse files
caishunfengcaishunfeng
and
caishunfeng
authored
add delete workflow instance when delete process definition (#8131)
Co-authored-by: caishunfeng <[email protected]>
1 parent 05545e1 commit 4748df9

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ProcessDefinitionServiceImpl.java

+33
Original file line numberDiff line numberDiff line change
@@ -709,10 +709,43 @@ public Map<String, Object> deleteProcessDefinitionByCode(User loginUser, long pr
709709
if (deleteRelation == 0) {
710710
logger.warn("The process definition has not relation, it will be delete successfully");
711711
}
712+
713+
try {
714+
syncDeleteWorkflowInstanceByCode(processDefinition.getCode());
715+
} catch (Exception e) {
716+
logger.error("delete workflow instance error", e);
717+
}
718+
712719
putMsg(result, Status.SUCCESS);
713720
return result;
714721
}
715722

723+
/**
724+
* delete workflow instance by processDefinitionCode
725+
* 1.delete processInstances
726+
* 2.delete subWorkProcesses
727+
* 3.delete processMap
728+
* 4.delete taskInstances
729+
*
730+
* todo delete syncly may take a long time when many processInstance
731+
* @param processDefinitionCode
732+
*/
733+
private void syncDeleteWorkflowInstanceByCode(long processDefinitionCode) {
734+
int pageSize = 100;
735+
while (true) {
736+
List<ProcessInstance> deleteProcessInstances = processInstanceService.queryByProcessDefineCode(processDefinitionCode, pageSize);
737+
if (CollectionUtils.isEmpty(deleteProcessInstances)) {
738+
break;
739+
}
740+
for (ProcessInstance deleteProcessInstance : deleteProcessInstances) {
741+
processService.deleteWorkProcessInstanceById(deleteProcessInstance.getId());
742+
processService.deleteAllSubWorkProcessByParentId(deleteProcessInstance.getId());
743+
processService.deleteWorkProcessMapByParentId(deleteProcessInstance.getId());
744+
processService.deleteWorkTaskInstanceByProcessInstanceId(deleteProcessInstance.getId());
745+
}
746+
}
747+
}
748+
716749
/**
717750
* release process definition: online / offline
718751
*

dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/process/ProcessService.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
package org.apache.dolphinscheduler.service.process;
1919

20-
import static java.util.stream.Collectors.toSet;
2120
import static org.apache.dolphinscheduler.common.Constants.CMDPARAM_COMPLEMENT_DATA_END_DATE;
2221
import static org.apache.dolphinscheduler.common.Constants.CMDPARAM_COMPLEMENT_DATA_START_DATE;
2322
import static org.apache.dolphinscheduler.common.Constants.CMD_PARAM_EMPTY_SUB_PROCESS;
@@ -28,6 +27,8 @@
2827
import static org.apache.dolphinscheduler.common.Constants.CMD_PARAM_SUB_PROCESS_PARENT_INSTANCE_ID;
2928
import static org.apache.dolphinscheduler.common.Constants.LOCAL_PARAMS;
3029

30+
import static java.util.stream.Collectors.toSet;
31+
3132
import org.apache.dolphinscheduler.common.Constants;
3233
import org.apache.dolphinscheduler.common.enums.AuthorizationType;
3334
import org.apache.dolphinscheduler.common.enums.CommandType;

0 commit comments

Comments
 (0)