diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/controller/ProjectTaskHubController.java b/backend/services/project-management/src/main/java/io/metersphere/project/controller/ProjectTaskHubController.java index 79cc11b732..32941bf742 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/controller/ProjectTaskHubController.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/controller/ProjectTaskHubController.java @@ -142,4 +142,11 @@ public class ProjectTaskHubController { "/project/task-center/exec-task/item/batch-stop", OperationLogModule.PROJECT_MANAGEMENT_TASK_CENTER); } + + @GetMapping("/schedule/delete/{id}") + @Operation(summary = "项目-任务中心-系统后台任务-删除") + @RequiresPermissions(PermissionConstants.PROJECT_SCHEDULE_TASK_CENTER_READ_DELETE) + public void deleteScheduleTask(@PathVariable String id) { + baseTaskHubService.deleteScheduleTask(id, SessionUtils.getUserId(), "/project/task-center/schedule/delete/", OperationLogModule.PROJECT_MANAGEMENT_TASK_CENTER); + } } diff --git a/backend/services/project-management/src/test/java/io/metersphere/project/controller/ProjectTaskHubControllerTests.java b/backend/services/project-management/src/test/java/io/metersphere/project/controller/ProjectTaskHubControllerTests.java index 2e9dc714a2..82211024cc 100644 --- a/backend/services/project-management/src/test/java/io/metersphere/project/controller/ProjectTaskHubControllerTests.java +++ b/backend/services/project-management/src/test/java/io/metersphere/project/controller/ProjectTaskHubControllerTests.java @@ -34,6 +34,7 @@ public class ProjectTaskHubControllerTests extends BaseTest { public static final String PROJECT_TASK_BATCH_DELETE = "/organization/task-center/exec-task/batch-delete"; public static final String PROJECT_TASK_ITEM_STOP = "/project/task-center/exec-task/item/stop/"; public static final String PROJECT_TASK_ITEM_BATCH_STOP = "/project/task-center/exec-task/item/batch-stop"; + public static final String PROJECT_SCHEDULE_TASK_DELETE = "/project/task-center/schedule/delete/"; @Test @Order(1) @@ -207,4 +208,19 @@ public class ProjectTaskHubControllerTests extends BaseTest { request.setSelectIds(List.of("pro_1", "pro_2")); this.requestPostWithOkAndReturn(PROJECT_TASK_ITEM_BATCH_STOP, request); } + + /** + * 项目后台任务删除 + */ + @Test + @Order(5) + public void projectScheduleTaskDelete() throws Exception { + this.requestGet(PROJECT_SCHEDULE_TASK_DELETE + "123143"); + MvcResult mvcResult = this.requestGetWithOkAndReturn(PROJECT_SCHEDULE_TASK_DELETE + "pro_wx_1"); + // 获取返回值 + String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + ResultHolder resultHolder = JSON.parseObject(returnData, ResultHolder.class); + // 返回请求正常 + Assertions.assertNotNull(resultHolder); + } } diff --git a/backend/services/project-management/src/test/resources/dml/init_project_exec_task_test.sql b/backend/services/project-management/src/test/resources/dml/init_project_exec_task_test.sql index c2d4c4242e..850202d272 100644 --- a/backend/services/project-management/src/test/resources/dml/init_project_exec_task_test.sql +++ b/backend/services/project-management/src/test/resources/dml/init_project_exec_task_test.sql @@ -13,3 +13,11 @@ INSERT INTO `test_resource_pool` (`id`, `name`, `type`, `description`, `enable`, VALUES ('1', 'LOCAL', 'Node', '测试资源池', b'1', 1705894549000, 1705894549000, 'admin', NULL, b'1', b'0'), ('2', 'LOCAL', 'Kubernetes', 'Kubernetes测试资源池', b'1', 1705894549000, 1705894549000, 'admin', NULL, b'1', b'0'); + + +INSERT INTO `schedule` (`id`, `key`, `type`, `value`, `job`, `resource_type`, `enable`, `resource_id`, `create_user`, `create_time`, `update_time`, `project_id`, `name`, `config`, `num`) +VALUES + ('pro_wx_1', 'wx_key_1', 'cron', '1233213', 'JobClass1', 'API_IMPORT', b'1', '12134', 'admin', 1640776000000, 1640777000000, '100001100001', 'Schedule 1', '{\"param1\": \"value1\", \"param2\": \"value2\"}', 100), + ('pro_wx_2', 'wx_key_2', 'cron', '1231321231', 'JobClass15', 'BUG_SYNC', b'0', '2234', 'admin', 1640777400000, 1640778400000, '100001100001', 'Schedule 15', '', 101), + ('wx_3', 'wx_key_3', 'cron', '50 15 10 20 05 ?', 'JobClass22', 'DEMAND_SYNC', b'1', '3234', 'admin', 1640778100000, 1640779100000, '100001100001', 'Schedule 22', '', 102); + diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/OrganizationTaskHubController.java b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/OrganizationTaskHubController.java index 64bfa66b7b..aad0a4ae73 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/OrganizationTaskHubController.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/OrganizationTaskHubController.java @@ -151,4 +151,12 @@ public class OrganizationTaskHubController { baseTaskHubLogService.taskItemBatchLog(itemIds, SessionUtils.getUserId(), OperationLogType.STOP.name(), OperationLogConstants.ORGANIZATION, SessionUtils.getCurrentOrganizationId(), "/organization/task-center/exec-task/item/batch-stop", OperationLogModule.SETTING_ORGANIZATION_TASK_CENTER); } + + + @GetMapping("/schedule/delete/{id}") + @Operation(summary = "组织-任务中心-系统后台任务-删除") + @RequiresPermissions(PermissionConstants.ORGANIZATION_SCHEDULE_TASK_CENTER_READ_DELETE) + public void deleteScheduleTask(@PathVariable String id) { + baseTaskHubService.deleteScheduleTask(id, SessionUtils.getUserId(), "/organization/task-center/schedule/delete/", OperationLogModule.SETTING_ORGANIZATION_TASK_CENTER); + } } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/SystemTaskHubController.java b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/SystemTaskHubController.java index f1546ec54c..67015f4cb2 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/SystemTaskHubController.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/SystemTaskHubController.java @@ -156,6 +156,11 @@ public class SystemTaskHubController { } //TODO 系统&组织&项目 任务详情按钮操作:查看 - + @GetMapping("/schedule/delete/{id}") + @Operation(summary = "系统-任务中心-系统后台任务-删除") + @RequiresPermissions(PermissionConstants.SYSTEM_SCHEDULE_TASK_CENTER_READ_DELETE) + public void deleteScheduleTask(@PathVariable String id) { + baseTaskHubService.deleteScheduleTask(id, SessionUtils.getUserId(), "/system/task-center/schedule/delete/", OperationLogModule.SETTING_SYSTEM_TASK_CENTER); + } //TODO 系统&组织&项目 后台任务操作:删除 批量开启 批量关闭 } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTaskHubService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTaskHubService.java index a449e36fe3..d0c31095e3 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTaskHubService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTaskHubService.java @@ -12,26 +12,30 @@ import io.metersphere.project.domain.ProjectTestResourcePool; import io.metersphere.project.domain.ProjectTestResourcePoolExample; import io.metersphere.project.mapper.ProjectMapper; import io.metersphere.project.mapper.ProjectTestResourcePoolMapper; -import io.metersphere.sdk.constants.ExecStatus; -import io.metersphere.sdk.constants.ResourcePoolTypeEnum; -import io.metersphere.sdk.constants.ResultStatus; -import io.metersphere.sdk.util.BeanUtils; -import io.metersphere.sdk.util.JSON; -import io.metersphere.sdk.util.LogUtils; -import io.metersphere.sdk.util.SubListUtils; +import io.metersphere.sdk.constants.*; +import io.metersphere.sdk.exception.MSException; +import io.metersphere.sdk.util.*; import io.metersphere.system.controller.handler.ResultHolder; import io.metersphere.system.domain.*; +import io.metersphere.system.dto.ProjectDTO; +import io.metersphere.system.dto.builder.LogDTOBuilder; import io.metersphere.system.dto.pool.TestResourceDTO; import io.metersphere.system.dto.pool.TestResourceNodeDTO; import io.metersphere.system.dto.pool.TestResourcePoolReturnDTO; import io.metersphere.system.dto.sdk.BasePageRequest; import io.metersphere.system.dto.sdk.OptionDTO; import io.metersphere.system.dto.table.TableBatchProcessDTO; +import io.metersphere.system.dto.taskcenter.enums.ScheduleTagType; import io.metersphere.system.dto.taskhub.*; import io.metersphere.system.dto.taskhub.request.TaskHubItemBatchRequest; import io.metersphere.system.dto.taskhub.request.TaskHubItemRequest; import io.metersphere.system.dto.taskhub.response.TaskStatisticsResponse; +import io.metersphere.system.log.constants.OperationLogModule; +import io.metersphere.system.log.constants.OperationLogType; +import io.metersphere.system.log.dto.LogDTO; +import io.metersphere.system.log.service.OperationLogService; import io.metersphere.system.mapper.*; +import io.metersphere.system.schedule.ScheduleService; import io.metersphere.system.utils.PageUtils; import io.metersphere.system.utils.Pager; import io.metersphere.system.utils.TaskRunnerClient; @@ -98,6 +102,15 @@ public class BaseTaskHubService { private final static String GET_TASK_ITEM_ORDER_URL = "http://%s/api/task/item/order"; + @Resource + private ScheduleService scheduleService; + @Resource + private ScheduleMapper scheduleMapper; + @Resource + private ExtSwaggerMapper extSwaggerMapper; + @Resource + private OperationLogService operationLogService; + /** * 系统-获取执行任务列表 * @@ -701,4 +714,52 @@ public class BaseTaskHubService { itemExample.createCriteria().andIdIn(taskIdItemIds); return execTaskItemMapper.selectByExample(itemExample); } + + public void deleteScheduleTask(String id, String userId, String path, String module) { + Schedule schedule = checkScheduleExit(id); + if (!StringUtils.equalsAnyIgnoreCase(schedule.getResourceType(), ScheduleResourceType.BUG_SYNC.name(), ScheduleResourceType.DEMAND_SYNC.name())) { + if (StringUtils.equals(schedule.getResourceType(), ScheduleTagType.API_IMPORT.name())) { + extSwaggerMapper.deleteByPrimaryKey(schedule.getResourceId()); + } + scheduleService.deleteByResourceId(schedule.getResourceId(), schedule.getJob()); + saveLog(List.of(schedule), userId, path, HttpMethodConstants.GET.name(), module, OperationLogType.DELETE.name()); + } + } + + private void saveLog(List scheduleList, String userId, String path, String method, String module, String type) { + //取出所有的项目id + if (scheduleList.isEmpty()) { + return; + } + List projectIds = scheduleList.stream().map(Schedule::getProjectId).distinct().toList(); + //根据项目id取出组织id + List orgList = extScheduleMapper.getOrgListByProjectIds(projectIds); + //生成map key:项目id value:组织id + Map orgMap = orgList.stream().collect(Collectors.toMap(ProjectDTO::getId, ProjectDTO::getOrganizationId)); + List logs = new ArrayList<>(); + scheduleList.forEach(s -> { + LogDTO dto = LogDTOBuilder.builder() + .projectId(s.getProjectId()) + .organizationId(orgMap.get(s.getProjectId())) + .type(type) + .module(module) + .method(method) + .path(path) + .sourceId(s.getResourceId()) + .content(s.getName()) + .createUser(userId) + .build().getLogDTO(); + logs.add(dto); + }); + operationLogService.batchAdd(logs); + } + + + public Schedule checkScheduleExit(String id) { + Schedule schedule = scheduleMapper.selectByPrimaryKey(id); + if (schedule == null) { + throw new MSException(Translator.get("schedule_not_exist")); + } + return schedule; + } } diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/BaseTaskHubControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/BaseTaskHubControllerTests.java index 7e74f98c1f..35f41b9b91 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/BaseTaskHubControllerTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/BaseTaskHubControllerTests.java @@ -51,6 +51,8 @@ public class BaseTaskHubControllerTests extends BaseTest { public static final String SYSTEM_TASK_ITEM_BATCH_STOP = "/system/task-center/exec-task/item/batch-stop"; public static final String SYSTEM_TASK_ITEM_ORDER = "/system/task-center/exec-task/item/order"; + public static final String SYSTEM_SCHEDULE_TASK_DELETE = "/system/task-center/schedule/delete/"; + @Test @Order(1) @Sql(scripts = {"/dml/init_exec_task_test.sql"}, config = @SqlConfig(encoding = "utf-8", transactionMode = SqlConfig.TransactionMode.ISOLATED)) @@ -242,6 +244,21 @@ public class BaseTaskHubControllerTests extends BaseTest { this.requestPost(SYSTEM_TASK_ITEM_BATCH_STOP, request); } + /** + * 系统后台任务删除 + */ + @Test + @Order(7) + public void systemScheduleTaskDelete() throws Exception { + this.requestGet(SYSTEM_SCHEDULE_TASK_DELETE + "1231"); + MvcResult mvcResult = this.requestGetWithOkAndReturn(SYSTEM_SCHEDULE_TASK_DELETE + "wx_1"); + // 获取返回值 + String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + ResultHolder resultHolder = JSON.parseObject(returnData, ResultHolder.class); + // 返回请求正常 + Assertions.assertNotNull(resultHolder); + } + /** * 组织任务中心测试用例 @@ -258,6 +275,7 @@ public class BaseTaskHubControllerTests extends BaseTest { public static final String ORG_TASK_ITEM_ORDER = "/organization/task-center/exec-task/item/order"; public static final String ORG_TASK_ITEM_STOP = "/organization/task-center/exec-task/item/stop/"; public static final String ORG_TASK_ITEM_BATCH_STOP = "/organization/task-center/exec-task/item/batch-stop"; + public static final String ORG_SCHEDULE_TASK_DELETE = "/organization/task-center/schedule/delete/"; @Test @Order(20) @@ -432,6 +450,21 @@ public class BaseTaskHubControllerTests extends BaseTest { Assertions.assertNotNull(resultHolder); } + /** + * 系统后台任务删除 + */ + @Test + @Order(26) + public void orgScheduleTaskDelete() throws Exception { + this.requestGet(ORG_SCHEDULE_TASK_DELETE + "123143"); + MvcResult mvcResult = this.requestGetWithOkAndReturn(ORG_SCHEDULE_TASK_DELETE + "wx_2"); + // 获取返回值 + String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + ResultHolder resultHolder = JSON.parseObject(returnData, ResultHolder.class); + // 返回请求正常 + Assertions.assertNotNull(resultHolder); + } + @Test @Order(21) diff --git a/backend/services/system-setting/src/test/resources/dml/init_exec_task_test.sql b/backend/services/system-setting/src/test/resources/dml/init_exec_task_test.sql index c62290e91a..93467c50ed 100644 --- a/backend/services/system-setting/src/test/resources/dml/init_exec_task_test.sql +++ b/backend/services/system-setting/src/test/resources/dml/init_exec_task_test.sql @@ -10,11 +10,11 @@ VALUES INSERT INTO `schedule` (`id`, `key`, `type`, `value`, `job`, `resource_type`, `enable`, `resource_id`, `create_user`, `create_time`, `update_time`, `project_id`, `name`, `config`, `num`) VALUES - ('wx_1', 'wx_key_1', 'cron', '1233213', 'JobClass1', 'API_IMPORT', b'1', 'NONE', 'admin', 1640776000000, 1640777000000, '100001100001', 'Schedule 1', '{\"param1\": \"value1\", \"param2\": \"value2\"}', 100), - ('wx_2', 'wx_key_2', 'cron', '1231321231', 'JobClass15', 'BUG_SYNC', b'0', 'NONE', 'admin', 1640777400000, 1640778400000, '100001100001', 'Schedule 15', '', 101), - ('wx_3', 'wx_key_3', 'cron', '50 15 10 20 05 ?', 'JobClass22', 'DEMAND_SYNC', b'1', 'NONE', 'admin', 1640778100000, 1640779100000, '100001100001', 'Schedule 22', '', 102), - ('wx_4', 'wx_key_4', 'cron', '59 46 16 01 06 ?', 'JobClass29', 'API_SCENARIO', b'1', 'NONE', 'admin', 1640778800000, 1640779800000, '100001100001', 'Schedule 29', '{\"param1\": \"value1\", \"param2\": \"value2\"}', 103), - ('wx_5', 'wx_key_5', 'cron', '59 46 16 01 06 ?', 'JobClass29', 'TEST_PLAN', b'1', 'NONE', 'admin', 1640778800000, 1640779800000, '100001100001', 'Schedule 29', '{\"param1\": \"value1\", \"param2\": \"value2\"}', 104); + ('wx_1', 'wx_key_1', 'cron', '1233213', 'JobClass1', 'API_IMPORT', b'1', '123456', 'admin', 1640776000000, 1640777000000, '100001100001', 'Schedule 1', '{\"param1\": \"value1\", \"param2\": \"value2\"}', 100), + ('wx_2', 'wx_key_2', 'cron', '1231321231', 'JobClass15', 'BUG_SYNC', b'0', '234256', 'admin', 1640777400000, 1640778400000, '100001100001', 'Schedule 15', '', 101), + ('wx_3', 'wx_key_3', 'cron', '50 15 10 20 05 ?', 'JobClass22', 'DEMAND_SYNC', b'1', '4353', 'admin', 1640778100000, 1640779100000, '100001100001', 'Schedule 22', '', 102), + ('wx_4', 'wx_key_4', 'cron', '59 46 16 01 06 ?', 'JobClass29', 'API_SCENARIO', b'1', '1323235', 'admin', 1640778800000, 1640779800000, '100001100001', 'Schedule 29', '{\"param1\": \"value1\", \"param2\": \"value2\"}', 103), + ('wx_5', 'wx_key_5', 'cron', '59 46 16 01 06 ?', 'JobClass29', 'TEST_PLAN', b'1', '54353', 'admin', 1640778800000, 1640779800000, '100001100001', 'Schedule 29', '{\"param1\": \"value1\", \"param2\": \"value2\"}', 104);