Built-in Schedules
On this page
To demonstrate the functionality of different schedules, we will be working with the following helper:
ts
import {Effect } from "effect"letstart = newDate ().getTime ()export constlog =Effect .sync (() => {constnow = newDate ().getTime ()console .log (`delay: ${now -start }`)start =now })
ts
import {Effect } from "effect"letstart = newDate ().getTime ()export constlog =Effect .sync (() => {constnow = newDate ().getTime ()console .log (`delay: ${now -start }`)start =now })
The log
helper logs the time delay between each execution. We will use this helper to showcase the behavior of various built-in schedules.
forever
A schedule that always recurs and produces number of recurrence at each run.
ts
import {Effect ,Schedule } from "effect"import * asDelay from "./Delay"constschedule =Schedule .forever Effect .runPromise (Effect .repeat (Delay .log ,schedule ))/*Output:delay: 3delay: 7 < foreverdelay: 5delay: 1delay: 1delay: 1delay: 1delay: 1...*/
ts
import {Effect ,Schedule } from "effect"import * asDelay from "./Delay"constschedule =Schedule .forever Effect .runPromise (Effect .repeat (Delay .log ,schedule ))/*Output:delay: 3delay: 7 < foreverdelay: 5delay: 1delay: 1delay: 1delay: 1delay: 1...*/
once
A schedule that recurs one time.
ts
import {Effect ,Schedule } from "effect"import * asDelay from "./Delay"constschedule =Schedule .once Effect .runPromise (Effect .repeat (Delay .log ,schedule ))/*Output:delay: 3delay: 8 < once*/
ts
import {Effect ,Schedule } from "effect"import * asDelay from "./Delay"constschedule =Schedule .once Effect .runPromise (Effect .repeat (Delay .log ,schedule ))/*Output:delay: 3delay: 8 < once*/
recurs
A schedule that only recurs the specified number of times.
ts
import {Effect ,Schedule } from "effect"import * asDelay from "./Delay"constschedule =Schedule .recurs (5)Effect .runPromise (Effect .repeat (Delay .log ,schedule ))/*Output:delay: 5delay: 8 < recursdelay: 6delay: 2delay: 1delay: 1*/
ts
import {Effect ,Schedule } from "effect"import * asDelay from "./Delay"constschedule =Schedule .recurs (5)Effect .runPromise (Effect .repeat (Delay .log ,schedule ))/*Output:delay: 5delay: 8 < recursdelay: 6delay: 2delay: 1delay: 1*/
Recurring at specific intervals
In the context of scheduling, two commonly used schedules are spaced
and fixed
. While they both involve recurring at specific intervals, they have a fundamental difference in how they determine the timing of repetitions.
The spaced
schedule creates a recurring pattern where each repetition is spaced apart by a specified duration. This means that there is a delay between the completion of one repetition and the start of the next. The duration between repetitions remains constant, providing a consistent spacing pattern.
On the other hand, the fixed
schedule recurs on a fixed interval, regardless of the duration of the actions or the completion time of previous repetitions. It operates independently of the execution time, ensuring a regular recurrence at the specified interval.
spaced
A schedule that recurs continuously, each repetition spaced the specified duration from the last run.
ts
import {Effect ,Schedule } from "effect"import * asDelay from "./Delay"constschedule =Schedule .spaced ("200 millis")constaction =Effect .delay (Delay .log , "100 millis")Effect .runPromise (Effect .repeat (action ,schedule ))/*Output:delay: 109delay: 316 < spaceddelay: 302delay: 306delay: 306...*/
ts
import {Effect ,Schedule } from "effect"import * asDelay from "./Delay"constschedule =Schedule .spaced ("200 millis")constaction =Effect .delay (Delay .log , "100 millis")Effect .runPromise (Effect .repeat (action ,schedule ))/*Output:delay: 109delay: 316 < spaceddelay: 302delay: 306delay: 306...*/
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 on a fixed interval. Returns the number of repetitions of the schedule so far.
ts
import {Effect ,Schedule } from "effect"import * asDelay from "./Delay"constschedule =Schedule .fixed ("200 millis")constaction =Effect .delay (Delay .log , "100 millis")Effect .runPromise (Effect .repeat (action ,schedule ))/*Output:delay: 109delay: 316 < fixeddelay: 202delay: 202delay: 203...*/
ts
import {Effect ,Schedule } from "effect"import * asDelay from "./Delay"constschedule =Schedule .fixed ("200 millis")constaction =Effect .delay (Delay .log , "100 millis")Effect .runPromise (Effect .repeat (action ,schedule ))/*Output:delay: 109delay: 316 < fixeddelay: 202delay: 202delay: 203...*/
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
ts
import {Effect ,Schedule } from "effect"import * asDelay from "./Delay"constschedule =Schedule .exponential ("10 millis")Effect .runPromise (Effect .repeat (Delay .log ,schedule ))/*Output:delay: 4delay: 17 < exponentialdelay: 21delay: 40delay: 85delay: 168delay: 324delay: 644delay: 1287...*/
ts
import {Effect ,Schedule } from "effect"import * asDelay from "./Delay"constschedule =Schedule .exponential ("10 millis")Effect .runPromise (Effect .repeat (Delay .log ,schedule ))/*Output:delay: 4delay: 17 < exponentialdelay: 21delay: 40delay: 85delay: 168delay: 324delay: 644delay: 1287...*/
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.
ts
import {Effect ,Schedule } from "effect"import * asDelay from "./Delay"constschedule =Schedule .fibonacci ("10 millis")Effect .runPromise (Effect .repeat (Delay .log ,schedule ))/*Output:delay: 3delay: 17 < fibonaccidelay: 11delay: 23delay: 32delay: 52delay: 84delay: 132delay: 212delay: 342delay: 556delay: 892...*/
ts
import {Effect ,Schedule } from "effect"import * asDelay from "./Delay"constschedule =Schedule .fibonacci ("10 millis")Effect .runPromise (Effect .repeat (Delay .log ,schedule ))/*Output:delay: 3delay: 17 < fibonaccidelay: 11delay: 23delay: 32delay: 52delay: 84delay: 132delay: 212delay: 342delay: 556delay: 892...*/