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 }}
-
+
{{ t('common.delete') }}
@@ -58,6 +63,7 @@
getScheduleOrgApiCaseList,
getScheduleProApiCaseList,
getScheduleSysApiCaseList,
+ switchSchedule,
} from '@/api/modules/project-management/taskCenter';
import { useI18n } from '@/hooks/useI18n';
import useModal from '@/hooks/useModal';
@@ -231,9 +237,9 @@
async function handleBeforeEnableChange(record: TimingTaskCenterApiCaseItem) {
try {
- await switchDefinitionSchedule(record.id);
+ await switchSchedule(record?.id as string);
Message.success(
- t(record.enable ? 'apiTestManagement.disableTaskSuccess' : 'apiTestManagement.enableTaskSuccess')
+ t(record.enable ? 'project.taskCenter.disableScheduleSuccess' : 'project.taskCenter.enableScheduleSuccess')
);
return true;
} catch (error) {
diff --git a/frontend/src/views/project-management/taskCenter/locale/en-US.ts b/frontend/src/views/project-management/taskCenter/locale/en-US.ts
index 372e8f0dd9..f5b2c03f03 100644
--- a/frontend/src/views/project-management/taskCenter/locale/en-US.ts
+++ b/frontend/src/views/project-management/taskCenter/locale/en-US.ts
@@ -45,4 +45,6 @@ export default {
'project.taskCenter.delScheduleSuccess': 'Delete scheduled task successfully',
'project.taskCenter.delSchedule.tip':
'Deleting the scheduled task will cause the task to stop. Do you want to continue?',
+ 'project.taskCenter.enableScheduleSuccess': 'Enable scheduled task successfully',
+ 'project.taskCenter.disableScheduleSuccess': 'Disable scheduled task successfully',
};
diff --git a/frontend/src/views/project-management/taskCenter/locale/zh-CN.ts b/frontend/src/views/project-management/taskCenter/locale/zh-CN.ts
index ede890feb4..4a218ece37 100644
--- a/frontend/src/views/project-management/taskCenter/locale/zh-CN.ts
+++ b/frontend/src/views/project-management/taskCenter/locale/zh-CN.ts
@@ -43,4 +43,6 @@ export default {
'project.taskCenter.delSchedule': '确定删除定时任务吗?',
'project.taskCenter.delScheduleSuccess': '删除定时任务成功',
'project.taskCenter.delSchedule.tip': '删除定时任务会导致任务停止,是否继续?',
+ 'project.taskCenter.enableScheduleSuccess': '启用定时任务成功',
+ 'project.taskCenter.disableScheduleSuccess': '关闭定时任务成功',
};