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

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

问题描述

我正在学习 node.js,只是设置了一个空的 Linux 虚拟机并安装了节点.

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);

并考虑添加一些基于当前时间的其他功能.-(例如,如果 dateTime = 周日中午运行函数)

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

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

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.

推荐答案

这取决于您必须遵守该分钟间隔的严格程度,以及您的节点脚本在此期间是否正在执行任何其他操作.如果脚本所做的唯一 事情是每个 X 运行一些东西,我会强烈考虑让您的节点脚本改为执行 X,并使用适当的操作系统调度程序对其进行调度.

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.

如果你在 node 中构建和运行它,你必须管理应用程序的生命周期并确保它正在运行,从崩溃中恢复等.通过 CRON 每分钟执行一次更直接,在我看来更符合到 Unix 哲学.

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 和 Cron 作业中设置间隔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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