Our Global Presence
Canada
57 Sherway St,
Stoney Creek, ON
L8J 0J3
India
606, Suvas Scala,
S P Ring Road, Nikol,
Ahmedabad 380049
USA
1131 Baycrest Drive,
Wesley Chapel,
FL 33544
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:
There are three popular modules that are sorted based on GitHub starts
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
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
2- At every 10th minute.
3- Every odd minute
4- At every minute from 10 through 20 on Sunday and Monday.
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
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
pm2 start index.js -i max
pm2 logs
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
>>> Solution
For solving this problem you should use NODE_APP_INSTANCE environment variable to run a cronjob only on one process,
The result
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
With these conditions, the cron job runs in the local environment and runs in the prod environment with cluster mode.
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
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:
57 Sherway St,
Stoney Creek, ON
L8J 0J3
606, Suvas Scala,
S P Ring Road, Nikol,
Ahmedabad 380049
1131 Baycrest Drive,
Wesley Chapel,
FL 33544
57 Sherway St,
Stoney Creek, ON
L8J 0J3
606, Suvas Scala,
S P Ring Road, Nikol,
Ahmedabad 380049
1131 Baycrest Drive,
Wesley Chapel,
FL 33544
© 2024 — HK Infosoft. All Rights Reserved.
© 2024 — HK Infosoft. All Rights Reserved.
T&C | Privacy Policy | Sitemap