16
16
package com .arpnetworking .metrics .portal .scheduling .impl ;
17
17
18
18
import com .arpnetworking .logback .annotations .Loggable ;
19
+ import com .arpnetworking .steno .Logger ;
20
+ import com .arpnetworking .steno .LoggerFactory ;
19
21
import com .google .common .base .MoreObjects ;
20
22
import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
21
23
import net .sf .oval .constraint .NotNull ;
26
28
import java .time .ZoneId ;
27
29
import java .time .ZonedDateTime ;
28
30
import java .time .temporal .ChronoUnit ;
31
+ import java .time .temporal .UnsupportedTemporalTypeException ;
29
32
import java .util .Objects ;
30
33
import java .util .Optional ;
31
34
@@ -48,6 +51,7 @@ public final class PeriodicSchedule extends BoundedSchedule {
48
51
private final long _periodCount ;
49
52
private final ZoneId _zone ;
50
53
private final Duration _offset ;
54
+ private static final Logger LOGGER = LoggerFactory .getLogger (PeriodicSchedule .class );
51
55
52
56
private PeriodicSchedule (final Builder builder ) {
53
57
super (builder );
@@ -63,13 +67,32 @@ protected Optional<Instant> unboundedNextRun(final Optional<Instant> lastRun) {
63
67
.map (run -> run .plus (_periodCount , _period ))
64
68
.orElseGet (this ::getRunAtAndAfter );
65
69
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
+ }
73
96
}
74
97
75
98
@ Override
0 commit comments