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