Skip to main content

@flow-state-dev/scheduled

Interfaces​

CreateResourceCollectionScheduleResolverOptions​

@flow-state-dev/scheduled — what a flow needs to keep and resolve dynamic schedules: schedule collections, the resolver over them, the schedule-index contract a store implements, and next-run math.

The dispatch endpoint a schedule fires through is the engine's own, mounted by the router on every app (/api/flows/:kind/schedules/*id/dispatch); a flow declares schedules and nothing more is wired. This package imports no engine. ./collection is the same surface without createBadCronWarner, and ./testing holds the schedule-index conformance suite.

Properties​

blocks​
blocks: Record<string, BlockDefinition>;

Handler registry keyed by the persisted row's kind discriminator. A schedule binding carries its handler block inline, but a block cannot be serialized into resource state — so the row stores a kind string and the resolver maps it to a real block here. A row whose kind is absent from this map resolves to null (404), exactly like an unknown schedule id.

collection​
collection: object;

The resource collection that holds schedule entries. Pass either a defineResourceCollection(...) result or any value with a pattern string. The helper extracts the literal prefix (everything before *) to build the storage key.

pattern​
pattern: string;
parseId?​
parseId?: (scheduleId) => ParsedScheduleId | null;

Map a dispatch id back to (orgId, userId, collectionKey). Default: defaultParseScheduleId. Return null to 404 the dispatch.

Parameters​
scheduleId​

string

Returns​

ParsedScheduleId | null

session?​
session?: "per-fire" | "per-schedule";

Where every resolved schedule's fires run (ScheduleConfig.session). Default: "per-fire", each fire a one-off run. Set for every row this resolver resolves, never read from a row, so a schedule written through a client can't pick its session.


DefineScheduleCollectionOptions​

Properties​

client?​
client?: CollectionClientConfig;

The collection's client config, as on defineResourceCollection. Omitted, no client reads the schedules.

flowIsolation?​
flowIsolation?: boolean;

The collection's own flowIsolation. Omitted, it follows the declaring flow's isolateUserState, as any collection does. false keeps one set of schedules for the user across flows, so one written by one flow is read and fired by another.

index?​
index?: ScheduleIndex;

Optional schedule index. When provided, lifecycle hooks mirror every create/update/delete into the index using cron-parser to compute nextFireAt. When omitted, no hooks fire and the collection is a plain defineResourceCollection over the schedule schema.

pattern​
pattern: string;

Collection pattern. Must be user-scoped at the framework level; scope:"user" is forced internally. Typically "schedules/*".

runBy?​
runBy?: ResourceRunBy;

The flow a client's write of a schedule runs through, as on defineResourceCollection; it needs a client that reads state. Omitted, a client can't write the schedules.


ParsedScheduleId​

The parts a dynamic schedule's dispatch id names.

Properties​

collectionKey​
collectionKey: string;

The row's key inside the schedule collection.

orgId​
orgId: string;

The org the schedule was saved in, and fires into.

userId​
userId: string;

Who the schedule runs as.


ScheduleIndex​

Schedule index adapter. Implementations live in store packages (e.g. createPostgresScheduleIndex, createSQLiteScheduleIndex).

All methods are async-shaped to allow remote implementations even when a particular backend is synchronous (e.g. better-sqlite3).

Methods​

claimDue()​
claimDue(now, limit?): Promise<ScheduleIndexEntry[]>;

Atomically claim all rows whose nextFireAt <= now AND advance them to the next fire time (using cron-parser) in a single transaction. Returns the rows that were claimed, carrying the pre-advance nextFireAt so the caller can log/observe what fired.

Rows whose cron expressions fail to parse are skipped (the row is left at its current nextFireAt, a warning is logged) so a single bad row does not block the rest of the batch.

limit defaults to 100.

Parameters​
now​

number

limit?​

number

Returns​

Promise<ScheduleIndexEntry[]>

remove()​
remove(id): Promise<void>;

Remove the row for (cell, key). No-op when the row does not exist. Never touches another cell's row with the same key.

Parameters​
id​
cell​

string

key​

string

Returns​

Promise<void>

upsert()​
upsert(row): Promise<void>;

Insert or update a schedule entry. Idempotent on primary key (cell, key). The caller passes the freshly-computed nextFireAt — implementations do not parse cron themselves.

Parameters​
row​

ScheduleIndexEntry

Returns​

Promise<void>


ScheduleIndexEntry​

A single row in the schedule index. key is the collection-relative key (no leading collection prefix). nextFireAt is the next fire time in milliseconds since epoch.

A row is identified by (cell, key) — the same address as the schedule it mirrors — so one schedule is always exactly one row. userId is who the schedule runs as, and is data, not identity: one person holds several rows with one key when they have schedules in several storage cells (one per organization, and a flow-isolated cell).

Properties​

cell​
cell: string;

The storage cell the schedule is persisted in. With key, the row's identity. Filled by defineScheduleCollection from the engine's own derivation (CollectionHookContext.cell). Opaque: store and compare it, never parse it.

cron​
cron: string;
key​
key: string;
nextFireAt​
nextFireAt: number;
orgId?​
orgId?: string;

The organization this schedule fires into.

The TARGET's organization, captured from the execution that created the row — not the organization of whichever scheduler gateway later fires it, and not whatever organization the target user happens to be acting as at fire time. A schedule is a standing instruction from one organization, so that is the binding it keeps.

Optional on the type only because a row written before this existed has none. Such a row is quarantined rather than dispatched — see createResourceCollectionScheduleResolver.

timezone?​
timezone?: string;
userId​
userId: string;

Who the schedule runs as.

Type Aliases​

BadCronWarner​

type BadCronWarner = (userId, key, cron) => void;

One-shot bad-cron warn function returned by createBadCronWarner.

Parameters​

userId​

string

key​

string

cron​

string

Returns​

void


ScheduleCollectionState​

type ScheduleCollectionState = z.infer<typeof SCHEDULE_RESOURCE_SCHEMA>;

Inferred state shape for a schedule resource instance.


ScheduleResourceState​

type ScheduleResourceState = ScheduleCollectionState;

Shape persisted under each schedule resource. The single source of truth is the zod schema in defineScheduleCollection.ts; this is its inferred type, re-exported under the historical name for hosts that wire the resolver without using defineScheduleCollection.

Functions​

createBadCronWarner()​

function createBadCronWarner(prefix): BadCronWarner;

Build a one-shot bad-cron warn function for a ScheduleIndex impl. prefix is prepended to the log line so the source store is obvious (e.g. "[flow-state/store-postgres]").

Parameters​

prefix​

string

Returns​

BadCronWarner


createResourceCollectionScheduleResolver()​

function createResourceCollectionScheduleResolver(options): (scheduleId, ctx) => Promise<ScheduleConfig | null>;

@flow-state-dev/scheduled — what a flow needs to keep and resolve dynamic schedules: schedule collections, the resolver over them, the schedule-index contract a store implements, and next-run math.

The dispatch endpoint a schedule fires through is the engine's own, mounted by the router on every app (/api/flows/:kind/schedules/*id/dispatch); a flow declares schedules and nothing more is wired. This package imports no engine. ./collection is the same surface without createBadCronWarner, and ./testing holds the schedule-index conformance suite.

Parameters​

options​

CreateResourceCollectionScheduleResolverOptions

Returns​

(scheduleId, ctx) => Promise<ScheduleConfig | null>


defaultParseScheduleId()​

function defaultParseScheduleId(scheduleId): ParsedScheduleId | null;

Default parseId: <orgId>/<userId>/<key>, the inverse of core's formatScheduleId. Splits on the first two / and URL-decodes each part; the key keeps any further /, so a hand-built nested key stays whole. All three parts must be non-empty, and an id with fewer parts names no org and parses as null.

Parameters​

scheduleId​

string

Returns​

ParsedScheduleId | null


defineScheduleCollection()​

function defineScheduleCollection(opts): object & ResourceCollectionConfig<JsonObject> & object;

Define a user-scoped schedule resource collection with optional automatic mirroring into a ScheduleIndex. See module doc for behavioural notes.

Parameters​

opts​

DefineScheduleCollectionOptions

Returns​

object & ResourceCollectionConfig<JsonObject> & object


parseNextFireAt()​

function parseNextFireAt(cron, tz?): number | null;

Parse cron (POSIX five-field) and return the next firing time in ms since epoch. Returns null if the expression is unparseable — callers decide how to surface that (write-time rejection vs. claim-time skip-and-warn).

Parameters​

cron​

string

tz?​

string

Returns​

number | null