diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/TaskCenterController.java b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/TaskCenterController.java index 4bfb50c175..3c060f026b 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/TaskCenterController.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/TaskCenterController.java @@ -58,5 +58,13 @@ public class TaskCenterController { taskCenterService.delete(id); } + @GetMapping("/schedule/switch/{id}") + @Operation(summary = "系统-任务中心-关闭/开启定时任务") + @CheckOwner(resourceId = "#id", resourceType = "schedule") + public void enable(@PathVariable String id) { + taskCenterService.enable(id); + } + + } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/TaskCenterService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/TaskCenterService.java index c874e73f58..6c4fa9f4f9 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/TaskCenterService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/TaskCenterService.java @@ -5,6 +5,7 @@ import com.github.pagehelper.page.PageMethod; import io.metersphere.project.domain.Project; import io.metersphere.project.mapper.ProjectMapper; import io.metersphere.sdk.exception.MSException; +import io.metersphere.sdk.util.LogUtils; import io.metersphere.sdk.util.Translator; import io.metersphere.system.domain.Organization; import io.metersphere.system.domain.Schedule; @@ -18,13 +19,11 @@ import io.metersphere.system.utils.PageUtils; import io.metersphere.system.utils.Pager; import jakarta.annotation.Resource; import org.apache.commons.lang3.StringUtils; -import org.quartz.CronExpression; -import org.quartz.CronScheduleBuilder; -import org.quartz.CronTrigger; -import org.quartz.TriggerBuilder; +import org.quartz.*; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.lang.reflect.Method; import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -204,4 +203,28 @@ public class TaskCenterService { return schedule; } + public void enable(String id) { + Schedule schedule = checkScheduleExit(id); + //根据全路径获取类 通过反射获取类 + schedule.setEnable(!schedule.getEnable()); + scheduleService.editSchedule(schedule); + try { + String className = schedule.getJob(); + Class clazz = Class.forName(className); + Object instance = clazz.getDeclaredConstructor().newInstance(); + + // 调用无参方法 + Method method = clazz.getMethod("getTriggerKey", String.class); + Object result = method.invoke(instance, schedule.getResourceId()); + + Method method1 = clazz.getMethod("getJobKey", String.class); + Object result1 = method1.invoke(instance, schedule.getResourceId()); + + scheduleService.addOrUpdateCronJob(schedule, (JobKey) result1, (TriggerKey) result, clazz); + } catch (Exception e) { + LogUtils.error("enable schedule error", e); + } + + + } } diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/TaskCenterScheduleControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/TaskCenterScheduleControllerTests.java index 069c9c2c21..cc2cfa580a 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/TaskCenterScheduleControllerTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/TaskCenterScheduleControllerTests.java @@ -1,7 +1,7 @@ package io.metersphere.system.controller; -import io.metersphere.api.domain.ApiDefinitionSwagger; -import io.metersphere.api.mapper.ApiDefinitionSwaggerMapper; +import io.metersphere.sdk.constants.ScheduleResourceType; +import io.metersphere.sdk.constants.ScheduleType; import io.metersphere.sdk.constants.SessionConstants; import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.LogUtils; @@ -12,6 +12,7 @@ import io.metersphere.system.dto.taskcenter.enums.ScheduleTagType; import io.metersphere.system.dto.taskcenter.request.TaskCenterSchedulePageRequest; import io.metersphere.system.mapper.ExtSwaggerMapper; import io.metersphere.system.mapper.ScheduleMapper; +import io.metersphere.system.schedule.ScheduleService; import io.metersphere.system.utils.Pager; import jakarta.annotation.Resource; import org.junit.jupiter.api.*; @@ -51,6 +52,8 @@ class TaskCenterScheduleControllerTests extends BaseTest { @Resource ExtSwaggerMapper extSwaggerMapper; + @Resource + ScheduleService scheduleService; @@ -167,4 +170,28 @@ class TaskCenterScheduleControllerTests extends BaseTest { this.requestGet(SCHEDULED_DELETE + "schedule-121", ERROR_REQUEST_MATCHER); } + @Test + @Order(13) + void testEnable() throws Exception { + //先导入数据 + Schedule schedule = new Schedule(); + schedule.setName("test-schedule-switch"); + schedule.setResourceId("test-schedule-switch"); + schedule.setEnable(true); + schedule.setValue("0 0/1 * * * ?"); + schedule.setKey("test-resource-id"); + schedule.setCreateUser("admin"); + schedule.setProjectId(DEFAULT_PROJECT_ID); + schedule.setConfig("{}"); + schedule.setJob("io.metersphere.api.service.schedule.SwaggerUrlImportJob"); + schedule.setType(ScheduleType.CRON.name()); + schedule.setResourceType(ScheduleResourceType.API_IMPORT.name()); + + scheduleService.addSchedule(schedule); + scheduleService.getSchedule(schedule.getId()); + scheduleService.editSchedule(schedule); + scheduleService.getScheduleByResource(schedule.getResourceId(), schedule.getJob()); + this.requestGet("/task/center/schedule/switch" + "test-schedule-switch"); + } + } diff --git a/frontend/src/api/modules/project-management/taskCenter.ts b/frontend/src/api/modules/project-management/taskCenter.ts index cd3c99cdd0..85f7da61c7 100644 --- a/frontend/src/api/modules/project-management/taskCenter.ts +++ b/frontend/src/api/modules/project-management/taskCenter.ts @@ -4,6 +4,7 @@ import { batchStopRealProjectApiUrl, batchStopRealSystemApiUrl, deleteScheduleSysTaskUrl, + enableSchedule, scheduleOrgCenterListUrl, scheduleProCenterListUrl, scheduleSysCenterListUrl, @@ -70,4 +71,8 @@ export function getScheduleProApiCaseList(data: TableQueryParams) { export function deleteScheduleSysTask(id: string) { return MSR.get({ url: `${deleteScheduleSysTaskUrl}/${id}` }); } + +export function switchSchedule(id: string) { + return MSR.get({ url: `${enableSchedule}/${id}` }); +} export default {}; diff --git a/frontend/src/api/requrls/project-management/taskCenter.ts b/frontend/src/api/requrls/project-management/taskCenter.ts index 4fb9b0090a..bcea69da22 100644 --- a/frontend/src/api/requrls/project-management/taskCenter.ts +++ b/frontend/src/api/requrls/project-management/taskCenter.ts @@ -8,6 +8,8 @@ export const deleteScheduleSysTaskUrl = '/task/center/schedule/delete'; // 系统接口用例和场景停止实时任务 export const batchStopRealSystemApiUrl = '/task/center/api/system/stop'; +export const enableSchedule = '/task/center/schedule/switch'; + // 组织管理 // 任务中心-实时任务-接口用例/场景 export const taskOrgRealCenterListUrl = '/task/center/api/org/real-time/page'; diff --git a/frontend/src/views/project-management/taskCenter/component/scheduledTask.vue b/frontend/src/views/project-management/taskCenter/component/scheduledTask.vue index 1f590c2890..219594bb46 100644 --- a/frontend/src/views/project-management/taskCenter/component/scheduledTask.vue +++ b/frontend/src/views/project-management/taskCenter/component/scheduledTask.vue @@ -28,7 +28,12 @@ {{ record.resourceName }}