Skip to content

Commit fd4314a

Browse files
committed
#21: Add method Scheduler.remove(String jobName)
1 parent e90158a commit fd4314a

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

src/main/java/com/coreoz/wisp/Scheduler.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff 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.

src/test/java/com/coreoz/wisp/SchedulerTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.time.Duration;
88
import java.util.concurrent.atomic.AtomicBoolean;
99

10+
import com.coreoz.wisp.schedule.Schedule;
1011
import org.assertj.core.data.Offset;
1112
import org.junit.Test;
1213
import 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
}

0 commit comments

Comments
 (0)