Reputation: 2332
Is it more efficient to run a Node JS application through CRON at set times, or to run a 24/7 Node Application utilizing schedulers inherently?
Option A:
Option B:
Upvotes: 1
Views: 896
Reputation: 3150
If you are looking at daily and weekly frequency only, I would go for a CRON job that calls your NodeJS app. If that task runs in 5 minutes, you will only have CPU and RAM utilisation during those minutes, instead of having a full NodeJS app staying in memory and using CPU resources all day (although minimal).
You don't need two directories; you can only have one directory where you have a daily.js job file and a weekly.js file. Or you could even have 1 single file (index.js) and call it with an argument to trigger the daily or weekly job. In your index.js you would need to read the process.argv array to read the argument that was passed in.
The index.js code would look as follows:
if (process.argv[2] == "D") {
//code for daily task
} else if (process.argv[2] == "W") {
//code for weekly task
} else {
throw new Error("Invalid argument");
}
And your crontab file would look like this (to run the daily job at 3am every day and the weekly job at 6am on Monday for example):
0 3 * * * node /path/index.js D
0 6 * * 1 node /path/index.js W
Upvotes: 2