Croner

Usage (JavaScript)

Basic usage #


Croner uses the function new Cron() which takes in three arguments:

const job = new Cron(
    /* The pattern */
    "* * * * * *",
    /* Options (optional) */
    { maxRuns: 1 },
    /* Function (optional) */
    () => {}
);

If the function is omitted in the constructor, it can be scheduled later:

job.schedule((job, context) => {});

The job will be scheduled to run at the next matching time unless you supply the option { paused: true }. The Cron(...) constructor will return a Cron instance, later referred to as job, which have a few methods and properties.

Status #

Check the status of the job using the following methods:

job.nextRun( /*optional*/ startFromDate );    // Get a Date object representing the next run.
job.nextRuns(10, /*optional*/ startFromDate ); // Get an array of Dates, containing the next n runs.
job.previousRuns(10, /*optional*/ referenceDate ); // Get an array of Dates, containing previous n scheduled runs.
job.enumerate( /*optional*/ startFromDate );  // Get a stateful CronIterator for use in for...of / destructuring.
job.msToNext( /*optional*/ startFromDate ); // Get the milliseconds left until the next execution.
job.currentRun();         // Get a Date object showing when the current (or last) run was started.
job.previousRun( );         // Get a Date object showing when the previous job was started.

job.match( date );     // Check if a Date object or date string matches the cron pattern (true or false).

job.isRunning();     // Indicates if the job is scheduled and not paused or killed (true or false).
job.isStopped();     // Indicates if the job is permanently stopped using `stop()` (true or false).
job.isBusy();         // Indicates if the job is currently busy doing work (true or false).

job.getPattern();     // Returns the original cron pattern string, or undefined for date-based jobs
job.getOnce();     // Returns the original run-once date (Date or null)
job.describe( /*optional*/ language ); // Describe the pattern in words, or undefined for date-based jobs

Describing a pattern #

job.describe() returns a description of the pattern in English, such as "At 12:00, on Monday, Tuesday, Wednesday, Thursday, and Friday." for 0 12 * * MON-FRI. Pass swedish, or your own phrases, for another language. Spread a language to change parts of it:

import { Cron, english, swedish } from "croner";

new Cron("0 12 * * MON-FRI").describe(swedish);
// "Klockan 12:00, på måndag, tisdag, onsdag, torsdag och fredag."

const twelveHour = { ...english, timeOfDay: (h, m) => `${h % 12 || 12}:${String(m).padStart(2, "0")} ${h < 12 ? "AM" : "PM"}` };
new Cron("0 14 * * *").describe(twelveHour); // "At 2:00 PM."

Control Functions #

Control the job using the following methods:

job.trigger();     // Force a trigger instantly
job.pause();       // Pause trigger
job.resume();      // Resume trigger
job.stop();        // Stop the job completely. It is not possible to resume after this.
                   // Note that this also removes named jobs from the exported `scheduledJobs` array.

Properties #

job.name             // Optional job name, populated if a name were passed to options