From 104f5406ae0194a372025d083c27bb7902f5c07a Mon Sep 17 00:00:00 2001 From: WangXu10 Date: Mon, 21 Oct 2024 14:36:39 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E4=BB=BB=E5=8A=A1=E4=B8=AD=E5=BF=83):=20?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F&=E7=BB=84=E7=BB=87&=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E5=90=8E=E5=8F=B0=E4=BB=BB=E5=8A=A1=E5=88=97=E8=A1=A8=E6=9B=B4?= =?UTF-8?q?=E6=96=B0cron=E8=A1=A8=E8=BE=BE=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ProjectTaskHubController.java | 8 ++++ .../ProjectTaskHubControllerTests.java | 23 ++++++--- .../OrganizationTaskHubController.java | 11 ++++- .../controller/SystemTaskHubController.java | 11 ++++- .../dto/taskhub/request/ScheduleRequest.java | 22 +++++++++ .../system/service/BaseTaskHubService.java | 14 ++++++ .../BaseTaskHubControllerTests.java | 47 ++++++++++++++----- 7 files changed, 116 insertions(+), 20 deletions(-) create mode 100644 backend/services/system-setting/src/main/java/io/metersphere/system/dto/taskhub/request/ScheduleRequest.java 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 fea1fe939d..760b90e7f3 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 @@ -8,6 +8,7 @@ import io.metersphere.system.dto.taskhub.ResourcePoolOptionsDTO; import io.metersphere.system.dto.taskhub.TaskHubDTO; import io.metersphere.system.dto.taskhub.TaskHubItemDTO; import io.metersphere.system.dto.taskhub.TaskHubScheduleDTO; +import io.metersphere.system.dto.taskhub.request.ScheduleRequest; import io.metersphere.system.dto.taskhub.request.TaskHubItemBatchRequest; import io.metersphere.system.dto.taskhub.request.TaskHubItemRequest; import io.metersphere.system.dto.taskhub.response.TaskStatisticsResponse; @@ -173,4 +174,11 @@ public class ProjectTaskHubController { public void batchDisable(@Validated @RequestBody TableBatchProcessDTO request) { baseTaskHubService.scheduleBatchOperation(request, SessionUtils.getUserId(), null, "/project/task-center/schedule/batch-disable", OperationLogModule.PROJECT_MANAGEMENT_TASK_CENTER, false, List.of(SessionUtils.getCurrentProjectId())); } + + @PostMapping("/schedule/update-cron") + @Operation(summary = "项目-任务中心-后台任务更新cron表达式") + @RequiresPermissions(PermissionConstants.PROJECT_SCHEDULE_TASK_CENTER_READ_UPDATE) + public void updateValue(@PathVariable ScheduleRequest request) { + baseTaskHubService.updateCron(request); + } } 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 31d036318c..b018141cb2 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 @@ -5,6 +5,7 @@ import io.metersphere.system.base.BaseTest; import io.metersphere.system.controller.handler.ResultHolder; import io.metersphere.system.dto.sdk.BasePageRequest; import io.metersphere.system.dto.table.TableBatchProcessDTO; +import io.metersphere.system.dto.taskhub.request.ScheduleRequest; import io.metersphere.system.dto.taskhub.request.TaskHubItemBatchRequest; import io.metersphere.system.dto.taskhub.request.TaskHubItemRequest; import org.junit.jupiter.api.Assertions; @@ -38,6 +39,7 @@ public class ProjectTaskHubControllerTests extends BaseTest { public static final String PROJECT_SCHEDULE_TASK_SWITCH = "/project/task-center/schedule/switch/"; public static final String PROJECT_SCHEDULE_TASK_BATCH_ENABLE = "/project/task-center/schedule/batch-enable"; public static final String PROJECT_SCHEDULE_TASK_BATCH_DISABLE = "/project/task-center/schedule/batch-disable"; + public static final String PROJECT_SCHEDULE_TASK_UPDATE_CRON = "/organization/task-center/schedule/update-cron"; @Test @Order(1) @@ -127,13 +129,10 @@ public class ProjectTaskHubControllerTests extends BaseTest { @Test @Order(6) public void projectTaskStop() throws Exception { + mockPost("/api/task/item/stop", ""); this.requestGet(PROJECT_TASK_STOP + "pro_1"); - MvcResult mvcResult = this.requestGetWithOkAndReturn(PROJECT_TASK_STOP + "pro_2"); - // 获取返回值 - String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); - ResultHolder resultHolder = JSON.parseObject(returnData, ResultHolder.class); - // 返回请求正常 - Assertions.assertNotNull(resultHolder); + mockPost("/api/task/stop", ""); + this.requestGet(PROJECT_TASK_STOP + "pro_2"); } /** @@ -142,6 +141,7 @@ public class ProjectTaskHubControllerTests extends BaseTest { @Test @Order(23) public void projectTaskBatchStop() throws Exception { + mockPost("/api/task/item/stop", ""); TableBatchProcessDTO request = new TableBatchProcessDTO(); request.setSelectAll(true); this.requestPost(PROJECT_TASK_BATCH_STOP, request); @@ -192,6 +192,7 @@ public class ProjectTaskHubControllerTests extends BaseTest { @Test @Order(4) public void projectTaskItemStop() throws Exception { + mockPost("/api/task/item/stop", ""); this.requestGet(PROJECT_TASK_ITEM_STOP + "pro_1"); MvcResult mvcResult = this.requestGetWithOkAndReturn(PROJECT_TASK_ITEM_STOP + "pro_2"); // 获取返回值 @@ -205,6 +206,7 @@ public class ProjectTaskHubControllerTests extends BaseTest { @Test @Order(4) public void projectTaskItemBatchStop() throws Exception { + mockPost("/api/task/item/stop", ""); TaskHubItemBatchRequest request = new TaskHubItemBatchRequest(); request.setSelectAll(false); request.setSelectIds(List.of("pro_1", "pro_2")); @@ -253,4 +255,13 @@ public class ProjectTaskHubControllerTests extends BaseTest { request.setSelectAll(true); this.requestPost(PROJECT_SCHEDULE_TASK_BATCH_DISABLE, request); } + + @Test + @Order(9) + public void projectScheduleUpdateCron() throws Exception { + ScheduleRequest request = new ScheduleRequest(); + request.setCron("0 0 0 1 * ?"); + request.setId("pro_wx_1"); + this.requestPost(PROJECT_SCHEDULE_TASK_UPDATE_CRON, request); + } } 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 cef5af1f6d..7843bb1a91 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 @@ -9,6 +9,7 @@ import io.metersphere.system.dto.taskhub.ResourcePoolOptionsDTO; import io.metersphere.system.dto.taskhub.TaskHubDTO; import io.metersphere.system.dto.taskhub.TaskHubItemDTO; import io.metersphere.system.dto.taskhub.TaskHubScheduleDTO; +import io.metersphere.system.dto.taskhub.request.ScheduleRequest; import io.metersphere.system.dto.taskhub.request.TaskHubItemBatchRequest; import io.metersphere.system.dto.taskhub.request.TaskHubItemRequest; import io.metersphere.system.dto.taskhub.response.TaskStatisticsResponse; @@ -181,10 +182,18 @@ public class OrganizationTaskHubController { @PostMapping("/schedule/batch-disable") @Operation(summary = "组织-任务中心-后台任务-批量关闭") - @RequiresPermissions(PermissionConstants.SYSTEM_SCHEDULE_TASK_CENTER_READ_UPDATE) + @RequiresPermissions(PermissionConstants.ORGANIZATION_SCHEDULE_TASK_CENTER_READ_UPDATE) public void batchDisable(@Validated @RequestBody TableBatchProcessDTO request) { List projectList = baseProjectMapper.getProjectOptionsByOrgId(SessionUtils.getCurrentOrganizationId()); List projectIds = projectList.stream().map(OptionDTO::getId).toList(); baseTaskHubService.scheduleBatchOperation(request, SessionUtils.getUserId(), null, "/organization/task-center/schedule/batch-disable", OperationLogModule.SETTING_ORGANIZATION_TASK_CENTER, false, projectIds); } + + + @PostMapping("/schedule/update-cron") + @Operation(summary = "组织-任务中心-后台任务更新cron表达式") + @RequiresPermissions(PermissionConstants.ORGANIZATION_SCHEDULE_TASK_CENTER_READ_UPDATE) + public void updateValue(@PathVariable ScheduleRequest request) { + baseTaskHubService.updateCron(request); + } } 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 7b18ef3f66..85b9e70266 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 @@ -5,12 +5,14 @@ import io.metersphere.sdk.constants.PermissionConstants; import io.metersphere.system.dto.sdk.BasePageRequest; import io.metersphere.system.dto.table.TableBatchProcessDTO; import io.metersphere.system.dto.taskhub.*; +import io.metersphere.system.dto.taskhub.request.ScheduleRequest; 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.annotation.Log; import io.metersphere.system.log.constants.OperationLogModule; import io.metersphere.system.log.constants.OperationLogType; +import io.metersphere.system.security.CheckOwner; import io.metersphere.system.service.BaseTaskHubLogService; import io.metersphere.system.service.BaseTaskHubService; import io.metersphere.system.utils.Pager; @@ -185,5 +187,12 @@ public class SystemTaskHubController { baseTaskHubService.scheduleBatchOperation(request, SessionUtils.getUserId(), null, "/system/task-center/schedule/batch-disable", OperationLogModule.SETTING_SYSTEM_TASK_CENTER, false, null); } - //TODO 系统&组织&项目 后台任务操作:删除 批量开启 批量关闭 + + @PostMapping("/schedule/update-cron") + @Operation(summary = "系统-任务中心-后台任务更新cron表达式") + @RequiresPermissions(PermissionConstants.SYSTEM_SCHEDULE_TASK_CENTER_READ_UPDATE) + public void updateValue(@PathVariable ScheduleRequest request) { + baseTaskHubService.updateCron(request); + } + } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/dto/taskhub/request/ScheduleRequest.java b/backend/services/system-setting/src/main/java/io/metersphere/system/dto/taskhub/request/ScheduleRequest.java new file mode 100644 index 0000000000..fb9825e980 --- /dev/null +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/dto/taskhub/request/ScheduleRequest.java @@ -0,0 +1,22 @@ +package io.metersphere.system.dto.taskhub.request; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import java.io.Serial; +import java.io.Serializable; + +/** + * @author wx + */ +@Data +public class ScheduleRequest implements Serializable { + @Serial + private static final long serialVersionUID = 1L; + + @Schema(description = "列表id") + private String id; + + @Schema(description = "cron表达式") + private String cron; +} 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 73cd6a4def..fe0f9997a5 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 @@ -27,6 +27,7 @@ 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.ScheduleRequest; import io.metersphere.system.dto.taskhub.request.TaskHubItemBatchRequest; import io.metersphere.system.dto.taskhub.request.TaskHubItemRequest; import io.metersphere.system.dto.taskhub.response.TaskStatisticsResponse; @@ -117,6 +118,7 @@ public class BaseTaskHubService { @Resource ApiScheduleNoticeService apiScheduleNoticeService; + /** * 系统-获取执行任务列表 * @@ -824,4 +826,16 @@ public class BaseTaskHubService { return list; } + public void updateCron(ScheduleRequest request) { + Schedule schedule = checkScheduleExit(request.getId()); + schedule.setValue(request.getCron()); + scheduleService.editSchedule(schedule); + try { + scheduleService.addOrUpdateCronJob(schedule, new JobKey(schedule.getKey(), schedule.getJob()), + new TriggerKey(schedule.getKey(), schedule.getJob()), Class.forName(schedule.getJob())); + } catch (ClassNotFoundException e) { + LogUtils.error(e); + throw new RuntimeException(e); + } + } } 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 3a16c6eaad..3ea7c82e65 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 @@ -1,5 +1,6 @@ package io.metersphere.system.controller; +import io.metersphere.sdk.constants.SessionConstants; import io.metersphere.sdk.util.JSON; import io.metersphere.system.base.BaseTest; import io.metersphere.system.controller.handler.ResultHolder; @@ -7,6 +8,7 @@ import io.metersphere.system.domain.ExecTask; import io.metersphere.system.domain.ExecTaskItem; import io.metersphere.system.dto.sdk.BasePageRequest; import io.metersphere.system.dto.table.TableBatchProcessDTO; +import io.metersphere.system.dto.taskhub.request.ScheduleRequest; import io.metersphere.system.dto.taskhub.request.TaskHubItemBatchRequest; import io.metersphere.system.dto.taskhub.request.TaskHubItemRequest; import io.metersphere.system.mapper.TestResourcePoolMapper; @@ -15,15 +17,20 @@ import jakarta.annotation.Resource; import org.junit.jupiter.api.*; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; import org.springframework.test.context.jdbc.Sql; import org.springframework.test.context.jdbc.SqlConfig; import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @AutoConfigureMockMvc @TestMethodOrder(MethodOrderer.OrderAnnotation.class) @@ -55,6 +62,7 @@ public class BaseTaskHubControllerTests extends BaseTest { public static final String SYSTEM_SCHEDULE_TASK_SWITCH = "/system/task-center/schedule/switch/"; public static final String SYSTEM_SCHEDULE_TASK_BATCH_ENABLE = "/system/task-center/schedule/batch-enable"; public static final String SYSTEM_SCHEDULE_TASK_BATCH_DISABLE = "/system/task-center/schedule/batch-disable"; + public static final String SYSTEM_SCHEDULE_TASK_UPDATE_CRON = "/system/task-center/schedule/update-cron"; @Test @Order(1) @@ -162,13 +170,10 @@ public class BaseTaskHubControllerTests extends BaseTest { @Test @Order(4) public void systemTaskStop() throws Exception { + mockPost("/api/task/item/stop", ""); this.requestGet(SYSTEM_TASK_STOP + "1"); - MvcResult mvcResult = this.requestGetWithOkAndReturn(SYSTEM_TASK_STOP + "2"); - // 获取返回值 - String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); - ResultHolder resultHolder = JSON.parseObject(returnData, ResultHolder.class); - // 返回请求正常 - Assertions.assertNotNull(resultHolder); + mockPost("/api/task/stop", ""); + this.requestGet(SYSTEM_TASK_STOP + "2"); } /** @@ -177,13 +182,10 @@ public class BaseTaskHubControllerTests extends BaseTest { @Test @Order(4) public void systemTaskItemStop() throws Exception { + mockPost("/api/task/item/stop", ""); this.requestGet(SYSTEM_TASK_ITEM_STOP + "1"); - MvcResult mvcResult = this.requestGetWithOkAndReturn(SYSTEM_TASK_ITEM_STOP + "2"); - // 获取返回值 - String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); - ResultHolder resultHolder = JSON.parseObject(returnData, ResultHolder.class); - // 返回请求正常 - Assertions.assertNotNull(resultHolder); + mockPost("/api/task/stop", ""); + this.requestGet(SYSTEM_TASK_ITEM_STOP + "2"); } @Test @@ -231,6 +233,7 @@ public class BaseTaskHubControllerTests extends BaseTest { @Test @Order(5) public void systemTaskBatchStop() throws Exception { + mockPost("/api/task/item/stop", ""); TableBatchProcessDTO request = new TableBatchProcessDTO(); request.setSelectAll(true); this.requestPost(SYSTEM_TASK_BATCH_STOP, request); @@ -245,6 +248,7 @@ public class BaseTaskHubControllerTests extends BaseTest { @Test @Order(5) public void systemTaskItemBatchStop() throws Exception { + mockPost("/api/task/item/stop", ""); TableBatchProcessDTO request = new TableBatchProcessDTO(); request.setSelectAll(true); this.requestPost(SYSTEM_TASK_ITEM_BATCH_STOP, request); @@ -291,6 +295,15 @@ public class BaseTaskHubControllerTests extends BaseTest { this.requestPost(SYSTEM_SCHEDULE_TASK_BATCH_DISABLE, request); } + @Test + @Order(9) + public void systemScheduleUpdateCron() throws Exception { + ScheduleRequest request = new ScheduleRequest(); + request.setCron("0 0 0 1 * ?"); + request.setId("wx_1"); + this.requestPost(SYSTEM_SCHEDULE_TASK_UPDATE_CRON, request); + } + /** * 组织任务中心测试用例 */ @@ -310,6 +323,7 @@ public class BaseTaskHubControllerTests extends BaseTest { public static final String ORG_SCHEDULE_TASK_SWITCH = "/organization/task-center/schedule/switch/"; public static final String ORG_SCHEDULE_TASK_BATCH_ENABLE = "/organization/task-center/schedule/batch-enable"; public static final String ORG_SCHEDULE_TASK_BATCH_DISABLE = "/organization/task-center/schedule/batch-disable"; + public static final String ORG_SCHEDULE_TASK_UPDATE_CRON = "/organization/task-center/schedule/update-cron"; @Test @Order(20) @@ -523,6 +537,15 @@ public class BaseTaskHubControllerTests extends BaseTest { this.requestPost(ORG_SCHEDULE_TASK_BATCH_DISABLE, request); } + @Test + @Order(9) + public void orgScheduleUpdateCron() throws Exception { + ScheduleRequest request = new ScheduleRequest(); + request.setCron("0 0 0 1 * ?"); + request.setId("wx_1"); + this.requestPost(ORG_SCHEDULE_TASK_UPDATE_CRON, request); + } + @Test @Order(21)