Reference

Types

The options you pass when creating a job, and the types the package exports.

On this page

CronOptions

Passed to baker.add() and Cron.create().

name string Required
Identifies the job. Must be unique within a baker.
cron string Required
A six-field expression or a preset.
callback () => void | Promise<void> Required
The work to do on each tick.
start boolean Default false
Start the job as soon as it’s created. On a baker, this defaults to the value of autoStart.
immediate boolean Default false
Also run once when the job starts, without waiting for the first tick. See Running on start.
delay number | string
Hold back the immediate run. A number of milliseconds, or a string such as "500ms", "10s", "2m", "1h" or "1d". Has no effect without immediate.
overrunProtection boolean Default true
Skip a tick if the previous run hasn’t finished. See Overlapping runs.
onTick () => void
Called after a run succeeds. See when each hook fires.
onError (error: Error) => void
Called when a run fails.
onComplete () => void
Called when the job is destroyed, not after each run.
priority number Default 0
The order bakeAll() starts jobs in, highest first. Has no other effect.
maxHistory number Default 100
How many recent runs to keep. On a baker, this defaults to schedulerConfig.maxHistoryEntries.
persist boolean Default true
Include this job in saved state. Only matters on a baker with persistence on.
logger Logger Default console
Where this job’s messages go. On a baker, this defaults to the baker’s logger.

IBakerOptions

Passed to Baker.create(). Baker options lists each one.

Status

type Status = "running" | "stopped" | "paused" | "error";

"error" is reserved, and nothing currently sets it. See States.

CronTime

The result of Cron.parse(). Each field is a sorted list of the values it matches.

type CronTime = {
  second?: number[];
  minute?: number[];
  hour?: number[];
  dayOfMonth?: number[];
  month?: number[];
  dayOfWeek?: number[];
};

Logger

interface Logger {
  info(message: any, ...args: any[]): void;
  warn(message: any, ...args: any[]): void;
  error(message: any, ...args: any[]): void;
  debug(message: any, ...args: any[]): void;
}

Preset types

CronExprs is the union of every preset. It’s built from these, which are also exported.

type unit = "seconds" | "minutes" | "hours" | "dayOfMonth" | "months" | "dayOfWeek";
type day = "sunday" | "monday" | "tuesday" | "wednesday" | "thursday" | "friday" | "saturday";

type EveryStrType = `@every_${string}_${unit}`;
type AtHourStrType = `@at_${number}:${number}`;
type OnDayStrType = `@on_${day}`;
type BetweenStrType = `@between_${number}_${number}`;

CronExpression is any preset or six-field string, and CronExpressionType<T> is what makes cron reject unknown presets.

Other exports

ICron and IBaker are the interfaces Cron and Baker implement. ICronParser is the interface for CronParser.

Types without an export

The package entry doesn’t export every type yet. You can derive the missing ones from types that it does export.

import type { IBakerOptions, ICron } from "cronbake";

type JobMetrics = ReturnType<ICron["getMetrics"]>;
type ExecutionHistory = ReturnType<ICron["getHistory"]>[number];

type PersistenceOptions = NonNullable<IBakerOptions["persistence"]>;
type SchedulerConfig = NonNullable<IBakerOptions["schedulerConfig"]>;
type PersistenceProvider = NonNullable<PersistenceOptions["provider"]>;
type PersistedState = Parameters<PersistenceProvider["save"]>[0];
esc

Try overlap, restore or timezone.