Skip to content

Cluster.Cron reference

Source: packages/effect-cluster/src/Cron.ts

A Cron on an Effect Cluster: a job that runs on a schedule, once per scheduled time across the cluster, on whichever runner owns it. Each run is a persisted message, so a run due while its runner restarts still happens.

Declare it by name, with its schedule, as Effect’s ClusterCron.make takes it, and its implementation: an Effect that resolves bindings once per runner and returns the job. The job can read the time it was scheduled for with ClusterCron.CurrentScheduledTime. Providing a pool Layer to the implementation is what deploys it. Or declare it bare and implement it with .make(props, impl), as a Layer.

A nightly job

src/Nightly.ts
export default class Nightly extends Cluster.Cron<Nightly>()(
"Nightly",
{ main: import.meta.url, cron: "0 3 * * *", skipIfOlderThan: "1 hour" },
Effect.gen(function* () {
const counters = yield* Counter;
return Effect.gen(function* () {
const counter = yield* counters.getByName("nightly");
yield* counter.Increment({ amount: 1 });
}).pipe(Effect.orDie);
}).pipe(Effect.provide(Default)),
) {}

Declared bare, and implemented as a Layer

src/Nightly.ts
export class Nightly extends Cluster.Cron<Nightly>()("Nightly") {}
// src/NightlyLive.ts
export default Nightly.make(
{ main: import.meta.url, cron: "0 3 * * *", skipIfOlderThan: "1 hour" },
Effect.gen(function* () {
const counters = yield* Counter;
return Effect.gen(function* () {
const counter = yield* counters.getByName("nightly");
yield* counter.Increment({ amount: 1 });
}).pipe(Effect.orDie);
}).pipe(Effect.provide(Default)),
);