From e07de33f38d9c6563e178b9a8c9ba149a498a835 Mon Sep 17 00:00:00 2001 From: chenjianxing Date: Fri, 21 Apr 2023 11:47:39 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E9=80=9A=E7=94=A8=E5=8A=9F=E8=83=BD):=20?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E6=97=B6=E9=87=8D=E6=96=B0=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/constants/ScheduleGroup.java | 2 +- .../service/BaseScheduleService.java | 18 +++++++++-------- .../listener/ProjectAppStartListener.java | 9 +++++++++ .../listener/InitListener.java | 20 +++++++++++++++++++ .../io/metersphere/listener/InitListener.java | 5 +++-- 5 files changed, 43 insertions(+), 11 deletions(-) create mode 100644 report-stat/backend/src/main/java/io/metersphere/reportstatistics/listener/InitListener.java diff --git a/framework/sdk-parent/sdk/src/main/java/io/metersphere/commons/constants/ScheduleGroup.java b/framework/sdk-parent/sdk/src/main/java/io/metersphere/commons/constants/ScheduleGroup.java index abd3656e48..378c0a588b 100644 --- a/framework/sdk-parent/sdk/src/main/java/io/metersphere/commons/constants/ScheduleGroup.java +++ b/framework/sdk-parent/sdk/src/main/java/io/metersphere/commons/constants/ScheduleGroup.java @@ -1,6 +1,6 @@ package io.metersphere.commons.constants; public enum ScheduleGroup { - API_TEST, PERFORMANCE_TEST, API_SCENARIO_TEST, TEST_PLAN_TEST, SWAGGER_IMPORT, ISSUE_SYNC, + PERFORMANCE_TEST, API_SCENARIO_TEST, TEST_PLAN_TEST, SWAGGER_IMPORT, ISSUE_SYNC, SCHEDULE_SEND_REPORT, CLEAN_UP_REPORT, UI_SCENARIO_TEST } diff --git a/framework/sdk-parent/sdk/src/main/java/io/metersphere/service/BaseScheduleService.java b/framework/sdk-parent/sdk/src/main/java/io/metersphere/service/BaseScheduleService.java index 897c4cae53..512fae7ae1 100644 --- a/framework/sdk-parent/sdk/src/main/java/io/metersphere/service/BaseScheduleService.java +++ b/framework/sdk-parent/sdk/src/main/java/io/metersphere/service/BaseScheduleService.java @@ -129,13 +129,13 @@ public class BaseScheduleService { return scheduleMapper.deleteByExample(scheduleExample); } - private void removeJob(String resourceId, String group) { - scheduleManager.removeJob(new JobKey(resourceId, ScheduleGroup.PERFORMANCE_TEST.name()), new TriggerKey(resourceId, group)); + private void removeJob(String key, String group) { + scheduleManager.removeJob(new JobKey(key, group), new TriggerKey(key, group)); } - public List getEnableSchedule(ScheduleGroup group) { + public List getScheduleByGroup(ScheduleGroup group) { ScheduleExample example = new ScheduleExample(); - example.createCriteria().andEnableEqualTo(true) + example.createCriteria() .andGroupEqualTo(group.name()); return scheduleMapper.selectByExample(example); } @@ -149,17 +149,19 @@ public class BaseScheduleService { } public void startEnableSchedules(ScheduleGroup group) { - List Schedules = getEnableSchedule(group); - + List Schedules = getScheduleByGroup(group); Schedules.forEach(schedule -> { try { + // 兼容历史数据 + jobConvert(schedule, group); if (schedule.getEnable()) { - // 兼容历史数据 - jobConvert(schedule, group); LogUtil.info("初始化任务:" + JSON.toJSONString(schedule)); scheduleManager.addOrUpdateCronJob(new JobKey(schedule.getKey(), schedule.getGroup()), new TriggerKey(schedule.getKey(), schedule.getGroup()), Class.forName(schedule.getJob()), schedule.getValue(), scheduleManager.getDefaultJobDataMap(schedule, schedule.getValue(), schedule.getUserId())); + } else { + // 删除关闭的job + removeJob(schedule.getKey(), group.toString()); } } catch (Exception e) { LogUtil.error("初始化任务失败", e); diff --git a/project-management/backend/src/main/java/io/metersphere/listener/ProjectAppStartListener.java b/project-management/backend/src/main/java/io/metersphere/listener/ProjectAppStartListener.java index ce5b335750..920f944809 100644 --- a/project-management/backend/src/main/java/io/metersphere/listener/ProjectAppStartListener.java +++ b/project-management/backend/src/main/java/io/metersphere/listener/ProjectAppStartListener.java @@ -1,6 +1,9 @@ package io.metersphere.listener; +import io.metersphere.commons.constants.ScheduleGroup; import io.metersphere.commons.utils.LogUtil; +import io.metersphere.service.BaseScheduleService; +import jakarta.annotation.Resource; import org.apache.jmeter.util.JMeterUtils; import org.python.core.Options; import org.python.util.PythonInterpreter; @@ -13,6 +16,10 @@ import java.util.Objects; @Component public class ProjectAppStartListener implements ApplicationListener { + + @Resource + private BaseScheduleService baseScheduleService; + @Override public void onApplicationEvent(ApplicationReadyEvent event) { LogUtil.info("================= PROJECT 应用启动 ================="); @@ -21,6 +28,8 @@ public class ProjectAppStartListener implements ApplicationListener