From 1ad79d1c43000a90e9724def3f4e38e15b45a1a6 Mon Sep 17 00:00:00 2001 From: WangXu10 Date: Sun, 20 Oct 2024 00:17:04 +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=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ProjectTaskHubController.java | 7 +++++++ .../ProjectTaskHubControllerTests.java | 7 +++++++ .../OrganizationTaskHubController.java | 8 ++++++++ .../controller/SystemTaskHubController.java | 7 +++++++ .../system/service/BaseTaskHubService.java | 20 +++++++++++++++++++ .../BaseTaskHubControllerTests.java | 14 +++++++++++++ .../resources/dml/init_exec_task_test.sql | 2 +- 7 files changed, 64 insertions(+), 1 deletion(-) 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 32941bf742..5f7818fa66 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 @@ -149,4 +149,11 @@ public class ProjectTaskHubController { public void deleteScheduleTask(@PathVariable String id) { baseTaskHubService.deleteScheduleTask(id, SessionUtils.getUserId(), "/project/task-center/schedule/delete/", OperationLogModule.PROJECT_MANAGEMENT_TASK_CENTER); } + + @GetMapping("/schedule/switch/{id}") + @Operation(summary = "项目-任务中心-后台任务开启关闭") + @RequiresPermissions(PermissionConstants.PROJECT_SCHEDULE_TASK_CENTER_READ_UPDATE) + public void enable(@PathVariable String id) { + baseTaskHubService.enable(id, SessionUtils.getUserId(), "/project/task-center/schedule/switch/", 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 82211024cc..a11fb99dda 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 @@ -35,6 +35,7 @@ public class ProjectTaskHubControllerTests extends BaseTest { 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/"; + public static final String PROJECT_SCHEDULE_TASK_SWITCH = "/project/task-center/schedule/switch/"; @Test @Order(1) @@ -223,4 +224,10 @@ public class ProjectTaskHubControllerTests extends BaseTest { // 返回请求正常 Assertions.assertNotNull(resultHolder); } + + @Test + @Order(6) + public void projectScheduleTaskSwitch() throws Exception { + this.requestGet(PROJECT_SCHEDULE_TASK_SWITCH + "pro_wx_1"); + } } 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 aad0a4ae73..1f6dd958f5 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 @@ -159,4 +159,12 @@ public class OrganizationTaskHubController { public void deleteScheduleTask(@PathVariable String id) { baseTaskHubService.deleteScheduleTask(id, SessionUtils.getUserId(), "/organization/task-center/schedule/delete/", OperationLogModule.SETTING_ORGANIZATION_TASK_CENTER); } + + + @GetMapping("/schedule/switch/{id}") + @Operation(summary = "组织-任务中心-后台任务开启关闭") + @RequiresPermissions(PermissionConstants.ORGANIZATION_SCHEDULE_TASK_CENTER_READ_UPDATE) + public void enable(@PathVariable String id) { + baseTaskHubService.enable(id, SessionUtils.getUserId(), "/organization/task-center/schedule/switch/", 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 67015f4cb2..258e341887 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 @@ -162,5 +162,12 @@ public class SystemTaskHubController { public void deleteScheduleTask(@PathVariable String id) { baseTaskHubService.deleteScheduleTask(id, SessionUtils.getUserId(), "/system/task-center/schedule/delete/", OperationLogModule.SETTING_SYSTEM_TASK_CENTER); } + + @GetMapping("/schedule/switch/{id}") + @Operation(summary = "系统-任务中心-后台任务开启关闭") + @RequiresPermissions(PermissionConstants.SYSTEM_SCHEDULE_TASK_CENTER_READ_UPDATE) + public void enable(@PathVariable String id) { + baseTaskHubService.enable(id, SessionUtils.getUserId(), "/system/task-center/schedule/switch/", 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 d0c31095e3..ae81bcf096 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 @@ -35,6 +35,7 @@ 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.ApiScheduleNoticeService; import io.metersphere.system.schedule.ScheduleService; import io.metersphere.system.utils.PageUtils; import io.metersphere.system.utils.Pager; @@ -46,6 +47,8 @@ import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionUtils; +import org.quartz.JobKey; +import org.quartz.TriggerKey; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; @@ -110,6 +113,8 @@ public class BaseTaskHubService { private ExtSwaggerMapper extSwaggerMapper; @Resource private OperationLogService operationLogService; + @Resource + ApiScheduleNoticeService apiScheduleNoticeService; /** * 系统-获取执行任务列表 @@ -762,4 +767,19 @@ public class BaseTaskHubService { } return schedule; } + + public void enable(String id, String userId, String path, String module) { + Schedule schedule = checkScheduleExit(id); + schedule.setEnable(!schedule.getEnable()); + 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); + } + apiScheduleNoticeService.sendScheduleNotice(schedule, userId); + saveLog(List.of(schedule), userId, path, HttpMethodConstants.GET.name(), module, OperationLogType.UPDATE.name()); + } } 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 35f41b9b91..f8c2ff7393 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 @@ -52,6 +52,7 @@ public class BaseTaskHubControllerTests extends BaseTest { 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/"; + public static final String SYSTEM_SCHEDULE_TASK_SWITCH = "/system/task-center/schedule/switch/"; @Test @Order(1) @@ -215,6 +216,12 @@ public class BaseTaskHubControllerTests extends BaseTest { this.requestPost(SYSTEM_TASK_BATCH_DELETE, request); } + @Test + @Order(6) + public void systemScheduleTaskSwitch() throws Exception { + this.requestGet(SYSTEM_SCHEDULE_TASK_SWITCH + "wx_1"); + } + /** * 系统执行任务停止 @@ -276,6 +283,7 @@ public class BaseTaskHubControllerTests extends BaseTest { 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/"; + public static final String ORG_SCHEDULE_TASK_SWITCH = "/organization/task-center/schedule/switch/"; @Test @Order(20) @@ -465,6 +473,12 @@ public class BaseTaskHubControllerTests extends BaseTest { Assertions.assertNotNull(resultHolder); } + @Test + @Order(27) + public void orgScheduleTaskSwitch() throws Exception { + this.requestGet(ORG_SCHEDULE_TASK_SWITCH + "wx_1"); + } + @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 93467c50ed..d51779e117 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,7 +10,7 @@ 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', '123456', 'admin', 1640776000000, 1640777000000, '100001100001', 'Schedule 1', '{\"param1\": \"value1\", \"param2\": \"value2\"}', 100), + ('wx_1', 'wx_key_1', 'cron', '1233213', 'io.metersphere.api.service.schedule.SwaggerUrlImportJob', '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),