dyouberg dyouberg - 3 months ago 19
Node.js Question

Is it more efficient to run a Node JS application through CRON at set times, or to run a 24/7 Node Application utilizing CRON inherently?

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:


  • CRON Job for daily process - calls daily NodeJS App

  • CRON Job for weekly process - calls weekly NodeJS App

  • Advantage is the application only runs when it needs to.

  • Disadvantage is overhead and organization. You are required to have separate projects/scripts for each different action.



Option B:


  • Separate NodeJS application which runs 24/7.

  • Calls it's daily operations daily, weekly operations weekly, etc.

  • Advantage: One project containing all CRON rules - easy to add more tasks

  • Disadvantage: Project must be running 24/7, more overhead when not needed.


Answer

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
Comments