What is cron?
The cron npm package is used for scheduling tasks to be executed at specific times or intervals. It is inspired by the Unix cron scheduler and allows for the use of cron syntax to schedule tasks in a Node.js environment. This package is useful for setting up jobs like backups, sending emails, or cleaning up databases at regular intervals.
What are cron's main functionalities?
Basic Cron Job
This feature allows you to create a basic cron job that runs at a specified interval. In the provided code sample, a new CronJob is created that logs a message to the console every second.
"const CronJob = require('cron').CronJob;\nconst job = new CronJob('* * * * * *', function() {\n console.log('You will see this message every second');\n}, null, true, 'America/Los_Angeles');\njob.start();"
Time Zone Support
This feature demonstrates the ability to specify a time zone for the cron job. The code sample schedules a job to run at 11:30 AM, according to the 'America/New_York' time zone, from Monday to Friday.
"const CronJob = require('cron').CronJob;\nconst job = new CronJob('00 30 11 * * 1-5', function() {\n console.log('This runs at 11:30 AM (server time) every Monday through Friday.');\n}, null, true, 'America/New_York');\njob.start();"
Dynamic Job Scheduling
This feature allows for dynamic scheduling of jobs. The schedule can be updated or changed based on certain conditions or inputs. In the example, the 'dynamicSchedule' variable can be updated to change the job's schedule.
"const CronJob = require('cron').CronJob;\nlet dynamicSchedule = '00 30 11 * * 1-5'; // This can be dynamically changed\nconst job = new CronJob(dynamicSchedule, function() {\n console.log('This job's schedule can be dynamically changed.');\n}, null, true, 'America/Los_Angeles');\njob.start();"
Other packages similar to cron
node-schedule
node-schedule is a flexible cron-like and not-cron-like job scheduler for Node.js. It allows for more complex scheduling and includes features like job cancellation. It is a good alternative to cron when more flexibility is required.
agenda
Agenda is a job scheduling library for Node.js that uses MongoDB for job storage. It offers more robust job management features compared to cron, such as persistence, job prioritization, and repeating jobs. It's a good choice when jobs need to be managed across server restarts or in distributed systems.
bull
Bull is a Redis-based queue system for Node.js. It is not a direct alternative to cron but can be used for scheduling through delayed jobs. It offers advanced features like job prioritization, concurrency control, and job events. Bull is suitable for applications requiring high reliability and real-time processing.
A NodeJS fork of jamespadolsey's cron.js.
Usage:
var cron = require('cron'), sys = require('sys');
new cron.CronJob('* * * * * *', function(){
sys.puts('You will see this message every second');
});
Available Cron patterns:
Asterisk. E.g. *
Ranges. E.g. 1-3,5
Steps. E.g. */2
Read up on cron patterns here.
Another example
var cron = require('cron'), sys = require('sys');
new CronJob('00 30 11 * * 2-6', function(){
// Runs every weekday (Monday through Friday)
// at 11:30:00 AM. It does not run on Saturday
// or Sunday.
});
How to check if a cron pattern is valid:
try {
new cron.CronTime('invalid cron pattern', function() {
sys.puts('this should not be printed');
})
} catch(ex) {
sys.puts("cron pattern not valid");
}
Install
From source: `sudo npm install`
From npm: `sudo npm install cron`
Contributors
- Nick Campbell
- Finn Herpich
- James Padolsey
- cliftonc
- Finn
- neyric
- humanchimp
License
This is under a dual license, MIT and GPL. However, the author of cron.js hasn't specified which version of the GPL, once I know I'll update this project and the packaging files.
Trademarks?
Node.js™ is an official trademark of Joyent. This module is not formally related to or endorsed by the official Joyent Node.js open source or commercial project