import * as Cause from "effect/Cause"; import * as Context from "effect/Context"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import * as Ref from "effect/Ref"; import * as Scope from "effect/Scope"; import * as Semaphore from "effect/Semaphore"; import { forkParked } from "../serverActivation.ts"; /** Sources load due work from their durable state; registration owns its execution lifetime. */ export class Scheduler extends Context.Service< Scheduler, { readonly register: ( name: string, runDueWork: Effect.Effect, ) => Effect.Effect; } >()("t3/scheduling/Scheduler") {} const make = Effect.gen(function* () { const sources = yield* Ref.make(new Map>()); const register: Scheduler["Service"]["register"] = Effect.fn("Scheduler.register")(function* < E, R, >(name: string, runDueWork: Effect.Effect) { const scope = yield* Effect.scope; const context = yield* Effect.context(); const permit = yield* Semaphore.make(1); const run = runDueWork.pipe( Effect.provideContext(context), Effect.catchCause((cause) => Cause.hasInterruptsOnly(cause) ? Effect.void : Effect.logWarning("Scheduler source failed", { source: name, cause }), ), permit.withPermitsIfAvailable(1), Effect.forkIn(scope), Effect.asVoid, ); const id = Symbol(name); yield* Effect.acquireRelease( Ref.update(sources, (current) => new Map(current).set(id, run)), () => Ref.update(sources, (current) => { const next = new Map(current); next.delete(id); return next; }), ); // Due work waits for startup recovery, which would cancel runs it started. yield* forkParked(run); }); const tick = Ref.get(sources).pipe( Effect.flatMap((current) => Effect.forEach(current.values(), (run) => run, { discard: true })), ); // One clock for all due-work sources. A slow source cannot block another // source or overlap itself, and no extra missed-tick backlog is queued. yield* forkParked(Effect.sleep("5 seconds").pipe(Effect.andThen(tick), Effect.forever)); return Scheduler.of({ register }); }); export const layer = Layer.effect(Scheduler, make);