refactor(系統): 初始化定时任务分页处理

This commit is contained in:
WangXu10 2024-04-08 11:22:41 +08:00 committed by 刘瑞斌
parent 693a2f9fef
commit fd5ad458be
3 changed files with 23 additions and 10 deletions

View File

@ -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<Schedule> getScheduleByLimit(@Param("start") int start, @Param("limit") int limit);
}

View File

@ -118,5 +118,8 @@
WHERE resource_id = #{0}
</select>
<select id="getScheduleByLimit" resultType="io.metersphere.system.domain.Schedule">
SELECT * FROM schedule ORDER BY create_time LIMIT #{start}, #{limit}
</select>
</mapper>

View File

@ -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,10 +23,23 @@ public class BaseScheduleService {
private ScheduleMapper scheduleMapper;
@Resource
private ScheduleManager scheduleManager;
@Resource
private ExtScheduleMapper extScheduleMapper;
public void startEnableSchedules() {
List<Schedule> 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<Schedule> schedules = extScheduleMapper.getScheduleByLimit(start, 100);
doHandleSchedule(schedules);
}
}
private void doHandleSchedule(List<Schedule> schedules) {
schedules.forEach(schedule -> {
try {
if (schedule.getEnable()) {
LogUtils.info("初始化任务:" + JSON.toJSONString(schedule));
@ -40,13 +54,6 @@ public class BaseScheduleService {
LogUtils.error("初始化任务失败", e);
}
});
}
private List<Schedule> getSchedule() {
ScheduleExample example = new ScheduleExample();
example.createCriteria();
return scheduleMapper.selectByExample(example);
}
private void removeJob(Schedule schedule) {