fix(通用功能): 启动时重新配置定时任务

This commit is contained in:
chenjianxing 2023-04-21 11:47:39 +08:00 committed by 刘瑞斌
parent 63b7057a50
commit e07de33f38
5 changed files with 43 additions and 11 deletions

View File

@ -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
}

View File

@ -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<Schedule> getEnableSchedule(ScheduleGroup group) {
public List<Schedule> 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<Schedule> Schedules = getEnableSchedule(group);
List<Schedule> Schedules = getScheduleByGroup(group);
Schedules.forEach(schedule -> {
try {
if (schedule.getEnable()) {
// 兼容历史数据
jobConvert(schedule, group);
if (schedule.getEnable()) {
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);

View File

@ -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<ApplicationReadyEvent> {
@Resource
private BaseScheduleService baseScheduleService;
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
LogUtil.info("================= PROJECT 应用启动 =================");
@ -21,6 +28,8 @@ public class ProjectAppStartListener implements ApplicationListener<ApplicationR
LogUtil.info("导入内置python包处理");
this.initPythonEnv();
baseScheduleService.startEnableSchedules(ScheduleGroup.CLEAN_UP_REPORT);
}
private void initJmeterHome() {

View File

@ -0,0 +1,20 @@
package io.metersphere.reportstatistics.listener;
import io.metersphere.commons.constants.ScheduleGroup;
import io.metersphere.service.BaseScheduleService;
import jakarta.annotation.Resource;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
@Component
public class InitListener implements ApplicationRunner {
@Resource
private BaseScheduleService baseScheduleService;
@Override
public void run(ApplicationArguments applicationArguments) throws Exception {
baseScheduleService.startEnableSchedules(ScheduleGroup.SCHEDULE_SEND_REPORT);
}
}

View File

@ -27,14 +27,15 @@ public class InitListener implements ApplicationRunner {
@Resource
private PlatformPluginService platformPluginService;
@Resource
private BaseScheduleService scheduleService;
private BaseScheduleService baseScheduleService;
@Override
public void run(ApplicationArguments applicationArguments) throws Exception {
this.initOnceOperate();
platformPluginService.loadPlatFormPlugins();
scheduleService.startEnableSchedules(ScheduleGroup.ISSUE_SYNC);
baseScheduleService.startEnableSchedules(ScheduleGroup.ISSUE_SYNC);
baseScheduleService.startEnableSchedules(ScheduleGroup.TEST_PLAN_TEST);
}
/**