@smitejs/jobs
Scheduled jobs for Smite: job descriptors with cron and interval schedules, a scheduler executor, and a collector.
yarn add @smitejs/jobs20 documented symbols · version 2.0.0-SNAPSHOT.0 · source
Builders
constcron
const cron: (expression: string) => JobSchedule;Builds a cron schedule from a 5-field POSIX expression. Every field accepts a bare value (*, n), a range (a-b), a range with a step (a-b/n), a wildcard with a step (for example * followed by /n), or a comma-separated list of any of those.
const everyFive = cron("5 * * * *");"cron"
Date.UTC(2026, 0, 1, 0, 5, 0)packages/jobs/src/schedule.ts:16
constinterval
const interval: (milliseconds: number) => JobSchedule;Builds a fixed-interval schedule from a duration in milliseconds.
packages/jobs/src/schedule.ts:30
functionjob
function job(app: AppDescriptor, id: string): JobBuilder;Creates a job builder for an app. The common piece — (app, id) — yields two schedule builders (cron() / every()); pick one and .run(fn) attaches the jobs.handler child and relates the jobs.job node under the app.
const everyFive = cron("5 * * * *");"cron"
Date.UTC(2026, 0, 1, 0, 5, 0)const app = createApp();
const heartbeat = job(app, "heartbeat")
.every(5_000)
.run(() => undefined);"interval"interfaceJobBuilder
interface JobBuilder {
readonly cron: (expression: string) => JobRun;
readonly every: (milliseconds: number) => JobRun;
}A job builder: choose a cron or interval schedule from one shared job(app, id) piece, then bind a run function. Always a builder; returns the jobs.job descriptor after run().
const app = createApp();
const descriptor = job(app, "nightly")
.cron("0 0 * * *")
.run(() => undefined);"jobs.job"
"nightly"
"cron"
1interfaceJobRun
interface JobRun {
readonly run: (fn: JobHandler) => JobDescriptor;
}The terminal step of a JobBuilder: binds the run function to the schedule chosen and registers the job node under the app.
Collector
interfaceCollectedJob
interface CollectedJob {
readonly id: string;
readonly schedule: JobSchedule;
readonly run: (signal: EmptySignal) => void | Promise<void>;
}A job as seen by artifact generators: its id, schedule, and the run function the scheduler invokes. run takes a zero-input signal.
packages/jobs/src/collector.ts:6
functionjobsOf
function jobsOf(app: AppDescriptor): readonly CollectedJob[];Walks an app's jobs.job children and returns the collected jobs with their schedules and run functions. Shared by artifact generators and the scheduler executor.
job(app, "cleanup")
.every(60_000)
.run(() => undefined);
job(app, "report")
.cron("0 2 * * 1")
.run(() => undefined);
const collected = jobsOf(app);[
"cleanup",
"report",
]
"interval"packages/jobs/src/collector.ts:24
Executor
interfaceJobScheduler
interface JobScheduler {
readonly jobs: readonly CollectedJob[];
readonly start: () => Promise<void>;
readonly tick: () => () => void;
readonly stop: () => void;
}A runtime handle returned by scheduler: the collected jobs, plus start (fire everything once) and tick/stop to drive the fire clock.
packages/jobs/src/scheduler.ts:32
functionnextFire
function nextFire(schedule: JobSchedule, from: Date): Date | null;Computes the next moment schedule fires at or after from. Cron fires on the first matching minute boundary; interval schedules fire milliseconds after from. Returns null when no cron fire is within the look-ahead horizon (one year).
const workdayNine = cron("0 9-17 * * 1-5");
const monday = new Date("2026-01-05T00:00:00Z"); // 2026-01-05 is a Monday
const next = nextFire(workdayNine, monday);Date.UTC(2026, 0, 5, 9, 0, 0)packages/jobs/src/schedule.ts:152
construnJob
const runJob: (descriptor: {
readonly data: {
readonly id: string;
};
}, fn: (signal: EmptySignal) => void | Promise<void>, at?: number | Date) => Promise<void>;Fires a job's run function with a zero-input signal at the given (or current) instant.
functionscheduler
function scheduler(app: AppDescriptor, options?: SchedulerOptions): JobScheduler;Turns an app into a runtime job scheduler. Walks the app's jobs.job IR tree via child refs — never the global registry — so it keeps working in production bundles where collect mode is folded out. start() fires every job once; tick() arms the cron/interval timers and returns a stop for shutdown; { start: true } arms automatically. onRun/onError observe each fire.
const handle = scheduler(app);
const stop = handle.tick();0packages/jobs/src/scheduler.ts:50
interfaceSchedulerOptions
interface SchedulerOptions {
readonly onRun?: (entry: {
readonly id: string;
readonly at: number;
}) => void;
readonly onError?: (entry: {
readonly id: string;
readonly error: unknown;
}) => void;
readonly start?: boolean;
}Options for scheduler.
packages/jobs/src/scheduler.ts:12
Internals
functioncompileCron
function compileCron(expr: string): CompiledCron;Parses a 5-field POSIX cron expression into CompiledCron.
packages/jobs/src/schedule.ts:94
interfaceJobDescriptor
interface JobDescriptor extends Descriptor<"jobs.job", {
readonly id: string;
readonly schedule: JobSchedule;
}> {
}A jobs.job IR node: a named schedule and the handler edge that runs it.
interfaceJobHandlerDescriptor
interface JobHandlerDescriptor extends Descriptor<"jobs.handler", {
readonly fn: JobHandler;
}> {
}A node wrapping the function a job runs.
Reference
interfaceCompiledCron
interface CompiledCron {
readonly minute: FieldMatcher;
readonly hour: FieldMatcher;
readonly dayOfMonth: FieldMatcher;
readonly month: FieldMatcher;
readonly dayOfWeek: FieldMatcher;
readonly dayOfMonthIsStar: boolean;
readonly dayOfWeekIsStar: boolean;
}A compiled cron expression: five field matchers plus wildcard flags.
packages/jobs/src/schedule.ts:45
typeFieldMatcher
type FieldMatcher = (value: number) => boolean;Predicate deciding whether a single field value is allowed.
packages/jobs/src/schedule.ts:42
Surface
constjobs
const jobs: {
cron: (expression: string) => import("./schedule.js").JobSchedule;
interval: (milliseconds: number) => import("./schedule.js").JobSchedule;
job: typeof job;
jobsOf: typeof jobsOf;
scheduler: typeof scheduler;
};The jobs namespace: one import for the whole scheduled-jobs app extensor.
const jobsBundle = jobs;"function"
"function"
"function"
"function"
"function"Types
typeJobHandler
type JobHandler = EmptyHandler;The zero-input function a job runs each time it fires.
typeJobSchedule
type JobSchedule = {
readonly kind: "cron";
readonly expression: string;
} | {
readonly kind: "interval";
readonly milliseconds: number;
};A job run schedule: either a POSIX cron expression (minute hour day-of-month month day-of-week; 0 and 7 both mean Sunday) or a fixed interval in milliseconds.