Skip to content

Commit

Permalink
Allow cron executor to restart
Browse files Browse the repository at this point in the history
  • Loading branch information
mdoering committed Feb 21, 2025
1 parent fb89c87 commit e01e436
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions core/src/main/java/life/catalogue/jobs/cron/CronExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import life.catalogue.concurrent.NamedThreadFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
Expand All @@ -18,29 +19,32 @@
public class CronExecutor implements Managed {
private static final Logger LOG = LoggerFactory.getLogger(CronExecutor.class);
private final String THREAD_NAME = "cron-executor";
private final CronJob[] jobs;
private ScheduledExecutorService scheduler;
private List<ScheduledFuture<?>> futures = new ArrayList<>();

public static CronExecutor startWith(CronJob... jobs) {
Preconditions.checkNotNull(jobs, "At least one cron job must be specified");
CronExecutor cron = new CronExecutor();
for (CronJob job : jobs) {
LOG.info("Schedule cron job {} every {} {}", job.getClass().getSimpleName(), job.getFrequency(), job.getFrequencyUnit());
var f = cron.scheduler.scheduleAtFixedRate(job, job.getDelay(), job.getFrequency(), job.getFrequencyUnit());
cron.futures.add(f);
}
return cron;
return new CronExecutor(jobs);
}

private CronExecutor() {
scheduler = Executors.newScheduledThreadPool(1,
new NamedThreadFactory(THREAD_NAME, Thread.NORM_PRIORITY, true)
);
private CronExecutor(CronJob[] jobs) {
this.jobs = jobs;
}

@Override
public void start() throws Exception {

if (scheduler == null && jobs.length > 0) {
LOG.info("Start cron executor with {} jobs", jobs.length);
scheduler = Executors.newScheduledThreadPool(1,
new NamedThreadFactory(THREAD_NAME, Thread.NORM_PRIORITY, true)
);
for (CronJob job : jobs) {
LOG.info("Schedule cron job {} every {} {}", job.getClass().getSimpleName(), job.getFrequency(), job.getFrequencyUnit());
var f = scheduler.scheduleAtFixedRate(job, job.getDelay(), job.getFrequency(), job.getFrequencyUnit());
futures.add(f);
}
}
}

@Override
Expand All @@ -51,10 +55,11 @@ public void stop() throws Exception {
}
scheduler.shutdown();
LOG.info("Cron executor stopped");
scheduler = null;
}

@Override
public boolean hasStarted() {
return scheduler.isShutdown();
return scheduler != null;
}
}

0 comments on commit e01e436

Please sign in to comment.