Skip to content

Commit 728976d

Browse files
committed
quartz: allow to clear all job data/trigger on startup
- Added cleanJobs and cleanStaleJobs flags to QuartzModule - Fix #3100
1 parent fe656f6 commit 728976d

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

modules/jooby-quartz/src/main/java/io/jooby/internal/quartz/JobFactoryImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
public class JobFactoryImpl implements JobFactory {
1717
private final JobFactory next;
1818

19-
private Registry registry;
19+
private final Registry registry;
2020

2121
public JobFactoryImpl(Registry registry, JobFactory next) {
2222
this.registry = registry;
@@ -34,6 +34,6 @@ public Job newJob(TriggerFiredBundle bundle, Scheduler scheduler) {
3434

3535
@Override
3636
public String toString() {
37-
return getClass().getName();
37+
return "JobFactory for " + registry.toString();
3838
}
3939
}

modules/jooby-quartz/src/main/java/io/jooby/quartz/QuartzApp.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import io.jooby.Jooby;
3636
import io.jooby.Route;
3737
import io.jooby.SneakyThrows;
38-
import io.jooby.StatusCode;
3938
import io.jooby.internal.quartz.JobGenerator;
4039

4140
/**
@@ -133,10 +132,10 @@ public class QuartzApp extends Jooby {
133132
delete(
134133
"/{group}/{name}",
135134
ctx -> {
136-
Scheduler scheduler = getScheduler();
137-
JobKey jobKey = jobKey(ctx);
138-
scheduler.deleteJob(jobKey);
139-
return ctx.send(StatusCode.NO_CONTENT);
135+
var scheduler = getScheduler();
136+
var jobKey = jobKey(ctx);
137+
var deleted = scheduler.deleteJob(jobKey);
138+
return Map.of("key", jobKey.toString(), "deleted", deleted);
140139
});
141140
}
142141

modules/jooby-quartz/src/main/java/io/jooby/quartz/QuartzModule.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ public class QuartzModule implements Extension {
105105

106106
private List<Class<?>> jobs;
107107
private Scheduler scheduler;
108+
private Boolean cleanStaleJobs;
109+
private boolean cleanJobs;
108110

109111
/**
110112
* Creates Quartz module and register the given jobs.
@@ -148,13 +150,37 @@ public QuartzModule(@NonNull Scheduler scheduler, final List<Class<?>> jobs) {
148150
this.jobs = jobs;
149151
}
150152

153+
/**
154+
* Lookup for existing (persisted) jobs and compare with the job list from {@link
155+
* #QuartzModule(Class[])}. Delete any persisted job that is not in the list.
156+
*
157+
* @param cleanStaleJobs True to clear/delete stale job and triggers.
158+
* @return This module.
159+
*/
160+
public QuartzModule cleanStaleJobs(boolean cleanStaleJobs) {
161+
this.cleanStaleJobs = cleanStaleJobs;
162+
return this;
163+
}
164+
165+
/**
166+
* Safely clear/delete all jobs before schedule startup. Use with caution.
167+
*
168+
* @param cleanJobs True to clear/delete all jobs and triggers.
169+
* @return This module.
170+
*/
171+
public QuartzModule cleanJobs(boolean cleanJobs) {
172+
this.cleanJobs = cleanJobs;
173+
return this;
174+
}
175+
151176
@Override
152177
public void install(@NonNull Jooby application) throws Exception {
153178
Config config = application.getConfig();
154179
Map<JobDetail, Trigger> jobMap = JobGenerator.build(application, jobs);
155180

156181
Properties properties = properties(config);
157182

183+
this.cleanStaleJobs = computeCleanStaleJobs(this.scheduler == null);
158184
Scheduler scheduler = this.scheduler == null ? newScheduler(application) : this.scheduler;
159185
var context = scheduler.getContext();
160186
context.put("registry", application);
@@ -167,7 +193,18 @@ public void install(@NonNull Jooby application) throws Exception {
167193
}
168194
application.onStarted(
169195
() -> {
170-
cleanStaleJobs(application.getLog(), scheduler, jobs);
196+
if (scheduler.isStarted()) {
197+
scheduler.standby();
198+
}
199+
200+
if (this.cleanJobs) {
201+
// clear all jobs
202+
scheduler.clear();
203+
} else {
204+
if (this.cleanStaleJobs) {
205+
cleanStaleJobs(application.getLog(), scheduler, jobs);
206+
}
207+
}
171208

172209
for (Map.Entry<JobDetail, Trigger> e : jobMap.entrySet()) {
173210
JobDetail jobDetail = e.getKey();
@@ -202,6 +239,10 @@ public void install(@NonNull Jooby application) throws Exception {
202239
application.onStop(() -> scheduler.shutdown(waitForJobsToComplete));
203240
}
204241

242+
private boolean computeCleanStaleJobs(boolean defaults) {
243+
return this.cleanStaleJobs == null ? defaults : this.cleanStaleJobs.booleanValue();
244+
}
245+
205246
/**
206247
* Cleanup any job that was persisted in previous execution and was removed from job list.
207248
*

0 commit comments

Comments
 (0)