feat(任务中心): 系统&组织&项目后台任务删除

This commit is contained in:
WangXu10 2024-10-19 10:31:10 +08:00 committed by Craftsman
parent 2442965a53
commit 0a71bbea52
8 changed files with 151 additions and 13 deletions

View File

@ -142,4 +142,11 @@ public class ProjectTaskHubController {
"/project/task-center/exec-task/item/batch-stop", OperationLogModule.PROJECT_MANAGEMENT_TASK_CENTER); "/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);
}
} }

View File

@ -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_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_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_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 @Test
@Order(1) @Order(1)
@ -207,4 +208,19 @@ public class ProjectTaskHubControllerTests extends BaseTest {
request.setSelectIds(List.of("pro_1", "pro_2")); request.setSelectIds(List.of("pro_1", "pro_2"));
this.requestPostWithOkAndReturn(PROJECT_TASK_ITEM_BATCH_STOP, request); 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);
}
} }

View File

@ -13,3 +13,11 @@ INSERT INTO `test_resource_pool` (`id`, `name`, `type`, `description`, `enable`,
VALUES VALUES
('1', 'LOCAL', 'Node', '测试资源池', b'1', 1705894549000, 1705894549000, 'admin', NULL, b'1', b'0'), ('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'); ('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);

View File

@ -151,4 +151,12 @@ public class OrganizationTaskHubController {
baseTaskHubLogService.taskItemBatchLog(itemIds, SessionUtils.getUserId(), OperationLogType.STOP.name(), OperationLogConstants.ORGANIZATION, SessionUtils.getCurrentOrganizationId(), 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); "/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);
}
} }

View File

@ -156,6 +156,11 @@ public class SystemTaskHubController {
} }
//TODO 系统&组织&项目 任务详情按钮操作查看 //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 系统&组织&项目 后台任务操作删除 批量开启 批量关闭 //TODO 系统&组织&项目 后台任务操作删除 批量开启 批量关闭
} }

View File

@ -12,26 +12,30 @@ import io.metersphere.project.domain.ProjectTestResourcePool;
import io.metersphere.project.domain.ProjectTestResourcePoolExample; import io.metersphere.project.domain.ProjectTestResourcePoolExample;
import io.metersphere.project.mapper.ProjectMapper; import io.metersphere.project.mapper.ProjectMapper;
import io.metersphere.project.mapper.ProjectTestResourcePoolMapper; import io.metersphere.project.mapper.ProjectTestResourcePoolMapper;
import io.metersphere.sdk.constants.ExecStatus; import io.metersphere.sdk.constants.*;
import io.metersphere.sdk.constants.ResourcePoolTypeEnum; import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.constants.ResultStatus; import io.metersphere.sdk.util.*;
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.system.controller.handler.ResultHolder; import io.metersphere.system.controller.handler.ResultHolder;
import io.metersphere.system.domain.*; 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.TestResourceDTO;
import io.metersphere.system.dto.pool.TestResourceNodeDTO; import io.metersphere.system.dto.pool.TestResourceNodeDTO;
import io.metersphere.system.dto.pool.TestResourcePoolReturnDTO; import io.metersphere.system.dto.pool.TestResourcePoolReturnDTO;
import io.metersphere.system.dto.sdk.BasePageRequest; import io.metersphere.system.dto.sdk.BasePageRequest;
import io.metersphere.system.dto.sdk.OptionDTO; import io.metersphere.system.dto.sdk.OptionDTO;
import io.metersphere.system.dto.table.TableBatchProcessDTO; 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.*;
import io.metersphere.system.dto.taskhub.request.TaskHubItemBatchRequest; import io.metersphere.system.dto.taskhub.request.TaskHubItemBatchRequest;
import io.metersphere.system.dto.taskhub.request.TaskHubItemRequest; import io.metersphere.system.dto.taskhub.request.TaskHubItemRequest;
import io.metersphere.system.dto.taskhub.response.TaskStatisticsResponse; 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.mapper.*;
import io.metersphere.system.schedule.ScheduleService;
import io.metersphere.system.utils.PageUtils; import io.metersphere.system.utils.PageUtils;
import io.metersphere.system.utils.Pager; import io.metersphere.system.utils.Pager;
import io.metersphere.system.utils.TaskRunnerClient; 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"; 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); itemExample.createCriteria().andIdIn(taskIdItemIds);
return execTaskItemMapper.selectByExample(itemExample); 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<Schedule> scheduleList, String userId, String path, String method, String module, String type) {
//取出所有的项目id
if (scheduleList.isEmpty()) {
return;
}
List<String> projectIds = scheduleList.stream().map(Schedule::getProjectId).distinct().toList();
//根据项目id取出组织id
List<ProjectDTO> orgList = extScheduleMapper.getOrgListByProjectIds(projectIds);
//生成map key:项目id value:组织id
Map<String, String> orgMap = orgList.stream().collect(Collectors.toMap(ProjectDTO::getId, ProjectDTO::getOrganizationId));
List<LogDTO> 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;
}
} }

View File

@ -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_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_TASK_ITEM_ORDER = "/system/task-center/exec-task/item/order";
public static final String SYSTEM_SCHEDULE_TASK_DELETE = "/system/task-center/schedule/delete/";
@Test @Test
@Order(1) @Order(1)
@Sql(scripts = {"/dml/init_exec_task_test.sql"}, config = @SqlConfig(encoding = "utf-8", transactionMode = SqlConfig.TransactionMode.ISOLATED)) @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); 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_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_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_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 @Test
@Order(20) @Order(20)
@ -432,6 +450,21 @@ public class BaseTaskHubControllerTests extends BaseTest {
Assertions.assertNotNull(resultHolder); 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 @Test
@Order(21) @Order(21)

View File

@ -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`) 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 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_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', 'NONE', 'admin', 1640777400000, 1640778400000, '100001100001', 'Schedule 15', '', 101), ('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', 'NONE', 'admin', 1640778100000, 1640779100000, '100001100001', 'Schedule 22', '', 102), ('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', 'NONE', 'admin', 1640778800000, 1640779800000, '100001100001', 'Schedule 29', '{\"param1\": \"value1\", \"param2\": \"value2\"}', 103), ('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', 'NONE', 'admin', 1640778800000, 1640779800000, '100001100001', 'Schedule 29', '{\"param1\": \"value1\", \"param2\": \"value2\"}', 104); ('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);