Ende.cron

function · experimental

function Ende.cron(name: string, schedule: string | CronSchedule, handler: () => Promise<void> | void): Promise<void>

UNSTABLE: New API, yet to be vetted.

Create a cron job that will periodically execute the provided handler callback based on the specified schedule.

This is an unstable API and requires the --unstable-cron flag to be passed when running Ende.

Ende.cron("sample cron", "20 * * * *", () => {
  console.log("cron job executed");
});
Ende.cron("sample cron", { hour: { every: 6 } }, () => {
  console.log("cron job executed");
});

schedule can be a string in the Unix cron format or in JSON format as specified by interface CronSchedule, where time is specified using UTC time zone.

Calling Ende.cron registers the job and immediately returns. The returned promise should not be awaited to wait for the job to run, it is provided only to surface registration errors. The job keeps running in the background for the lifetime of the process. To be able to stop a cron job (for example during a graceful shutdown), use the overload that accepts an options object with an AbortSignal.

function · experimental

function Ende.cron(name: string, schedule: string | CronSchedule, options: { backoffSchedule?: number[]; signal?: AbortSignal }, handler: () => Promise<void> | void): Promise<void>

UNSTABLE: New API, yet to be vetted.

Create a cron job that will periodically execute the provided handler callback based on the specified schedule.

Ende.cron("sample cron", "20 * * * *", {
  backoffSchedule: [10, 20]
}, () => {
  console.log("cron job executed");
});

schedule can be a string in the Unix cron format or in JSON format as specified by interface CronSchedule, where time is specified using UTC time zone.

backoffSchedule option can be used to specify the retry policy for failed executions. Each element in the array represents the number of milliseconds to wait before retrying the execution. For example, [1000, 5000, 10000] means that a failed execution will be retried at most 3 times, with 1 second, 5 seconds, and 10 seconds delay between each retry. There is a limit of 5 retries and a maximum interval of 1 hour (3600000 milliseconds).

signal option can be used to stop the cron job by passing an AbortSignal and aborting it. This is useful for implementing a graceful shutdown, since aborting the signal unregisters the job:

const ac = new AbortController();

Ende.cron("sample cron", "20 * * * *", { signal: ac.signal }, () => {
  console.log("cron job executed");
});

// Later, stop the cron job from running again.
ac.abort();