refactor(系统设置): 增加关闭开启定时任务

This commit is contained in:
wxg0103 2024-04-07 11:53:34 +08:00 committed by Craftsman
parent 84904194f6
commit c97d58629c
8 changed files with 84 additions and 9 deletions

View File

@ -58,5 +58,13 @@ public class TaskCenterController {
taskCenterService.delete(id); taskCenterService.delete(id);
} }
@GetMapping("/schedule/switch/{id}")
@Operation(summary = "系统-任务中心-关闭/开启定时任务")
@CheckOwner(resourceId = "#id", resourceType = "schedule")
public void enable(@PathVariable String id) {
taskCenterService.enable(id);
}
} }

View File

@ -5,6 +5,7 @@ import com.github.pagehelper.page.PageMethod;
import io.metersphere.project.domain.Project; import io.metersphere.project.domain.Project;
import io.metersphere.project.mapper.ProjectMapper; import io.metersphere.project.mapper.ProjectMapper;
import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.util.LogUtils;
import io.metersphere.sdk.util.Translator; import io.metersphere.sdk.util.Translator;
import io.metersphere.system.domain.Organization; import io.metersphere.system.domain.Organization;
import io.metersphere.system.domain.Schedule; import io.metersphere.system.domain.Schedule;
@ -18,13 +19,11 @@ import io.metersphere.system.utils.PageUtils;
import io.metersphere.system.utils.Pager; import io.metersphere.system.utils.Pager;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.quartz.CronExpression; import org.quartz.*;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.TriggerBuilder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.lang.reflect.Method;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -204,4 +203,28 @@ public class TaskCenterService {
return schedule; 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);
}
}
} }

View File

@ -1,7 +1,7 @@
package io.metersphere.system.controller; package io.metersphere.system.controller;
import io.metersphere.api.domain.ApiDefinitionSwagger; import io.metersphere.sdk.constants.ScheduleResourceType;
import io.metersphere.api.mapper.ApiDefinitionSwaggerMapper; import io.metersphere.sdk.constants.ScheduleType;
import io.metersphere.sdk.constants.SessionConstants; import io.metersphere.sdk.constants.SessionConstants;
import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.LogUtils; 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.dto.taskcenter.request.TaskCenterSchedulePageRequest;
import io.metersphere.system.mapper.ExtSwaggerMapper; import io.metersphere.system.mapper.ExtSwaggerMapper;
import io.metersphere.system.mapper.ScheduleMapper; import io.metersphere.system.mapper.ScheduleMapper;
import io.metersphere.system.schedule.ScheduleService;
import io.metersphere.system.utils.Pager; import io.metersphere.system.utils.Pager;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.junit.jupiter.api.*; import org.junit.jupiter.api.*;
@ -51,6 +52,8 @@ class TaskCenterScheduleControllerTests extends BaseTest {
@Resource @Resource
ExtSwaggerMapper extSwaggerMapper; ExtSwaggerMapper extSwaggerMapper;
@Resource
ScheduleService scheduleService;
@ -167,4 +170,28 @@ class TaskCenterScheduleControllerTests extends BaseTest {
this.requestGet(SCHEDULED_DELETE + "schedule-121", ERROR_REQUEST_MATCHER); 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");
}
} }

View File

@ -4,6 +4,7 @@ import {
batchStopRealProjectApiUrl, batchStopRealProjectApiUrl,
batchStopRealSystemApiUrl, batchStopRealSystemApiUrl,
deleteScheduleSysTaskUrl, deleteScheduleSysTaskUrl,
enableSchedule,
scheduleOrgCenterListUrl, scheduleOrgCenterListUrl,
scheduleProCenterListUrl, scheduleProCenterListUrl,
scheduleSysCenterListUrl, scheduleSysCenterListUrl,
@ -70,4 +71,8 @@ export function getScheduleProApiCaseList(data: TableQueryParams) {
export function deleteScheduleSysTask(id: string) { export function deleteScheduleSysTask(id: string) {
return MSR.get({ url: `${deleteScheduleSysTaskUrl}/${id}` }); return MSR.get({ url: `${deleteScheduleSysTaskUrl}/${id}` });
} }
export function switchSchedule(id: string) {
return MSR.get({ url: `${enableSchedule}/${id}` });
}
export default {}; export default {};

View File

@ -8,6 +8,8 @@ export const deleteScheduleSysTaskUrl = '/task/center/schedule/delete';
// 系统接口用例和场景停止实时任务 // 系统接口用例和场景停止实时任务
export const batchStopRealSystemApiUrl = '/task/center/api/system/stop'; 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'; export const taskOrgRealCenterListUrl = '/task/center/api/org/real-time/page';

View File

@ -28,7 +28,12 @@
<a-button type="text" class="flex w-full">{{ record.resourceName }}</a-button> <a-button type="text" class="flex w-full">{{ record.resourceName }}</a-button>
</template> </template>
<template #operation="{ record }"> <template #operation="{ record }">
<a-switch v-model="record.enable" size="small" type="line" /> <a-switch
v-model="record.enable"
size="small"
type="line"
:before-change="() => handleBeforeEnableChange(record)"
/>
<a-divider direction="vertical" /> <a-divider direction="vertical" />
<MsButton class="!mr-0" @click="delSchedule(record)">{{ t('common.delete') }}</MsButton> <MsButton class="!mr-0" @click="delSchedule(record)">{{ t('common.delete') }}</MsButton>
<!-- TODO这一版不上 --> <!-- TODO这一版不上 -->
@ -58,6 +63,7 @@
getScheduleOrgApiCaseList, getScheduleOrgApiCaseList,
getScheduleProApiCaseList, getScheduleProApiCaseList,
getScheduleSysApiCaseList, getScheduleSysApiCaseList,
switchSchedule,
} from '@/api/modules/project-management/taskCenter'; } from '@/api/modules/project-management/taskCenter';
import { useI18n } from '@/hooks/useI18n'; import { useI18n } from '@/hooks/useI18n';
import useModal from '@/hooks/useModal'; import useModal from '@/hooks/useModal';
@ -231,9 +237,9 @@
async function handleBeforeEnableChange(record: TimingTaskCenterApiCaseItem) { async function handleBeforeEnableChange(record: TimingTaskCenterApiCaseItem) {
try { try {
await switchDefinitionSchedule(record.id); await switchSchedule(record?.id as string);
Message.success( Message.success(
t(record.enable ? 'apiTestManagement.disableTaskSuccess' : 'apiTestManagement.enableTaskSuccess') t(record.enable ? 'project.taskCenter.disableScheduleSuccess' : 'project.taskCenter.enableScheduleSuccess')
); );
return true; return true;
} catch (error) { } catch (error) {

View File

@ -45,4 +45,6 @@ export default {
'project.taskCenter.delScheduleSuccess': 'Delete scheduled task successfully', 'project.taskCenter.delScheduleSuccess': 'Delete scheduled task successfully',
'project.taskCenter.delSchedule.tip': 'project.taskCenter.delSchedule.tip':
'Deleting the scheduled task will cause the task to stop. Do you want to continue?', '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',
}; };

View File

@ -43,4 +43,6 @@ export default {
'project.taskCenter.delSchedule': '确定删除定时任务吗?', 'project.taskCenter.delSchedule': '确定删除定时任务吗?',
'project.taskCenter.delScheduleSuccess': '删除定时任务成功', 'project.taskCenter.delScheduleSuccess': '删除定时任务成功',
'project.taskCenter.delSchedule.tip': '删除定时任务会导致任务停止,是否继续?', 'project.taskCenter.delSchedule.tip': '删除定时任务会导致任务停止,是否继续?',
'project.taskCenter.enableScheduleSuccess': '启用定时任务成功',
'project.taskCenter.disableScheduleSuccess': '关闭定时任务成功',
}; };