7373 * based on the relative time between it and {@link Worker#now(TimeUnit)}. However, drifts or changes in the
7474 * system clock could affect this calculation either by scheduling subsequent runs too frequently or too far apart.
7575 * Therefore, the default implementation uses the {@link #clockDriftTolerance()} value (set via
76- * {@code rx3.scheduler.drift-tolerance} in minutes ) to detect a drift in {@link Worker#now(TimeUnit)} and
77- * re-adjust the absolute/relative time calculation accordingly.
76+ * {@code rx3.scheduler.drift-tolerance} and {@code rx3.scheduler.drift-tolerance-unit} ) to detect a
77+ * drift in {@link Worker#now(TimeUnit)} and re-adjust the absolute/relative time calculation accordingly.
7878 * <p>
7979 * The default implementations of {@link #start()} and {@link #shutdown()} do nothing and should be overridden if the
8080 * underlying task-execution scheme supports stopping and restarting itself.
@@ -91,17 +91,42 @@ public abstract class Scheduler {
9191 /**
9292 * The tolerance for a clock drift in nanoseconds where the periodic scheduler will rebase.
9393 * <p>
94- * The associated system parameter, {@code rx3.scheduler.drift-tolerance}, expects its value in minutes.
94+ * Associated system parameters:
95+ * <ul>
96+ * <li>{@code rx3.scheduler.drift-tolerance}, long, default {@code 15}</li>
97+ * <li>{@code rx3.scheduler.drift-tolerance-unit}, string, default {@code minutes},
98+ * supports {@code seconds} and {@code milliseconds}.
99+ * </ul>
95100 */
96- static final long CLOCK_DRIFT_TOLERANCE_NANOSECONDS ;
97- static {
98- CLOCK_DRIFT_TOLERANCE_NANOSECONDS = TimeUnit .MINUTES .toNanos (
99- Long .getLong ("rx3.scheduler.drift-tolerance" , 15 ));
101+ static final long CLOCK_DRIFT_TOLERANCE_NANOSECONDS =
102+ computeClockDrift (
103+ Long .getLong ("rx3.scheduler.drift-tolerance" , 15 ),
104+ System .getProperty ("rx3.scheduler.drift-tolerance-unit" , "minutes" )
105+ );
106+
107+ /**
108+ * Returns the clock drift tolerance in nanoseconds based on the input selection.
109+ * @param time the time value
110+ * @param timeUnit the time unit string
111+ * @return the time amount in nanoseconds
112+ */
113+ static long computeClockDrift (long time , String timeUnit ) {
114+ if ("seconds" .equalsIgnoreCase (timeUnit )) {
115+ return TimeUnit .SECONDS .toNanos (time );
116+ } else if ("milliseconds" .equalsIgnoreCase (timeUnit )) {
117+ return TimeUnit .MILLISECONDS .toNanos (time );
118+ }
119+ return TimeUnit .MINUTES .toNanos (time );
100120 }
101121
102122 /**
103123 * Returns the clock drift tolerance in nanoseconds.
104- * <p>Related system property: {@code rx3.scheduler.drift-tolerance} in minutes.
124+ * <p>Related system properties:
125+ * <ul>
126+ * <li>{@code rx3.scheduler.drift-tolerance}, long, default {@code 15}</li>
127+ * <li>{@code rx3.scheduler.drift-tolerance-unit}, string, default {@code minutes},
128+ * supports {@code seconds} and {@code milliseconds}.
129+ * </ul>
105130 * @return the tolerance in nanoseconds
106131 * @since 2.0
107132 */
@@ -350,7 +375,7 @@ public <S extends Scheduler & Disposable> S when(@NonNull Function<Flowable<Flow
350375 * based on the relative time between it and {@link #now(TimeUnit)}. However, drifts or changes in the
351376 * system clock would affect this calculation either by scheduling subsequent runs too frequently or too far apart.
352377 * Therefore, the default implementation uses the {@link #clockDriftTolerance()} value (set via
353- * {@code rx3.scheduler.drift-tolerance} in minutes ) to detect a drift in {@link #now(TimeUnit)} and
378+ * {@code rx3.scheduler.drift-tolerance} and {@code rx3.scheduler.drift-tolerance-unit} ) to detect a drift in {@link #now(TimeUnit)} and
354379 * re-adjust the absolute/relative time calculation accordingly.
355380 * <p>
356381 * If the {@code Worker} is disposed, the {@code schedule} methods
0 commit comments