Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
[astro] Tidy up set of scheduled futures (#3811)
Browse files Browse the repository at this point in the history
...in order to not keep useless references to finished jobs in the collections.

It's taking the opportunity for tyding up every time a new job gets scheduled.
In that way, the job's runnables don't have to be cluttered with these
internals of the handler.

Signed-off-by: Simon Kaufmann <simon.kfm@googlemail.com>
  • Loading branch information
sjsf authored and kaikreuzer committed Jul 7, 2017
1 parent 94303b4 commit 8af6f61
Showing 1 changed file with 12 additions and 0 deletions.
Expand Up @@ -16,6 +16,7 @@
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -278,6 +279,7 @@ public void schedule(Job job, Calendar eventAt) {
long sleepTime;
monitor.lock();
try {
tidyScheduledFutures();
sleepTime = eventAt.getTimeInMillis() - new Date().getTime();
ScheduledFuture<?> future = scheduler.schedule(job, sleepTime, TimeUnit.MILLISECONDS);
scheduledFutures.add(future);
Expand All @@ -290,6 +292,16 @@ public void schedule(Job job, Calendar eventAt) {
}
}

private void tidyScheduledFutures() {
for (Iterator<ScheduledFuture<?>> iterator = scheduledFutures.iterator(); iterator.hasNext();) {
ScheduledFuture<?> future = iterator.next();
if (future.isDone()) {
logger.trace("Tidying up done future {}", future);
iterator.remove();
}
}
}

/**
* Calculates and publishes the daily Astro data.
*/
Expand Down

0 comments on commit 8af6f61

Please sign in to comment.