nodejs:worker disconnect()无法按预期工作

时间:2015-08-11 06:06:49

标签: node.js multithreading

我正在尝试在有“/ disconnect”路由请求时断开工作组与群集的连接。即使master正在接收来自worker的断开事件,Worker也不会断开连接。

工人

var express=require("express"),
app=express(),
http=require("http"),
server = null;

app.get('/disconnect',function(req,res){
  var worker = require('cluster').worker;    
  setTimeout(function(){        
    // disconnect from the cluster            
    if(worker) worker.disconnect();                
   }, 2000);
  res.send("Worker " +   worker.id +  " is disconnecting ");
});

server = http.createServer(app).listen(3000);

server.on('close', function() {
   console.log("server asked to close");
});

LOG

> node app/app_cluster.js

CLUSTER: Worker 1 started
CLUSTER: Worker 2 started
CLUSTER: Worker 3 started
CLUSTER: Worker 4 started
Yay, the worker responded after it was forked
Yay, the worker responded after it was forked
Yay, the worker responded after it was forked
Yay, the worker responded after it was forked
A worker is now connected to null:3000
A worker is now connected to null:3000
A worker is now connected to null:3000
A worker is now connected to null:3000
CLUSTER: Worker 1 disconnected from the cluster.
events.js:85
  throw er; // Unhandled 'error' event
        ^
Error: IPC channel is already disconnected
at process.target.disconnect (child_process.js:510:26)
at Worker.disconnect (cluster.js:650:13)
at null._onTimeout (/home/user/nodejs/node-hawkeye/app/app.js:18:31)
at Timer.listOnTimeout (timers.js:110:15)
CLUSTER: Worker 1 died with exit code 1 (null)

我有两个疑问

  1. 对于第一个“/ disconnect”路由请求,我看到“CLUSTER:Worker 1与集群断开连接”。在日志中。我等待大约10秒后再次向“/ disconnect”路由发送请求。当我第二次发送时出现错误“错误:IPC通道已断开连接”。

  2. 根据文档https://nodejs.org/api/cluster.html#cluster_worker_disconnect

    在工作人员中,此功能将关闭所有服务器,等待这些服务器上的“关闭”事件,然后断开IPC频道。

    我在工作人员代码中没有看到服务器上的紧密事件。工作者会在worker.disconnect()内部调用server.close()吗?

2 个答案:

答案 0 :(得分:1)

我的第一个疑问得到澄清。在Master中的'disconnect'事件后,花了近两分钟才收到'退出'事件。对于我的测试用例,我在第一次请求后等待大约10秒钟向'/ disconnect'路由发送请求,似乎10秒是不够的。请参阅下面的日志,我在日志中记录了事件的时间。

<强>日志

> node app/app_cluster.js

CLUSTER: Worker 1 started
CLUSTER: Worker 2 started
CLUSTER: Worker 3 started
CLUSTER: Worker 4 started
Yay, the worker responded after it was forked
Yay, the worker responded after it was forked
Yay, the worker responded after it was forked
Yay, the worker responded after it was forked
A worker is now connected to null:3000
A worker is now connected to null:3000
A worker is now connected to null:3000
A worker is now connected to null:3000
CLUSTER: Worker 3 disconnected from the cluster.Tue Aug 11 2015    13:45:35 GMT+0530 (IST)
CLUSTER: Worker 3 died with exit code 0 (null)Tue Aug 11 2015 13:47:29 GMT+0530 (IST)

<强>更新

我不确定'disconnect'和'exit'事件之间的2分钟是否可以接受,但是,似乎有人检查worker.isConnected()以了解IPC连接的状态。

文档https://nodejs.org/api/cluster.html#cluster_cluster_workers

中有警告

在工作人员断开连接并退出后,工作人员将从cluster.workers中删除。这两个事件之间的顺序无法提前确定。但是,可以保证在发出最后一次“断开连接”或“退出”事件之前从cluster.workers列表中删除。

如果是这种情况,我不知道如果IPC通道已经死亡并且请求来自断开连接的工作人员,如何处理传入请求。

答案 1 :(得分:0)

.disconnect()关闭IPC(进程间通信)通道,server.close()关闭服务器以获取任何新的入站请求...这些正在关闭不同的方面(一个关闭连接到master和其他进程以及其他关闭到外部)。

在工作人员方面立即断开连接,只是断开它并且没有时间做关闭或任何事情......

如果你看一下docs中的例子,你可以看到它从master发送一条消息,当被worker接收时,会触发disconnect和kill timer。