在 SailsJs 中使用 Node 集群模块:EADDRINUSE [英] Using Node cluster module with SailsJs: EADDRINUSE

查看:55
本文介绍了在 SailsJs 中使用 Node 集群模块:EADDRINUSE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于 SailsJs (http://sailsjs.org/) 的应用程序,它必须处理一些 CPU密集的任务.简而言之,我想使用 cluster (https://nodejs.org/api/cluster.html) 模块将这些任务的处理委托给工作进程,以便 Sails 应用程序的主事件循环不会被阻塞(因此可以正常响应请求).

I have a SailsJs (http://sailsjs.org/) based application that has to deal with some CPU intensive tasks. In short, I want to use the cluster (https://nodejs.org/api/cluster.html) module to delegate the processing of these tasks to worker processes so that the main event loop of the Sails application isn't blocked (and so can respond to requests as normal).

创建工作线程时,我收到 EADDRINUSE 错误,因为 Sails 尝试再次运行并绑定到同一端口.

When creating a worker, I'm getting an EADDRINUSE error as Sails is trying to run again and bind to the same port.

示例代码:

// SomeSailsService.js

var cluster = require('cluster');
var Queue = require('bull');
var myQueue = Queue('myQueue', 'connection stuff', 'etc');
var numWorkers = 2;
var i;

if (cluster.isMaster) {
  // Spawn some workers
  for (i = 0; i < numWorkers; i++) {
    cluster.fork();
  }
} else {
  // This is a worker, so bind to new job event
  myQueue.process(function processJob(job, done) {
    // CPU intensive shenanigans
  });
}

module.exports = {
  addToQueue: function(foo) {
    myQueue.add({foo: foo});
  }
};

运行上述程序时,Sails 应用程序启动,然后在启动时,两个工作人员尝试再启动该应用程序两次.这会导致以下两个错误:

When running the above, the Sails app starts up, then on starting the two workers tries to start the app another two times. This results in two of the following errors:

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: bind EADDRINUSE
    at errnoException (net.js:904:11)
    at net.js:1084:30
    at Object.1:1 (cluster.js:594:5)
    at handleResponse (cluster.js:171:41)
    at respond (cluster.js:192:5)
    at handleMessage (cluster.js:202:5)
    at process.emit (events.js:117:20)
    at handleMessage (child_process.js:322:10)
    at child_process.js:396:7
    at process.handleConversion.net.Native.got (child_process.js:91:7)
    at process.<anonymous> (child_process.js:395:13)
    at process.emit (events.js:117:20)
    at handleMessage (child_process.js:322:10)
    at Pipe.channel.onread (child_process.js:347:11)

有没有办法解决这个问题?或者解决这个问题的替代方法?

Is there a way around this? Or an alternative way of tackling this problem?

推荐答案

我最终遵循了 这篇博文 来启动应用的准系统实例.之后,解决我的问题的基本过程是:

I ended up following the advice of this blog post to boot up barebones instances of the app. Following on from that, the basic process to solve my problem was:

  • app.js中,检查cluster.isMaster === true.
  • 如果是,则正常启动应用程序并创建工作进程.
  • 如果不是,则它是一个工作进程,因此启动应用的最小版本没有 HTTP 服务器.注册作业进程处理程序.
  • In app.js, check if cluster.isMaster === true.
  • If it is, launch the app as normal and create worker process(es).
  • If not, it's a worker process so boot up a minimal version of the app without the HTTP server. Register job process handlers.

上面的博客文章更详细地(包括相当不错的实现)如何管理这些进程处理程序的创建和注册.它没有提到处理 cluster,但希望上述步骤可以帮助遇到此问题的其他人.

The above blog post goes into more detail (including quite a nice implementation) of how to manage the creation and registering of these process handlers. It doesn't mention dealing with cluster, but hopefully the above steps help anyone else who has run into this problem.

根据 Travis 的建议(见下面的评论),我放弃了 cluster 模块的使用,而是创建了一个 worker.js 文件,它做了很多相同的事情.即在 worker.js 中,我启动了一个准系统 Sails 应用程序,开始侦听新工作.我的主要 Sails 应用(网络 API)然后只响应添加和获取作业(低延迟的东西).

On advice of Travis (see below comment), I dropped the use of the cluster module and instead created a worker.js file that did much the same thing. i.e. in worker.js I booted up a barebones Sails app started listening for new jobs. My main Sails app (the web API) is then just responsive for adding and getting jobs (low latency stuff).

运行良好,应该可以轻松与 Heroku 等服务配合使用.

Works nicely and should work easily with services such as Heroku.

这篇关于在 SailsJs 中使用 Node 集群模块:EADDRINUSE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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