To demonstrate the functionality of different schedules, we will use the following helper function that logs the delay between executions.
forever
A schedule that repeats indefinitely, producing the number of recurrences each time it runs.
Example (Forever Recurring Schedule)
once
A schedule that recurs only once.
Example (Single Recurrence Schedule)
recurs
A schedule that repeats a specified number of times.
Example (Fixed Number of Recurrences)
Recurring at specific intervals
You can use spaced or fixed schedules to specify intervals between recurrences. The difference lies in how the interval is measured: spaced delays each repetition from the end of the previous one, while fixed ensures regular intervals, regardless of how long the previous effect took.
spaced
A schedule that repeats indefinitely, each repetition spaced the specified duration from the last run.
Example (Recurring with Delay Between Executions)
The first delay is approximately 100 milliseconds, as the initial execution is not affected by the schedule. Subsequent delays are approximately 200 milliseconds apart, demonstrating the effect of the spaced schedule.
fixed
A schedule that recurs at fixed intervals. Returns the number of repetitions of the schedule so far.
Example (Fixed Interval Recurrence)
The first delay is approximately 100 milliseconds, as the initial execution is not affected by the schedule. Subsequent delays are consistently around 200 milliseconds apart, demonstrating the effect of the fixed schedule.
exponential
A schedule that recurs using exponential backoff, with each delay increasing exponentially.
Example (Exponential Backoff Schedule)
fibonacci
A schedule that always recurs, increasing delays by summing the preceding two delays (similar to the fibonacci sequence). Returns the current duration between recurrences.