Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Interval in Node.js vs. Cron Job?

I'm learning node.js and just set up an empty Linux Virtual Machine and installed node.

I'm running a function constantly every minute

var request = require('request') var minutes = 1, the_interval = minutes * 60 * 1000  setInterval(function() {     // Run code   }) }, the_interval); 

And considering adding some other functions based on current time. - (e.g. run function if dateTime = Sunday at noon)

My question is are there any disadvantages to running a set up like this compared to a traditional cron job set up?

Keep in mind I have to run this function in node every minute anyways.

like image 917
Philip Kirkbride Avatar asked Aug 08 '13 08:08

Philip Kirkbride


People also ask

Can I use setInterval in node js?

What is the use of setInterval() method in Node. js ? The setInterval() method helps us to repeatedly execute a function after a fixed delay. It returns a unique interval ID which can later be used by the clearInterval() method which stops further repeated execution of the function.

Is node-cron blocking?

First, node-cron has the same merits and demerits as Node. js, being a runtime of JavaScript, which happens to be a non-blocking single-threaded language that uses the event loop. Secondly, to understand the merit part of that fact, note that there is a difference between an asynchronous task and a synchronous task.

What is cron job node js?

The node-cron module is tiny task scheduler in pure JavaScript for node. js based on GNU crontab. This module allows you to schedule task in node. js using full crontab syntax.


2 Answers

My question is are there any disadvantages to running a set up like this compared to a traditional cron job set up?

As long as //run the code isn't a CPU-bound thing like cryptography, stick with 1 node process, at least to start. Since you are requiring request I guess you might be making an HTTP request, which is IO, which means this will be fine.

It's just simpler to have 1 thing to install/launch/start/stop/upgrade/connect-a-debugger than to deal with an app server as well as a separate cron-managed process. For what it's worth, keeping it in javascript makes it portable across platforms, although that probably doesn't really matter.

There is also a handy node-cron module which I have used as well as approximately one bazillion other alternatives.

like image 60
Peter Lyons Avatar answered Sep 18 '22 18:09

Peter Lyons


It depends on how strictly you have to adhere to that minute interval and if your node script is doing anything else in the meantime. If the only thing the script does is run something every X, I would strongly consider just having your node script do X instead, and scheduling it using the appropriate operating system scheduler.

If you build and run this in node, you have to manage the lifecycle of the app and make sure it's running, recover from crashes, etc. Just executing once a minute via CRON is much more straightforward and in my opinion conforms more to the Unix Philosophy.

like image 43
Timothy Strimple Avatar answered Sep 22 '22 18:09

Timothy Strimple