From fd5ad458be5d9eeecdfac69e485228ffff3907d0 Mon Sep 17 00:00:00 2001 From: WangXu10 Date: Mon, 8 Apr 2024 11:22:41 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E7=B3=BB=E7=B5=B1):=20=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1=E5=88=86?= =?UTF-8?q?=E9=A1=B5=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/mapper/ExtScheduleMapper.java | 3 +++ .../system/mapper/ExtScheduleMapper.xml | 3 +++ .../system/service/BaseScheduleService.java | 27 ++++++++++++------- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtScheduleMapper.java b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtScheduleMapper.java index e12ec71723..7f01c7be89 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtScheduleMapper.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtScheduleMapper.java @@ -2,6 +2,7 @@ package io.metersphere.system.mapper; import io.metersphere.api.domain.ApiScenario; import io.metersphere.api.domain.ApiTestCase; +import io.metersphere.system.domain.Schedule; import io.metersphere.system.dto.taskcenter.TaskCenterScheduleDTO; import io.metersphere.system.dto.taskcenter.request.TaskCenterSchedulePageRequest; import org.apache.ibatis.annotations.Param; @@ -29,4 +30,6 @@ public interface ExtScheduleMapper { long countQuartzTriggersByResourceId(String scheduleId); long countQuartzCronTriggersByResourceId(String scheduleId); + + List getScheduleByLimit(@Param("start") int start, @Param("limit") int limit); } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtScheduleMapper.xml b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtScheduleMapper.xml index e78190b680..2b348e8366 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtScheduleMapper.xml +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtScheduleMapper.xml @@ -118,5 +118,8 @@ WHERE resource_id = #{0} + \ No newline at end of file diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseScheduleService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseScheduleService.java index 7b7b64a3e1..5ddcde7746 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseScheduleService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseScheduleService.java @@ -4,6 +4,7 @@ import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.LogUtils; import io.metersphere.system.domain.Schedule; import io.metersphere.system.domain.ScheduleExample; +import io.metersphere.system.mapper.ExtScheduleMapper; import io.metersphere.system.mapper.ScheduleMapper; import io.metersphere.system.schedule.ScheduleManager; import jakarta.annotation.Resource; @@ -22,15 +23,28 @@ public class BaseScheduleService { private ScheduleMapper scheduleMapper; @Resource private ScheduleManager scheduleManager; + @Resource + private ExtScheduleMapper extScheduleMapper; public void startEnableSchedules() { - List Schedules = getSchedule(); - Schedules.forEach(schedule -> { + ScheduleExample example = new ScheduleExample(); + example.createCriteria(); + long count = scheduleMapper.countByExample(example); + long pages = Double.valueOf(Math.ceil(count / 100.0)).longValue(); + for (int i = 0; i < pages; i++) { + int start = i * 100; + List schedules = extScheduleMapper.getScheduleByLimit(start, 100); + doHandleSchedule(schedules); + } + } + + private void doHandleSchedule(List schedules) { + schedules.forEach(schedule -> { try { if (schedule.getEnable()) { LogUtils.info("初始化任务:" + JSON.toJSONString(schedule)); scheduleManager.addOrUpdateCronJob(new JobKey(schedule.getKey(), schedule.getJob()), - new TriggerKey(schedule.getKey(),schedule.getJob()), Class.forName(schedule.getJob()), schedule.getValue(), + new TriggerKey(schedule.getKey(), schedule.getJob()), Class.forName(schedule.getJob()), schedule.getValue(), scheduleManager.getDefaultJobDataMap(schedule, schedule.getValue(), schedule.getCreateUser())); } else { // 删除关闭的job @@ -40,13 +54,6 @@ public class BaseScheduleService { LogUtils.error("初始化任务失败", e); } }); - - } - - private List getSchedule() { - ScheduleExample example = new ScheduleExample(); - example.createCriteria(); - return scheduleMapper.selectByExample(example); } private void removeJob(Schedule schedule) {