简体   繁体   English

在生产节点服务器上部署cron服务的最佳实践?

[英]Best practice to deploy cron services on a production Node server?

Currently, I have a complex production NodeJS server that sends an e-mail to users through Sendgrid with the press of a button. 目前,我有一个复杂的生产NodeJS服务器, 只需按一下按钮就可以通过Sendgrid向用户发送电子邮件。 This server is then deployed using Docker and Convox into a different production environment called 'production', and tested at a "staging" level. 然后使用Docker和Convox将此服务器部署到名为“production”的不同生产环境中,并在“staging”级别进行测试。

I want to automate the functionality of sending e-mails to users by using the cron NPM package . 我想通过使用cron NPM软件包自动化向用户发送电子邮件的功能。 This cron job sends an e-mail to customers everyday automatically at a specified time. 此cron作业每天在指定时间自动向客户发送电子邮件。 A problem I'm facing is the fact that the staging environment Node environment is the exact same as the production environment (both called 'production'). 我面临的一个问题是,临时环境节点环境与生产环境完全相同(都称为“生产”)。 One possible solution is that I change the Docker/Convox configurations to have a separate Node environment for "staging" as well to prevent the cron job from running there. 一种可能的解决方案是我将Docker / Convox配置更改为具有用于“暂存”的单独节点环境,以防止cron作业在那里运行。

However that got me thinking - what is the best practice to deploy cron-job services with functionality that is closely coupled to processes in my Node server? 然而,这让我想到了 - 使用与我的Node服务器中的流程紧密耦合的功能来部署cron-job服务的最佳实践是什么?

Should the cron service be running on the same Node server? cron服务应该在同一个节点服务器上运行吗? A concerned that was raised about this was that it may create a more "monolithic" instance with less fragmentation. 引起关注的一个问题是,它可能会创建一个更加“整体”的实例,而且碎片更少。

Should the cron service be running on a separate EC2 NodeJS or AWS Lambda? cron服务应该在单独的EC2 NodeJS还是AWS Lambda上运行?

I am trying to figure out the best way to structure the deployment of cron services so that it is extensible and scalable. 我试图找出构建cron服务部署的最佳方法,以便它是可扩展和可扩展的。 In the future I wish to add more cron services with similar levels of functionality. 在未来,我希望添加更多具有类似功能级别的cron服务。

Should the cron service be running on the same Node server? cron服务应该在同一个节点服务器上运行吗?

If the cron service does not need access to any live state within your existing node.js server, then you are free to either include it in the existing server or break it out into its own process more akin to a microservices architecture where you break apart things that don't have to be coupled. 如果cron服务不需要访问现有node.js服务器中的任何实时状态,那么您可以将其包含在现有服务器中,也可以将其分解为自己的进程,更类似于您拆分的微服务架构不需要耦合的东西。 This is simply a design choice that depends upon a bunch of different things. 这只是一个设计选择,取决于一堆不同的东西。

It's really a tradeoff between keeping things simple for deployment and management (one server process to deploy and manage) versus separating out things that don't need to be coupled and can be run/managed/coded/tested independently. 在部署和管理(一个服务器流程部署和管理)之间保持简单,而不是分离出不需要耦合的东西,可以独立运行/管理/编码/测试,这实际上是一种权衡。

Typically, you'd make this tradeoff decision based on complexity. 通常,您会根据复杂性做出权衡决策。 If the email code is fairly trivial and doesn't particularly impact the memory or CPU usage much, then there's really no particular advantage to creating another server to manage just like you wouldn't think about breaking out other small things from your server. 如果电子邮件代码相当简单并且对内存或CPU使用率没有特别大的影响,那么创建另一个服务器来管理就没有特别的优势,就像你不会考虑从服务器中分解其他小东西一样。 But, if there's a real reason to manage it separately or you can scale easier by getting it's memory and CPU usage out of your main server process, then by all means break it out into its own server. 但是,如果有一个真正的理由单独管理它,或者你可以通过从主服务器进程中获取内存和CPU使用来扩展,那么通过各种方法将其分解到自己的服务器中。

I'll offer you a couple examples that show the extremes. 我将为您提供一些展示极端情况的例子。

Imagine you had 20 lines of code in your server that did some maintenance operation for you once a day (imagine it was log file management). 想象一下,你的服务器中有20行代码,每天为你做一次维护操作(想象一下是日志文件管理)。 They take about 10 seconds to run and really aren't complicated at all. 它们需要大约10秒才能运行,而且实际上并不复杂。 Would you break that out into another node.js server. 你会把它分解成另一个node.js服务器吗? Probably not because there's no main benefit to breaking it out, yet there is added complexity to now have yet another process to run and manage. 可能不是因为分解它没有主要好处,但现在又增加了复杂性,以便运行和管理另一个流程。

Now imagine you had a process that was kicked off at a certain time at night that crawled the entire sites of 100 related companies gathering information from their sites. 现在想象一下,你有一个过程在晚上的某个时间开始,爬过100个相关公司的整个网站,从他们的网站收集信息。 Because of the size of these sites, this could run for hours and could use a fair amount of CPU, memory, bandwidth and database cycles storing what it finds. 由于这些站点的大小,这可能会持续数小时,并且可能会使用相当数量的CPU,内存,带宽和数据库周期来存储它找到的内容。 Here there are real scale and management reasons to put this in its own separate process where it can be managed and scaled separately and where running it doesn't need to affect the ability of your other server to do its job effectively. 这里有真正的规模和管理原因,可以将它放在自己独立的流程中,可以单独管理和扩展,运行它不需要影响其他服务器有效完成工作的能力。

Should the cron service be running on a separate EC2 NodeJS or AWS Lambda? cron服务应该在单独的EC2 NodeJS还是AWS Lambda上运行?

This really depends upon your scale and management needs. 这实际上取决于您的规模和管理需求。 See the above examples. 见上面的例子。


On your other topic... 关于你的另一个话题......

One possible solution is that I change the Docker/Convox configurations to have a separate Node environment for "staging" as well to prevent the cron job from running there. 一种可能的解决方案是我将Docker / Convox配置更改为具有用于“暂存”的单独节点环境,以防止cron作业在那里运行。

This seems to be a completely separate issue from what the rest of your question is about. 这似乎与您的其他问题完全不同。 It seems like it would be useful to have a separate environment variable that indicates STAGING. 似乎有一个单独的环境变量指示STAGING是有用的。 That way code that only cares about the difference between production and development can just look at the one existing environment variable (and you don't have to change any of that code that already does this). 那样只关注生产和开发之间差异的代码只需查看一个现有的环境变量(并且您不必更改已经执行此操作的任何代码)。 But code that wants to know the difference between staging and production can then examine the new variable. 但是,想要了解暂存和生产之间差异的代码可以检查新变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM