在Node.js vs. Cron Job中设置间隔? [英] Set Interval in Node.js vs. Cron Job?

查看:136
本文介绍了在Node.js vs. Cron Job中设置间隔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我每分钟都在运行一个函数

我正在学习node.js并且只是设置一个空的Linux虚拟机和已安装的节点。 >

  var request = require('request')
var minutes = 1,the_interval = minutes * 60 * 1000

setInterval(function(){
//运行代码
})
},the_interval);

并考虑根据当前时间添加一些其他函数。 - (例如,如果dateTime =星期日中午,运行函数)



我的问题是,与传统的cron作业相比,运行这样的设置有什么缺点吗?



请记住,我必须每分钟在节点中运行此函数。

解决方案

这取决于您必须严格遵守该分钟间隔,以及您的节点脚本是否正在执行其他操作。如果脚本执行的只是每X运行一次,我强烈地考虑只是让你的节点脚本做X,并使用适当的操作系统调度程序调度它。



如果您在节点中构建并运行此应用程序,则必须管理应用程序的生命周期,并确保其运行,崩溃恢复等。只需每分钟执行一次通过CRON更直接,在我看来更符合 Unix哲学


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.

解决方案

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.

这篇关于在Node.js vs. Cron Job中设置间隔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆