Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Watcher: Simplify finding next date in cron schedule #33015

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,14 @@ private CronnableSchedule(Cron... crons) {
@Override
public long nextScheduledTimeAfter(long startTime, long time) {
assert time >= startTime;
long nextTime = Long.MAX_VALUE;
for (Cron cron : crons) {
long nextValidTimeAfter = cron.getNextValidTimeAfter(time);

boolean previousCronExpired = nextTime == -1;
boolean currentCronValid = nextValidTimeAfter > -1;
if (previousCronExpired && currentCronValid) {
nextTime = nextValidTimeAfter;
} else {
nextTime = Math.min(nextTime, nextValidTimeAfter);
}
}
return nextTime;
return Arrays.stream(crons)
.map(cron -> cron.getNextValidTimeAfter(time))
// filter out expired dates before sorting
.filter(nextValidTime -> nextValidTime > -1)
.sorted()
.findFirst()
// no date in the future found, return -1 to the caller
.orElse(-1L);
}

public Cron[] crons() {
Expand Down