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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With