Skip to content

@smitejs/jobs ​

Scheduled jobs for Smite: job descriptors with cron and interval schedules, a scheduler executor, and a collector.

bash
yarn add @smitejs/jobs

20 documented symbols · version 2.0.0-SNAPSHOT.0 · source

Builders ​

constcron ​

ts
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.

ts
const everyFive = cron("5 * * * *");
ts
"cron"
Date.UTC(2026, 0, 1, 0, 5, 0)

packages/jobs/src/schedule.ts:16

constinterval ​

ts
const interval: (milliseconds: number) => JobSchedule;

Builds a fixed-interval schedule from a duration in milliseconds.

packages/jobs/src/schedule.ts:30

functionjob ​

ts
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.

ts
const everyFive = cron("5 * * * *");
ts
"cron"
Date.UTC(2026, 0, 1, 0, 5, 0)
ts
const app = createApp();
const heartbeat = job(app, "heartbeat")
  .every(5_000)
  .run(() => undefined);
ts
"interval"

packages/jobs/src/job.ts:60

interfaceJobBuilder ​

ts
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().

ts
const app = createApp();
const descriptor = job(app, "nightly")
  .cron("0 0 * * *")
  .run(() => undefined);
ts
"jobs.job"
"nightly"
"cron"
1

packages/jobs/src/job.ts:45

interfaceJobRun ​

ts
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.

packages/jobs/src/job.ts:34

Collector ​

interfaceCollectedJob ​

ts
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 ​

ts
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.

ts
job(app, "cleanup")
  .every(60_000)
  .run(() => undefined);
job(app, "report")
  .cron("0 2 * * 1")
  .run(() => undefined);
const collected = jobsOf(app);
ts
[
  "cleanup",
  "report",
]
"interval"

packages/jobs/src/collector.ts:24

Executor ​

interfaceJobScheduler ​

ts
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 ​

ts
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).

ts
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);
ts
Date.UTC(2026, 0, 5, 9, 0, 0)

packages/jobs/src/schedule.ts:152

construnJob ​

ts
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.

packages/jobs/src/job.ts:98

functionscheduler ​

ts
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.

ts
const handle = scheduler(app);
const stop = handle.tick();
ts
0

packages/jobs/src/scheduler.ts:50

interfaceSchedulerOptions ​

ts
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 ​

ts
function compileCron(expr: string): CompiledCron;

Parses a 5-field POSIX cron expression into CompiledCron.

packages/jobs/src/schedule.ts:94

interfaceJobDescriptor ​

ts
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.

packages/jobs/src/job.ts:15

interfaceJobHandlerDescriptor ​

ts
interface JobHandlerDescriptor extends Descriptor<"jobs.handler", {
    readonly fn: JobHandler;
}> {
}

A node wrapping the function a job runs.

packages/jobs/src/job.ts:26

Reference ​

interfaceCompiledCron ​

ts
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 ​

ts
type FieldMatcher = (value: number) => boolean;

Predicate deciding whether a single field value is allowed.

packages/jobs/src/schedule.ts:42

Surface ​

constjobs ​

ts
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.

ts
const jobsBundle = jobs;
ts
"function"
"function"
"function"
"function"
"function"

packages/jobs/src/index.ts:24

Types ​

typeJobHandler ​

ts
type JobHandler = EmptyHandler;

The zero-input function a job runs each time it fires.

packages/jobs/src/job.ts:8

typeJobSchedule ​

ts
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.

packages/jobs/src/schedule.ts:5

Released under the MIT License.

/ search•[ ] prev/next•j/k scroll•h home•t theme•api/smitejs-jobs