Cron jobs in Node.js
Cron Jobs in node.js is used for scheduling scripts to run, They’re most commonly used for automating doing a task like scraping data from another website, deleting some data from the database, etc. However, there are many situations when a web application may need specific tasks to run periodically.
In this article, the points to cover are:
- Best module for creating a cron job in Node.js
- Review cron job patterns
- Essential points a Node.js project with cron job
Best modules for creating a cron job in Node.js
There are three popular modules that are sorted based on GitHub starts
- node-schedule
- cron
- node-cron
But the number of downloads cron is more than other modules currently.
All three modules do a similar thing, but it’s preferred to use the cron module because there are more examples available in this repository examples
Review cron job patterns
There are multiple tutorials for cron job patterns
Note that the cron job modules have 6 values but the crontab website has 5 values because the first start (second) is optional
The cron format consists of: ``` * * * * * * ┬ ┬ ┬ ┬ ┬ ┬ │ │ │ │ │ │ │ │ │ │ │ └ day of week (0 - 7) (0 or 7 is Sun) │ │ │ │ └───── month (1 - 12) │ │ │ └────────── day of month (1 - 31) │ │ └─────────────── hour (0 - 23) │ └──────────────────── minute (0 - 59) └───────────────────────── second (0 - 59, OPTIONAL)
1- At 04:05

Pic courtesy: medium.com
2- At every 10th minute.

Pic courtesy: medium.com
3- Every odd minute

Pic courtesy: medium.com
4- At every minute from 10 through 20 on Sunday and Monday.

Pic courtesy: medium.com
Essential points a Node.js project with cron job
You know that Node.js is a single thread so using clustering in production, and running Node.js projects on multiple CPUs, to do this you can PM2.
If you use Cron job in Nest.js Framework or Express.js and use clustering so the cron job starts on multiple CPUs. Let’s review an example that shows you the problem.
Cronjob In Cluster Mode
>>> Problem
npm install cron

Pic courtesy: medium.com
Run the project based on cluster mode with PM2, the cluster mode allows networked Node.js applications (HTTP(s)/TCP/UDP server) to be scaled across all CPUs available
- Start cron job example
pm2 start index.js -i max
- Show logs in pm2
pm2 logs

Pic courtesy: medium.com
As you can see after 20 seconds the cron job has executed the desired function 8 times instead of 2 times, because the project clusters on 4 CPUs

Pic courtesy: medium.com
>>> Solution
For solving this problem you should use NODE_APP_INSTANCE environment variable to run a cronjob only on one process,

Pic courtesy: medium.com
The result

Pic courtesy: medium.com
As you see, cronjob is running just on one process.
If you want to run in different environments with cluster mode and none cluster mode, with the NODE_ENV variable, handle this problem
NODE_ENV=local node index

Pic courtesy: medium.com
With these conditions, the cron job runs in the local environment and runs in the prod environment with cluster mode.
Cronjob In Different Timezone
When you start a cron job on the server, It runs based on the time server and timezone of the server. the best approach is using the timezone ‘Etc/UTC’ instead of the local timezone server.
>>> Problem
Imagine there is a website and this website publishes new data every day at 8:00 AM o’clock. Your script is running on the server, the problem is that some timezone clocks change (daylight saving time) so when the time of the server changes, The execution time of your script is also affected
>>> Solution
Check the local time from the UTC time

Pic courtesy: medium.com
For more information and to develop web applications using Node JS, Hire Node Developer from us as we give you a high-quality product by utilizing all the latest tools and advanced technology. E-mail us any clock at – hello@hkinfosoft.com or Skype us: “hkinfosoft”.
To develop your custom web app using Node JS, please visit our technology page.
Content Source:
- medium.com