API Reference

CronJob

interface CronJob extends cron.ParserOptions<false> {
  id: string;
  type: "cron";
  expression: string;
  execute: ExecuteFn;
}

A job that is executed based on a CRON expression. Backed by cron-parser.

cron.ParserOptions includes options like timezone.

Properties

defineJobScheduler

function defineJobScheduler(options?: JobSchedulerConfig): JobScheduler {
  // ...
}

Requires the alarms permission.

Creates a JobScheduler backed by the alarms API.

Parameters

  • options?: JobSchedulerConfig

Returns

A JobScheduler that can be used to schedule and manage jobs.

ExecuteFn

type ExecuteFn = () => Promise<any> | any;

Function ran when executing the job. Errors are automatically caught and will trigger the "error" event. If a value is returned, the result will be available in the "success" event.

IntervalJob

interface IntervalJob {
  id: string;
  type: "interval";
  duration: number;
  immediate?: boolean;
  execute: ExecuteFn;
}

A job that executes on a set interval, starting when the job is scheduled for the first time.

Properties

  • id: string
  • type: 'interval'
  • duration: number
    Interval in milliseconds. Due to limitations of the alarms API, it must be greater than 1 minute.
  • immediate?: boolean (default: false)
    Execute the job immediately when it is scheduled for the first time. If false, it will execute for the first time after duration. This has no effect when updating an existing job.
  • execute: ExecuteFn

Job

type Job = IntervalJob | CronJob | OnceJob;

JobScheduler

interface JobScheduler {
  scheduleJob(job: Job): Promise<void>;
  removeJob(jobId: string): Promise<void>;
  on(
    event: "success",
    callback: (job: Job, result: any) => void,
  ): RemoveListenerFn;
  on(
    event: "error",
    callback: (job: Job, error: unknown) => void,
  ): RemoveListenerFn;
}

JobSchedulerConfig

interface JobSchedulerConfig {
  logger?: Logger | null;
}

Configures how the job scheduler behaves.

Properties

  • logger?: Logger | null (default: console)
    The logger to use when logging messages. Set to null to disable logging.

Logger

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

Interface used to log text to the console when creating and executing jobs.

OnceJob

interface OnceJob {
  id: string;
  type: "once";
  date: Date | string | number;
  execute: ExecuteFn;
}

Runs a job once, at a specific date/time.

Properties

  • id: string
  • type: 'once'
  • date: Date | string | number
    The date to run the job on.
  • execute: ExecuteFn




API reference generated by docs/generate-api-references.ts