@@ -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