Skip to content

Commit 13020da

Browse files
authored
Fix period scheduling (#728)
* additional logging in case of bug found in tests * fix period handling
1 parent 083f491 commit 13020da

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

app/com/arpnetworking/metrics/portal/scheduling/impl/PeriodicSchedule.java

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package com.arpnetworking.metrics.portal.scheduling.impl;
1717

1818
import com.arpnetworking.logback.annotations.Loggable;
19+
import com.arpnetworking.steno.Logger;
20+
import com.arpnetworking.steno.LoggerFactory;
1921
import com.google.common.base.MoreObjects;
2022
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2123
import net.sf.oval.constraint.NotNull;
@@ -26,6 +28,7 @@
2628
import java.time.ZoneId;
2729
import java.time.ZonedDateTime;
2830
import java.time.temporal.ChronoUnit;
31+
import java.time.temporal.UnsupportedTemporalTypeException;
2932
import java.util.Objects;
3033
import java.util.Optional;
3134

@@ -48,6 +51,7 @@ public final class PeriodicSchedule extends BoundedSchedule {
4851
private final long _periodCount;
4952
private final ZoneId _zone;
5053
private final Duration _offset;
54+
private static final Logger LOGGER = LoggerFactory.getLogger(PeriodicSchedule.class);
5155

5256
private PeriodicSchedule(final Builder builder) {
5357
super(builder);
@@ -63,13 +67,32 @@ protected Optional<Instant> unboundedNextRun(final Optional<Instant> lastRun) {
6367
.map(run -> run.plus(_periodCount, _period))
6468
.orElseGet(this::getRunAtAndAfter);
6569

66-
final Instant nextRun =
67-
ZonedDateTime.ofInstant(untruncatedNextRun, _zone)
68-
.truncatedTo(_period)
69-
.plus(_offset)
70-
.toInstant();
71-
72-
return Optional.of(nextRun);
70+
try {
71+
final ChronoUnit truncationPeriod;
72+
if (_period.getDuration().toMillis() > Duration.ofDays(1).toMillis()) {
73+
truncationPeriod = ChronoUnit.DAYS;
74+
} else {
75+
truncationPeriod = _period;
76+
}
77+
final Instant nextRun =
78+
ZonedDateTime.ofInstant(untruncatedNextRun, _zone)
79+
.truncatedTo(truncationPeriod)
80+
.plus(_offset)
81+
.toInstant();
82+
83+
return Optional.of(nextRun);
84+
} catch (final UnsupportedTemporalTypeException e) {
85+
LOGGER.error().setMessage("Error creating next run time")
86+
.addData("lastRun", lastRun)
87+
.addData("untruncatedNextRun", untruncatedNextRun)
88+
.addData("period", _period)
89+
.addData("periodCount", _periodCount)
90+
.addData("zone", _zone)
91+
.addData("offset", _offset)
92+
.setThrowable(e)
93+
.log();
94+
throw e;
95+
}
7396
}
7497

7598
@Override

0 commit comments

Comments
 (0)