File tree Expand file tree Collapse file tree
main/java/com/coreoz/wisp
test/java/com/coreoz/wisp Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -272,6 +272,27 @@ public CompletionStage<Job> cancel(String jobName) {
272272 }
273273 }
274274
275+ /**
276+ * Remove a terminated job, so with the status {@link JobStatus#DONE}),
277+ * from the monitored jobs. The monitored jobs are the ones
278+ * still referenced using {@link #jobStatus()}.
279+ *
280+ * This can be useful to avoid memory leak in case many jobs
281+ * with a short lifespan are created.
282+ * @param jobName The job name to remove
283+ * @throws IllegalArgumentException If there is no job corresponding to the job name or if the job is not on the status {@link JobStatus#DONE})
284+ */
285+ public void remove (String jobName ) {
286+ Job jobToRemove = indexedJobsByName .get (jobName );
287+ if (jobToRemove == null ) {
288+ throw new IllegalArgumentException ("There is no existing job with the name " + jobName );
289+ }
290+ if (jobToRemove .status () != JobStatus .DONE ) {
291+ throw new IllegalArgumentException ("Job is not terminated. Need to call the cancel() method before trying to remove it?" );
292+ }
293+ indexedJobsByName .remove (jobName );
294+ }
295+
275296 /**
276297 * Wait until the current running jobs are executed
277298 * and cancel jobs that are planned to be executed.
Original file line number Diff line number Diff line change 77import java .time .Duration ;
88import java .util .concurrent .atomic .AtomicBoolean ;
99
10+ import com .coreoz .wisp .schedule .Schedule ;
1011import org .assertj .core .data .Offset ;
1112import org .junit .Test ;
1213import org .slf4j .Logger ;
@@ -311,4 +312,25 @@ public void check_that_metrics_are_correctly_updated_during_and_after_a_job_exec
311312 assertThat (job .threadRunningJob ()).isNull ();
312313 }
313314
315+ @ Test
316+ public void remove__verify_that_a_done_job_is_correctly_removed () {
317+ Scheduler scheduler = new Scheduler ();
318+ Job job = scheduler .schedule (Utils .doNothing (), Schedule .willNeverBeExecuted );
319+ assertThat (scheduler .jobStatus ()).contains (job );
320+ scheduler .remove (job .name ());
321+ assertThat (scheduler .jobStatus ()).isEmpty ();
322+ }
323+
324+ @ Test (expected = IllegalArgumentException .class )
325+ public void remove__verify_that_a_non_existent_job_name_raises_an_illegal_argument_exception () {
326+ Scheduler scheduler = new Scheduler ();
327+ scheduler .remove ("does not exist" );
328+ }
329+
330+ @ Test (expected = IllegalArgumentException .class )
331+ public void remove__verify_that_a_non_done_job_name_raises_an_illegal_argument_exception () {
332+ Scheduler scheduler = new Scheduler ();
333+ Job job = scheduler .schedule (Utils .doNothing (), Schedules .fixedDelaySchedule (Duration .ofMillis (1000 )));
334+ scheduler .remove (job .name ());
335+ }
314336}
You can’t perform that action at this time.
0 commit comments