Croner
Trigger functions or evaluate cron expressions in JavaScript or TypeScript....
README
Croner - Cron for JavaScript and TypeScript
- ```javascript
- // Basic: Run a function at the interval defined by a cron expression
- const job = Cron('*/5 * * * * *', () => {
- console.log('This will run every fifth second');
- });
- // Enumeration: What dates do the next 100 sundays occur on?
- const nextSundays = Cron('0 0 0 * * 7').nextRuns(100);
- console.log(nextSundays);
- // Days left to a specific date
- const msLeft = Cron('59 59 23 24 DEC *').nextRun() - new Date();
- console.log(Math.floor(msLeft/1000/3600/24) + " days left to next christmas eve");
- // Run a function at a specific date/time using a non-local timezone (time is ISO 8601 local time)
- // This will run 2024-01-23 00:00:00 according to the time in Asia/Kolkata
- Cron('2024-01-23T00:00:00', { timezone: 'Asia/Kolkata' }, () => { console.log('Yay!') });
- ```
Installation
Note
If you are migrating from a different library such as cron or node-cron, or upgrading from a older version of croner, see MIGRATION.md.
- ```javascript
- // ESM Import ...
- import { Cron } from "croner";
- // ... or CommonJS Require
- const Cron = require("croner");
- ```
- ```typescript
- import { Cron } from "https://deno.land/x/croner@6.0.3/dist/croner.js";
- ```
- ```html
- <script src="https://cdn.jsdelivr.net/npm/croner@6/dist/croner.umd.min.js"></script>
- ```
Documentation
Signature
- ```javascript
- // Parameters
- // - First: Cron pattern, js date object (fire once), or ISO 8601 time string (fire once)
- // - Second: Options (optional)
- // - Third: Function run trigger (optional)
- const job = Cron("* * * * * *", { maxRuns: 1 }, () => {} );
- // If function is omitted in constructor, it can be scheduled later
- job.schedule(job, /* optional */ context) => {});
- ```
Status
- ```javascript
- 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.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.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 pattern string
- ```
Control functions
- ```javascript
- 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
- ```javascript
- job.name // Optional job name, populated if a name were passed to options
- ```
Options
Key | Default | Data | Remarks |
---|---|---|---|
|--------------|----------------|----------------|---------------------------------------| | |||
name | undefined | String | If |
maxRuns | Infinite | Number | | |
catch | false | Boolean\|Function | Catch |
timezone | undefined | String | Timezone |
startAt | undefined | String | ISO |
stopAt | undefined | String | ISO |
interval | 0 | Number | Minimum |
paused | false | Boolean | If |
context | undefined | Any | Passed |
legacyMode | true | boolean | Combine |
unref | false | boolean | Setting |
utcOffset | undefined | number | Schedule |
protect | undefined | boolean\|Function | Enabled |
Warning
Unreferencing timers (option unref) is only supported by Node.js and Deno.
Browsers have not yet implemented this feature, and it does not make sense to use it in a browser environment.
Pattern
- ```javascript
- // ┌──────────────── (optional) second (0 - 59)
- // │ ┌────────────── minute (0 - 59)
- // │ │ ┌──────────── hour (0 - 23)
- // │ │ │ ┌────────── day of month (1 - 31)
- // │ │ │ │ ┌──────── month (1 - 12, JAN-DEC)
- // │ │ │ │ │ ┌────── day of week (0 - 6, SUN-Mon)
- // │ │ │ │ │ │ (0 to 6 are Sunday to Saturday; 7 is Sunday, the same as 0)
- // │ │ │ │ │ │
- // * * * * * *
- ```
Field | Required | Allowed | Allowed | Remarks |
---|---|---|---|---|
|--------------|----------|----------------|----------------------------|---------------------------------------| | ||||
Seconds | Optional | 0-59 | * | | |
Minutes | Yes | 0-59 | * | | |
Hours | Yes | 0-23 | * | | |
Day | Yes | 1-31 | * | | |
Month | Yes | 1-12 | * | | |
Day | Yes | 0-7 | * | 0 |
Note
Weekday and month names are case-insensitive. Both MON and mon work.
When using L in the Day of Week field, it affects all specified weekdays. For example, L5,6 means the last Friday and Saturday in the month."
Nickname | Description |
---|---|
-------- | ----------- |
\@yearly | Run |
\@annually | Run |
\@monthly | Run |
\@weekly | Run |
\@daily | Run |
\@hourly | Run |
Why another JavaScript cron implementation
| | cronosjs | node-cron | cron | node-schedule |
---|---|---|---|---|
|---------------------------|:-------------------:|:-------------------:|:---------:|:-------------------------:|:-------------------:| | ||||
**Platforms** | ||||
Node.js | ✓ | ✓ | ✓ | ✓ |
Browser | ✓ | ✓ | | | | |
Deno | ✓ | | | | | |
**Features** | ||||
Over-run | ✓ | | | | | |
Error | ✓ | | | | | |
Typescript | ✓ | ✓ | | | | |
Unref | ✓ | | | ✓ | | |
dom-OR-dow | ✓ | ✓ | ✓ | ✓ |
dom-AND-dow | ✓ | | | | | |
Next | ✓ | ✓ | | | ✓ |
Next | ✓ | ✓ | | | | |
Timezone | ✓ | ✓ | ✓ | ✓ |
Minimum | ✓ | | | | | |
Controls | ✓ | ✓ | ✓ | ✓ |
Range | ✓ | ✓ | ✓ | ✓ |
Stepping | ✓ | ✓ | ✓ | ✓ |
Last | ✓ | ✓ | | | | |
In depth comparison of various libraries
| | cronosjs | node-cron | cron | node-schedule |
---|---|---|---|---|
|---------------------------|:-------------------:|:-------------------:|:---------:|:-------------------------:|:-------------------:| | ||||
**Size** | ||||
Minified | 15.5 | 16.3 | 16.5 | - |
Bundlephobia | 3.6 | 5.1 | 5.7 | 23.9 |
Dependencies | 0 | 0 | 1 | 1 |
**Popularity** | ||||
Downloads/week | 576K | 31K | 433K | 2239K |
**Quality** | ||||
Issues | 0 | 2 | 127 | 43 |
Code | 99% | 98% | 100% | 81% |
**Performance** | ||||
Ops/s | 99 | 49 | N/A | Test |
Ops/s | 65 | 17 | N/A | Test |
**Tests** | **8/8** | **7/8** | **0/8** | **1/8** |
Test | 2022-10-09 | 2022-10-09 | N/A | 2022-10-09 |
Test | 2023-02-28 | 2023-02-28 | N/A | N/A |
Test | 2024-02-29 | 2024-02-29 | N/A | 2023-03-29 |
Test | 2048-02-09 | N/A | N/A | N/A |
Test | 2023-02-16 | 2023-02-16 | N/A | 2023-03-15 |
Test | 2022-10-10 | 2022-10-10 | N/A | 2022-11-07 |
Test | 2023-03-31 | 2023-03-31 | N/A | 2023-04-01 |
Test | 2023-05-04 | 2023-05-04 | N/A | 2023-06-03 |
Note
Table last updated at 2022-10-23, issues and downloads updated 2023-02-19
node-cron has no interface to predict when the function will run, so tests cannot be carried out.
All tests and benchmarks were carried out using https://github.com/Hexagon/cron-comparison
Development
Master branch
- ```
- npm install croner --save
- ```
Dev branch
- ```
- npm install croner@dev
- ```
Warning
Expect breaking changes if you do not pin to a specific version.