Skip to content

Cluster.Singleton reference

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

A Singleton on an Effect Cluster: one fiber, running on exactly one of its pool. If that runner stops, another starts it.

Declare it by name, with its implementation: an Effect that resolves bindings once per runner and returns the fiber, which only the runner that owns the singleton runs. 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 loop that runs once across the cluster

src/Reaper.ts
export default class Reaper extends Cluster.Singleton<Reaper>()(
"Reaper",
{ main: import.meta.url },
Effect.gen(function* () {
const counters = yield* Counter;
return Effect.gen(function* () {
const counter = yield* counters.getByName("reaped");
yield* counter.Increment({ amount: 1 });
}).pipe(Effect.repeat(Schedule.spaced("1 minute")), Effect.orDie);
}).pipe(Effect.provide(Default)),
) {}

Declared bare, and implemented as a Layer

src/Reaper.ts
export class Reaper extends Cluster.Singleton<Reaper>()("Reaper") {}
// src/ReaperLive.ts
export default Reaper.make(
{ main: import.meta.url },
Effect.gen(function* () {
const counters = yield* Counter;
return Effect.gen(function* () {
const counter = yield* counters.getByName("reaped");
yield* counter.Increment({ amount: 1 });
}).pipe(Effect.repeat(Schedule.spaced("1 minute")), Effect.orDie);
}).pipe(Effect.provide(Default)),
);