Skip to content

Commit

Permalink
perf: 定时任务全量加载速度优化 TencentBlueKing#3363
Browse files Browse the repository at this point in the history
1.更准确地判断Quartz状态;
2.加载任务前增加对Quartz状态的判断,Quartz启动完成后才开始加载任务。
  • Loading branch information
jsonwan committed Jan 9, 2025
1 parent 51783d7 commit 1975429
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.tencent.bk.job.crontab.service.CronJobLoadingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

Expand All @@ -40,11 +41,13 @@
public class CronJobLoadingServiceImpl implements CronJobLoadingService {

private final CronJobBatchLoadService cronJobBatchLoadService;
private final Scheduler scheduler;
private volatile boolean loadingCronToQuartz = false;

@Autowired
public CronJobLoadingServiceImpl(CronJobBatchLoadService cronJobBatchLoadService) {
public CronJobLoadingServiceImpl(CronJobBatchLoadService cronJobBatchLoadService, Scheduler scheduler) {
this.cronJobBatchLoadService = cronJobBatchLoadService;
this.scheduler = scheduler;
}

@Override
Expand All @@ -56,6 +59,7 @@ public void loadAllCronJob() {
return;
}
loadingCronToQuartz = true;
waitUtilQuartzStarted();
loadAllCronJobToQuartz();
} catch (InterruptedException e) {
log.info("loadAllCronJob interrupted, application may be closing");
Expand All @@ -67,6 +71,14 @@ public void loadAllCronJob() {
}
}

private void waitUtilQuartzStarted() throws Exception {
while (!scheduler.isStarted()) {
log.info("Quartz Scheduler is not started, sleep 1s and retry");
Thread.sleep(1000);
}
log.info("Quartz Scheduler is started now");
}

private void loadAllCronJobToQuartz() throws InterruptedException {
int start = 0;
int limit = 100;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public void addJob(QuartzJob quartzJob) throws SchedulerException {

Set<? extends Trigger> triggers = createTriggers(quartzJob);

if (scheduler.isShutdown()) {
log.info("scheduler is shutdown, ignore add job {}!", quartzJob.getKey().getName());
if (!scheduler.isStarted()) {
log.info("scheduler is not started, ignore add job {}!", quartzJob.getKey().getName());
return;
}

Expand Down Expand Up @@ -96,8 +96,8 @@ public void deleteJob(JobKey jobKey) throws SchedulerException {
Assert.notNull(jobKey, "jobKey cannot be empty!");
Assert.notNull(jobKey.getName(), "jobKey name cannot be empty!");

if (scheduler.isShutdown()) {
log.info("scheduler is shutdown, ignore delete job {}!", jobKey.getName());
if (!scheduler.isStarted()) {
log.info("scheduler is not started, ignore delete job {}!", jobKey.getName());
return;
}

Expand All @@ -108,9 +108,9 @@ public void deleteJob(JobKey jobKey) throws SchedulerException {
public void deleteJob(List<JobKey> jobKeys) throws SchedulerException {
Assert.notNull(jobKeys, "jobKeys cannot be empty!");

if (scheduler.isShutdown()) {
if (!scheduler.isStarted()) {
log.info(
"scheduler is shutdown, ignore delete {} job keys: {}",
"scheduler is not started, ignore delete {} job keys: {}",
jobKeys.size(),
jobKeys.stream().map(JobKey::getName).collect(Collectors.toList())
);
Expand All @@ -125,8 +125,8 @@ public void deleteJob(List<JobKey> jobKeys) throws SchedulerException {
*/
@Override
public void pauseAll() throws SchedulerException {
if (scheduler.isShutdown()) {
log.info("scheduler is shutdown, ignore pauseAll!");
if (!scheduler.isStarted()) {
log.info("scheduler is not started, ignore pauseAll!");
return;
}
this.scheduler.pauseAll();
Expand Down

0 comments on commit 1975429

Please sign in to comment.