Make sure task is not executed after is has cancelled himself

This commit is contained in:
René Pfeuffer
2019-06-24 17:51:38 +02:00
parent eec4b282e6
commit 1e586b020c
3 changed files with 20 additions and 25 deletions

View File

@@ -30,7 +30,7 @@ final class CronExpression {
boolean shouldRun(ZonedDateTime time) {
ZonedDateTime now = ZonedDateTime.now(clock);
return time.isBefore(now);
return time.isBefore(now) || time.isEqual(now);
}
Optional<ZonedDateTime> calculateNextRun() {

View File

@@ -32,7 +32,7 @@ class CronTask implements Task, Runnable {
@Override
public synchronized void run() {
if (expression.shouldRun(nextRun)) {
if (hasNextRun() && expression.shouldRun(nextRun)) {
LOG.debug("execute task {}, because of matching expression {}", name, expression);
runnable.run();
Optional<ZonedDateTime> next = expression.calculateNextRun();
@@ -40,6 +40,7 @@ class CronTask implements Task, Runnable {
nextRun = next.get();
} else {
LOG.debug("cancel task {}, because expression {} has no next execution", name, expression);
nextRun = null;
cancel();
}
} else {