From 9a89d59485329d8ee4d67086d8aa7220db548fcb Mon Sep 17 00:00:00 2001 From: ligang Date: Mon, 13 May 2019 14:03:57 +0800 Subject: [PATCH 1/2] add batchDeleteProcessInstanceByIds --- .../controller/ProcessInstanceController.java | 26 +++++++++++ .../java/cn/escheduler/api/enums/Status.java | 1 + .../api/service/ProcessInstanceService.java | 43 +++++++++++++++++++ .../service/ProcessInstanceServiceTest.java | 12 ++++++ 4 files changed, 82 insertions(+) diff --git a/escheduler-api/src/main/java/cn/escheduler/api/controller/ProcessInstanceController.java b/escheduler-api/src/main/java/cn/escheduler/api/controller/ProcessInstanceController.java index 2ae21ffb89..b05aa656f1 100644 --- a/escheduler-api/src/main/java/cn/escheduler/api/controller/ProcessInstanceController.java +++ b/escheduler-api/src/main/java/cn/escheduler/api/controller/ProcessInstanceController.java @@ -282,4 +282,30 @@ public class ProcessInstanceController extends BaseController{ return error(Status.ENCAPSULATION_PROCESS_INSTANCE_GANTT_STRUCTURE_ERROR.getCode(),ENCAPSULATION_PROCESS_INSTANCE_GANTT_STRUCTURE_ERROR.getMsg()); } } + + /** + * batch delete process instance by ids, at the same time, + * delete task instance and their mapping relation data + * + * @param loginUser + * @param projectName + * @param processInstanceIds + * @return + */ + @GetMapping(value="/batch-delete") + @ResponseStatus(HttpStatus.OK) + public Result batchDeleteProcessInstanceByIds(@RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @PathVariable String projectName, + @RequestParam("processInstanceIds") String processInstanceIds + ){ + try{ + logger.info("delete process instance by ids, login user:{}, project name:{}, process instance ids :{}", + loginUser.getUserName(), projectName, processInstanceIds); + Map result = processInstanceService.batchDeleteProcessInstanceByIds(loginUser, projectName, processInstanceIds); + return returnDataList(result); + }catch (Exception e){ + logger.error(BATCH_DELETE_PROCESS_INSTANCE_BY_IDS_ERROR.getMsg(),e); + return error(Status.BATCH_DELETE_PROCESS_INSTANCE_BY_IDS_ERROR.getCode(), Status.BATCH_DELETE_PROCESS_INSTANCE_BY_IDS_ERROR.getMsg()); + } + } } diff --git a/escheduler-api/src/main/java/cn/escheduler/api/enums/Status.java b/escheduler-api/src/main/java/cn/escheduler/api/enums/Status.java index bda65de9cd..a4a44bd904 100644 --- a/escheduler-api/src/main/java/cn/escheduler/api/enums/Status.java +++ b/escheduler-api/src/main/java/cn/escheduler/api/enums/Status.java @@ -160,6 +160,7 @@ public enum Status { NAME_EXIST(10135, "name {0} already exists"), SAVE_ERROR(10136, "save error"), DELETE_PROJECT_ERROR_DEFINES_NOT_NULL(10137, "please delete the process definitions in project first!"), + BATCH_DELETE_PROCESS_INSTANCE_BY_IDS_ERROR(10117,"batch delete process instance by ids {0} error"), diff --git a/escheduler-api/src/main/java/cn/escheduler/api/service/ProcessInstanceService.java b/escheduler-api/src/main/java/cn/escheduler/api/service/ProcessInstanceService.java index 43d5377da2..70abaf07eb 100644 --- a/escheduler-api/src/main/java/cn/escheduler/api/service/ProcessInstanceService.java +++ b/escheduler-api/src/main/java/cn/escheduler/api/service/ProcessInstanceService.java @@ -467,6 +467,7 @@ public class ProcessInstanceService extends BaseDAGService { putMsg(result, Status.PROCESS_INSTANCE_NOT_EXIST, workflowId); return result; } + int delete = processDao.deleteWorkProcessInstanceById(workflowId); processDao.deleteAllSubWorkProcessByParentId(workflowId); processDao.deleteWorkProcessMapByParentId(workflowId); @@ -479,6 +480,48 @@ public class ProcessInstanceService extends BaseDAGService { return result; } + /** + * batch delete process instance by ids, at the same timeļ¼Œdelete task instance and their mapping relation data + * + * @param loginUser + * @param projectName + * @param processInstanceIds + * @return + */ + public Map batchDeleteProcessInstanceByIds(User loginUser, String projectName, String processInstanceIds) { + + Map result = new HashMap<>(5); + List deleteFailedIdList = new ArrayList(); + + Project project = projectMapper.queryByName(projectName); + + Map checkResult = projectService.checkProjectAndAuth(loginUser, project, projectName); + Status resultEnum = (Status) checkResult.get(Constants.STATUS); + if (resultEnum != Status.SUCCESS) { + return checkResult; + } + + if(StringUtils.isNotEmpty(processInstanceIds)){ + String[] processInstanceIdArray = processInstanceIds.split(","); + + for (String strProcessInstanceId:processInstanceIdArray) { + int processInstanceId = Integer.parseInt(strProcessInstanceId); + try { + deleteProcessInstanceById(loginUser, projectName, processInstanceId); + } catch (Exception e) { + deleteFailedIdList.add(processInstanceId); + } + } + } + if(deleteFailedIdList.size() > 0){ + putMsg(result, Status.BATCH_DELETE_PROCESS_INSTANCE_BY_IDS_ERROR,StringUtils.join(deleteFailedIdList.toArray(),",")); + }else{ + putMsg(result, Status.SUCCESS); + } + + return result; + } + /** * view process instance variables * diff --git a/escheduler-api/src/test/java/cn/escheduler/api/service/ProcessInstanceServiceTest.java b/escheduler-api/src/test/java/cn/escheduler/api/service/ProcessInstanceServiceTest.java index 4fff4f184f..532c08df54 100644 --- a/escheduler-api/src/test/java/cn/escheduler/api/service/ProcessInstanceServiceTest.java +++ b/escheduler-api/src/test/java/cn/escheduler/api/service/ProcessInstanceServiceTest.java @@ -75,4 +75,16 @@ public class ProcessInstanceServiceTest { Assert.assertEquals(Status.SUCCESS, map.get(Constants.STATUS)); logger.info(JSON.toJSONString(map)); } + + @Test + public void batchDeleteProcessInstanceByIds() throws Exception { + + User loginUser = new User(); + loginUser.setId(2); + loginUser.setUserType(UserType.GENERAL_USER); + Map map = processInstanceService.batchDeleteProcessInstanceByIds(loginUser, "li_test_1", "4,2,300"); + + Assert.assertEquals(Status.SUCCESS, map.get(Constants.STATUS)); + logger.info(JSON.toJSONString(map)); + } } \ No newline at end of file From b7f3dad506720d035092ed22abf8fcc25bf69317 Mon Sep 17 00:00:00 2001 From: ligang Date: Mon, 13 May 2019 15:24:25 +0800 Subject: [PATCH 2/2] add function batchDeleteProcessDefinitionByIds --- .../ProcessDefinitionController.java | 25 ++++++++++ .../java/cn/escheduler/api/enums/Status.java | 2 + .../api/service/ProcessDefinitionService.java | 50 +++++++++++++++++++ .../service/ProcessDefinitionServiceTest.java | 11 ++++ 4 files changed, 88 insertions(+) diff --git a/escheduler-api/src/main/java/cn/escheduler/api/controller/ProcessDefinitionController.java b/escheduler-api/src/main/java/cn/escheduler/api/controller/ProcessDefinitionController.java index 0ca71da70d..8b28925a43 100644 --- a/escheduler-api/src/main/java/cn/escheduler/api/controller/ProcessDefinitionController.java +++ b/escheduler-api/src/main/java/cn/escheduler/api/controller/ProcessDefinitionController.java @@ -350,4 +350,29 @@ public class ProcessDefinitionController extends BaseController{ } } + /** + * batch delete process definition by ids + * + * @param loginUser + * @param projectName + * @param processDefinitionIds + * @return + */ + @GetMapping(value="/batch-delete") + @ResponseStatus(HttpStatus.OK) + public Result batchDeleteProcessDefinitionByIds(@RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @PathVariable String projectName, + @RequestParam("processDefinitionIds") String processDefinitionIds + ){ + try{ + logger.info("delete process definition by ids, login user:{}, project name:{}, process definition ids:{}", + loginUser.getUserName(), projectName, processDefinitionIds); + Map result = processDefinitionService.batchDeleteProcessDefinitionByIds(loginUser, projectName, processDefinitionIds); + return returnDataList(result); + }catch (Exception e){ + logger.error(BATCH_DELETE_PROCESS_DEFINE_BY_IDS_ERROR.getMsg(),e); + return error(Status.BATCH_DELETE_PROCESS_DEFINE_BY_IDS_ERROR.getCode(), Status.BATCH_DELETE_PROCESS_DEFINE_BY_IDS_ERROR.getMsg()); + } + } + } diff --git a/escheduler-api/src/main/java/cn/escheduler/api/enums/Status.java b/escheduler-api/src/main/java/cn/escheduler/api/enums/Status.java index a4a44bd904..6e25d91825 100644 --- a/escheduler-api/src/main/java/cn/escheduler/api/enums/Status.java +++ b/escheduler-api/src/main/java/cn/escheduler/api/enums/Status.java @@ -206,6 +206,8 @@ public enum Status { DELETE_PROCESS_DEFINE_BY_ID_ERROR(50022,"delete process definition by id error"), SCHEDULE_CRON_STATE_ONLINE(50023,"the status of schedule {0} is already on line"), DELETE_SCHEDULE_CRON_BY_ID_ERROR(50024,"delete schedule by id error"), + BATCH_DELETE_PROCESS_DEFINE_ERROR(50025,"batch delete process definition error"), + BATCH_DELETE_PROCESS_DEFINE_BY_IDS_ERROR(50026,"batch delete process definition by ids {0} error"), HDFS_NOT_STARTUP(60001,"hdfs not startup"), HDFS_TERANT_RESOURCES_FILE_EXISTS(60002,"resource file exists,please delete resource first"), diff --git a/escheduler-api/src/main/java/cn/escheduler/api/service/ProcessDefinitionService.java b/escheduler-api/src/main/java/cn/escheduler/api/service/ProcessDefinitionService.java index 57b67ec503..d9bdc3caab 100644 --- a/escheduler-api/src/main/java/cn/escheduler/api/service/ProcessDefinitionService.java +++ b/escheduler-api/src/main/java/cn/escheduler/api/service/ProcessDefinitionService.java @@ -38,6 +38,7 @@ import cn.escheduler.dao.mapper.*; import cn.escheduler.dao.model.*; import com.alibaba.fastjson.JSON; import com.fasterxml.jackson.core.JsonProcessingException; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -395,6 +396,55 @@ public class ProcessDefinitionService extends BaseDAGService { return result; } + /** + * batch delete process definition by ids + * + * @param loginUser + * @param projectName + * @param processDefinitionIds + * @return + */ + public Map batchDeleteProcessDefinitionByIds(User loginUser, String projectName, String processDefinitionIds) { + + Map result = new HashMap<>(5); + + Map deleteReuslt = new HashMap<>(5); + + List deleteFailedIdList = new ArrayList(); + Project project = projectMapper.queryByName(projectName); + + Map checkResult = projectService.checkProjectAndAuth(loginUser, project, projectName); + Status resultEnum = (Status) checkResult.get(Constants.STATUS); + if (resultEnum != Status.SUCCESS) { + return checkResult; + } + + + if(StringUtils.isNotEmpty(processDefinitionIds)){ + String[] processInstanceIdArray = processDefinitionIds.split(","); + + for (String strProcessInstanceId:processInstanceIdArray) { + int processInstanceId = Integer.parseInt(strProcessInstanceId); + try { + deleteReuslt = deleteProcessDefinitionById(loginUser, projectName, processInstanceId); + if(!Status.SUCCESS.equals(deleteReuslt.get(Constants.STATUS))){ + deleteFailedIdList.add(processInstanceId); + logger.error((String)deleteReuslt.get(Constants.MSG)); + } + } catch (Exception e) { + deleteFailedIdList.add(processInstanceId); + } + } + } + + if(deleteFailedIdList.size() > 0){ + putMsg(result, Status.BATCH_DELETE_PROCESS_DEFINE_BY_IDS_ERROR,StringUtils.join(deleteFailedIdList.toArray(),",")); + }else{ + putMsg(result, Status.SUCCESS); + } + return result; + } + /** * release process definition: online / offline * diff --git a/escheduler-api/src/test/java/cn/escheduler/api/service/ProcessDefinitionServiceTest.java b/escheduler-api/src/test/java/cn/escheduler/api/service/ProcessDefinitionServiceTest.java index 48dcd043fa..4de3cbc103 100644 --- a/escheduler-api/src/test/java/cn/escheduler/api/service/ProcessDefinitionServiceTest.java +++ b/escheduler-api/src/test/java/cn/escheduler/api/service/ProcessDefinitionServiceTest.java @@ -75,4 +75,15 @@ public class ProcessDefinitionServiceTest { Assert.assertEquals(Status.SUCCESS, map.get(Constants.STATUS)); logger.info(JSON.toJSONString(map)); } + + @Test + public void batchDeleteProcessDefinitionByIds() throws Exception { + + User loginUser = new User(); + loginUser.setId(2); + loginUser.setUserType(UserType.GENERAL_USER); + Map map = processDefinitionService.batchDeleteProcessDefinitionByIds(loginUser, "li_test_1", "2,3"); + Assert.assertEquals(Status.SUCCESS, map.get(Constants.STATUS)); + logger.info(JSON.toJSONString(map)); + } } \ No newline at end of file