refactor: 重启服务后定时任务重新初始化

This commit is contained in:
fit2-zhao 2024-11-05 17:37:31 +08:00 committed by Craftsman
parent 8b06b8f97b
commit bcce383b92
1 changed files with 27 additions and 14 deletions

View File

@ -27,11 +27,18 @@ public class BaseScheduleService {
@Resource @Resource
private ExtScheduleMapper extScheduleMapper; private ExtScheduleMapper extScheduleMapper;
private static final List<String> RESOURCE_TYPES = List.of(
ScheduleResourceType.API_IMPORT.name(),
ScheduleResourceType.API_SCENARIO.name(),
ScheduleResourceType.TEST_PLAN.name(),
ScheduleResourceType.BUG_SYNC.name(),
ScheduleResourceType.DEMAND_SYNC.name()
);
public void startEnableSchedules() { public void startEnableSchedules() {
ScheduleExample example = new ScheduleExample(); long count = scheduleMapper.countByExample(new ScheduleExample());
example.createCriteria(); long pages = (long) Math.ceil(count / 100.0);
long count = scheduleMapper.countByExample(example);
long pages = Double.valueOf(Math.ceil(count / 100.0)).longValue();
for (int i = 0; i < pages; i++) { for (int i = 0; i < pages; i++) {
int start = i * 100; int start = i * 100;
List<Schedule> schedules = extScheduleMapper.getScheduleByLimit(start, 100); List<Schedule> schedules = extScheduleMapper.getScheduleByLimit(start, 100);
@ -40,22 +47,25 @@ public class BaseScheduleService {
} }
private void doHandleSchedule(List<Schedule> schedules) { private void doHandleSchedule(List<Schedule> schedules) {
List<String> resourceTypes = List.of(ScheduleResourceType.API_IMPORT.name(), ScheduleResourceType.API_SCENARIO.name(), ScheduleResourceType.TEST_PLAN.name(), ScheduleResourceType.BUG_SYNC.name(), ScheduleResourceType.DEMAND_SYNC.name());
schedules.forEach(schedule -> { schedules.forEach(schedule -> {
try { try {
if (schedule.getEnable()) { if (schedule.getEnable()) {
if (resourceTypes.contains(schedule.getResourceType())) { if (RESOURCE_TYPES.contains(schedule.getResourceType())) {
// 删除关闭的job removeJob(schedule); // 删除关闭的job
removeJob(schedule);
} }
LogUtils.info("初始化任务:" + JSON.toJSONString(schedule)); LogUtils.info("初始化任务:" + JSON.toJSONString(schedule));
scheduleManager.addOrUpdateCronJob(new JobKey(schedule.getKey(), schedule.getJob()), scheduleManager.addOrUpdateCronJob(
new TriggerKey(schedule.getKey(), schedule.getJob()), Class.forName(schedule.getJob()), schedule.getValue(), new JobKey(schedule.getKey(), schedule.getJob()),
scheduleManager.getDefaultJobDataMap(schedule, schedule.getValue(), schedule.getCreateUser())); new TriggerKey(schedule.getKey(), schedule.getJob()),
Class.forName(schedule.getJob()),
schedule.getValue(),
scheduleManager.getDefaultJobDataMap(schedule, schedule.getValue(), schedule.getCreateUser())
);
} else { } else {
// 删除关闭的job removeJob(schedule); // 删除关闭的job
removeJob(schedule);
} }
} catch (ClassNotFoundException e) {
LogUtils.error("任务类未找到:" + schedule.getJob(), e);
} catch (Exception e) { } catch (Exception e) {
LogUtils.error("初始化任务失败", e); LogUtils.error("初始化任务失败", e);
} }
@ -63,6 +73,9 @@ public class BaseScheduleService {
} }
private void removeJob(Schedule schedule) { private void removeJob(Schedule schedule) {
scheduleManager.removeJob(new JobKey(schedule.getKey(), schedule.getJob()), new TriggerKey(schedule.getKey(), schedule.getJob())); scheduleManager.removeJob(
new JobKey(schedule.getKey(), schedule.getJob()),
new TriggerKey(schedule.getKey(), schedule.getJob())
);
} }
} }