Javascript Node.js 中的后台进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31009340/
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
Background processes in Node.js
提问by Ole Spaarmann
What is a good aproach to handle background processes in a NodeJS application?
在 NodeJS 应用程序中处理后台进程的好方法是什么?
Scenario: After a user posts something to an app I want to crunch the data, request additional data from external resources, etc. All of this is quite time consuming, so I want it out of the req/res loop. Ideal would be to just have a queue of jobs where you can quickly dump a job on and a daemon or task runner will always take the oldest one and process it.
场景:在用户向应用程序发布内容后,我想处理数据、从外部资源请求其他数据等。所有这些都非常耗时,因此我希望它脱离 req/res 循环。理想的情况是只有一个作业队列,您可以在其中快速转储作业,守护程序或任务运行程序将始终采用最旧的一个并处理它。
In RoR I would have done it with something like Delayed Job. What is the Node equivalent of this API?
在 RoR 中,我会使用诸如延迟作业之类的方法来完成。这个 API 的 Node 等价物是什么?
回答by Yuri Zarubin
If you want something lightweight, that runs in the same process as the server, I highly recommend Bull. It has a simple API that allows for a fine grained control over your queues.
如果你想要轻量级的东西,它在与服务器相同的进程中运行,我强烈推荐Bull。它有一个简单的 API,允许对您的队列进行细粒度的控制。
If you're looking for something that runs as a standalone worker process, perhaps look into Kue. It can run as a RESTful API server, and even has several front-end apps written for it.
如果您正在寻找作为独立工作进程运行的东西,也许可以查看Kue。它可以作为 RESTful API 服务器运行,甚至可以为它编写多个前端应用程序。
If you're familiar with Ruby's Resque, there is a node implementation called Node-resque
如果您熟悉 Ruby 的 Resque,则有一个名为Node-resque的节点实现
Bull, Kue and Node-resque are all backed by Redis, which is ubiquitous among Node.js worker queues. All 3 would be able to do what RoR's DelayedJob does, it's matter of specific features that you want, and your API preferences.
Bull、Kue 和 Node-resque 都由Redis支持,它在 Node.js 工作队列中无处不在。所有这 3 个都可以做 RoR 的 DelayedJob 所做的事情,这取决于您想要的特定功能以及您的 API 首选项。
回答by wberry
Background jobs are not directly related to your web service work, so they should not be in the same process. As you scale up, the memory usage of the background jobs will impact the web service performance. But you can put them in the same code repository if you want, whatever makes more sense.
后台作业与您的 Web 服务工作没有直接关系,因此它们不应在同一进程中。随着规模的扩大,后台作业的内存使用量将影响 Web 服务性能。但是如果你愿意,你可以把它们放在同一个代码存储库中,任何更有意义的东西。
One good choice for messaging between the two processes would be redis, if dropping a message every now and then is OK. If you want "no message left behind" you'll need a more heavyweight broker like Rabbit. Your web service process can publish and your background job process can subscribe.
在两个进程之间进行消息传递的一个不错的选择是redis,如果不时删除一条消息就可以了。如果你想要“不留下任何消息”,你需要一个更重量级的经纪人,比如Rabbit。您的 Web 服务进程可以发布,您的后台作业进程可以订阅。
It is not necessary for the two processes to be co-hosted, they can be on separate VMs, Docker containers, whatever you use. This allows you to scale out without much trouble.
这两个进程不必共同托管,它们可以位于不同的 VM、Docker 容器上,无论您使用什么。这使您可以轻松扩展。
回答by sean2078
If you're using MongoDB, I recommend Agenda. That way, separate Redis instances aren't running and features such as scheduling, queuing, and Web UI are all present. Agenda UIis optional and can be run separately of course.
如果您使用 MongoDB,我推荐Agenda。这样,单独的 Redis 实例不会运行,并且诸如调度、排队和 Web UI 之类的功能都存在。 议程 UI是可选的,当然可以单独运行。
Would also recommend setting up a loosely coupled abstraction between your application logic and the queuing / scheduling system so the entire background processing system can be swapped out if needed. In other words, keep as much application / processing logic away from your Agenda job definitions in order to keep them lightweight.
还建议在您的应用程序逻辑和排队/调度系统之间设置松散耦合的抽象,以便在需要时可以更换整个后台处理系统。换句话说,让尽可能多的应用程序/处理逻辑远离您的议程作业定义,以保持它们的轻量级。
回答by stefkin
I'd like to suggest using Redisfor scheduling jobs. It has plenty of different data structures, you can always pick one that suits better to your use case.
我想建议使用Redis来安排作业。它有许多不同的数据结构,您可以随时选择最适合您的用例的数据结构。
You mentioned RoR and DJ, so I assume you're familiar with sidekiq. You can use node-sidekiqfor job scheduling if you want to, but its suboptimal imo, since it's main purpose is to integrate nodejs with RoR.
你提到了 RoR 和 DJ,所以我假设你熟悉 sidekiq。如果需要,您可以使用node-sidekiq进行作业调度,但它不是最佳的 imo,因为它的主要目的是将 nodejs 与 RoR 集成。
For worker daemonising I'd recommend using PM2. It's widely used and actively-maintained. It solves a lot of problems (e.g. deployment, monitoring, clustering) so make sure it won't be an overkill for you.
对于工人守护进程,我建议使用PM2。它被广泛使用和积极维护。它解决了很多问题(例如部署、监控、集群),因此请确保它不会对您造成过度杀伤。
回答by Qiulang
I tried bee-queue& bulland chose bull in the end. I first chose bee-queue b/c it is quite simple, their examples are easy to understand, while bull's examples are bit complicated. bee's wiki Bee Queue's Originalso resonates with me. But the problem with bee is <1> their issue resolution time is quite slow, their latest update was 10 months ago. <2> I can't find an easy way to pause/cancel job.
我尝试了bee-queue& Bull,最后选择了Bull。我首先选择了bee-queue b/c,很简单,他们的例子很容易理解,而bull的例子有点复杂。bee 的 wiki Bee Queue 的 Origin也引起了我的共鸣。但是 bee 的问题是 <1> 他们的问题解决时间很慢,他们的最新更新是 10 个月前。<2> 我找不到暂停/取消作业的简单方法。
Bull, on the other hand, frequently updates their codes, response to issues. Node.js job queue evaluationsaid bull's weakness is "slow issues resolution time", but my experience is the opposite!
另一方面,Bull 经常更新他们的代码,以应对问题。Node.js 作业队列评测说 Bull 的弱点是“问题解决时间慢”,但我的经验却恰恰相反!
But anyway their api is similar so it is quite easy to switch from one to another.
但无论如何他们的 api 是相似的,所以很容易从一个切换到另一个。
回答by Zio Mak Sò
I suggest to use a proper Node.js framework to build you app.
我建议使用合适的 Node.js 框架来构建您的应用程序。
I think that the most powerful and easy to use is Sails.js.
我认为最强大和最容易使用的是Sails.js。
It's a MVC framework so if you are used to develop in ROR, you will find it very very easy!
它是一个MVC框架,所以如果你习惯在ROR中开发,你会发现它非常非常容易!
If you use it, It's already present a powerful (in javascript terms) job manager.
如果你使用它,它已经提供了一个强大的(用 javascript 术语来说)作业管理器。
new sails.cronJobs('0 01 01 * * 0', function () {
sails.log.warn("START ListJob");
}, null, true, "Europe/Dublin");
If you need more info not hesitate to contact me!
如果您需要更多信息,请随时与我联系!