Job scheduler in NodeJs
Posted By : Shashwat Gupta | 30-Nov-2017
What is Cron ??
Cron is a node js job scheduler which actuality run the the script in certain time iterations
Description :
Cron is a node js job scheduler made some updates and changes to the code base this has evolved to something that have a bit of both. The cron syntax parsing is mostly using timeout instead of interval
Additionally, Cron node js job scheduler library goes beyond the basic cron syntax and allows you to supply a Date object and promises. This will be used as the trigger for your callback. Cron job schedular syntax is still an acceptable CronTime format. Although the Cron patterns supported extend on the standard Unix format to support seconds and min. digits, its default to 0 and match with the Unix behavior.
As goes with Cron, breaking backwards compatibility should be explicit in the versioning of your library. we upgrade the version of this module in accordance with breaking changes (I am not always good about doing it this way so if we notice that there are breaking changes that have not been bumped appropriately please let me know).
Usage
var CronJob = require('cron').CronJob;
new CronJob('* * * * * *', function() {
console.log('You will see this message every second');
}, null, true, 'America/Los_Angeles');
Available Cron patterns:
for Ranges. Example 1-3,5
For Asterisk. Example. *
For Steps. Example */2
Cron Ranges
When specifying your cron values you'll need to make sure that your values fall within the ranges. like, cron uses a 0-7 range for the day of week where 0 and 7 represent Sunday. .
Seconds: 0-59
Minutes: 0-59
Day of Month: 1-31
Day of Week: 0-6
Months: 0-11
Hours: 0-23
Install
From source: npm install
From npm: npm install cron /code>
API
CronJob:
constructor(cron job Time, on Tick, on Completed, begin , timezones, context, runOnInit) :-
first parameter here can be a JSON object that has the below names and associated types.
on Tick - [THIS IS REQUIRED] - The function to fire at the specified time.
on Completed - [THIS IS OPTIONAL] - A function that will fire when the job is complete, when it is stopped.
start - [THIS IS OPTIONAL] - where to start.
timeZoneS - [THIS IS OPTIONAL] - give the timezone for the script execution.
start - Runs your job.
stop - Stops your job
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Shashwat Gupta
Shashwat is a bright Mean Stack Developer . He has good experience in development of complex UI's of web application and hybrid mobile applications.