在 Node.js 和 Cron 作业中设置间隔?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18120909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set Interval in Node.js vs. Cron Job?
提问by Philip Kirkbride
I'm learning node.js and just set up an empty Linux Virtual Machine and installed node.
我正在学习 node.js,只是设置了一个空的 Linux 虚拟机并安装了节点。
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)
并考虑根据当前时间添加一些其他功能。-(例如,如果 dateTime = 周日中午运行函数)
My question is are there any disadvantages to running a set up like this compared to a traditional cron job set up?
我的问题是,与传统的 cron 作业设置相比,运行这样的设置有什么缺点吗?
Keep in mind I have to run this function in node every minute anyways.
请记住,无论如何我必须每分钟在节点中运行此函数。
采纳答案by Timothy Strimple
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 onlything 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.
这取决于您必须遵守该分钟间隔的严格程度,以及您的节点脚本在此期间是否正在执行任何其他操作。如果脚本唯一要做的就是在每个 X 运行一些东西,我会强烈考虑只让你的节点脚本执行 X,并使用适当的操作系统调度程序对其进行调度。
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.
如果你在 node 中构建和运行它,你必须管理应用程序的生命周期并确保它正在运行,从崩溃中恢复等。 通过 CRON 每分钟执行一次更直接,在我看来更符合Unix哲学。
回答by Peter Lyons
My question is are there any disadvantages to running a set up like this compared to a traditional cron job set up?
我的问题是,与传统的 cron 作业设置相比,运行这样的设置有什么缺点吗?
As long as //run the codeisn't a CPU-bound thing like cryptography, stick with 1 node process, at least to start. Since you are requiring requestI guess you might be making an HTTP request, which is IO, which means this will be fine.
只要//run the code不是像密码学那样受 CPU 限制的东西,就坚持使用 1 个节点进程,至少要开始。由于您需要,request我猜您可能正在发出 HTTP 请求,即 IO,这意味着这会很好。
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.
安装/启动/启动/停止/升级/连接调试器比处理应用程序服务器以及单独的 cron 管理进程更简单。就其价值而言,将其保存在 javascript 中使其可跨平台移植,尽管这可能并不重要。
There is also a handy node-cronmodule which I have used as well as approximately one bazillion other alternatives.
我还使用了一个方便的node-cron模块以及大约无数的其他替代方案。

