如何调试Node.JS小孩分叉进程? [英] How to debug Node.JS child forked process?

查看:109
本文介绍了如何调试Node.JS小孩分叉进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下方式创建子节点Node.JS进程:

  var child = require('child_process' ); 
child .fork(__ dirname +'/task.js');

问题是在IntelliJ / WebStorm中运行时,父进程和子进程都在同一端口上启动。

 调试器侦听端口40893 
调试器侦听端口40893
/ pre>

所以它只调试父进程。



有没有办法设置IntelliJ来调试子进程或强制它启动在不同的端口,所以我可以连接远程调试?

解决方案

这是一个已知的bug在最近修复的node.js中(虽然没有反向输入到v0.10)。



有关详细信息,请参阅此问题: https://github.com/joyent/node/issues/5318



有一个解决方法,您可以更改每个工作进程的命令行,尽管API不是以这种方式使用(解决方法可能会在将来停止工作)。以下是github问题的源代码:

  var cluster = require('cluster'); 
var http = require('http');

if(cluster.isMaster){
var debug = process.execArgv.indexOf(' - debug')!== -1;
cluster.setupMaster({
execArgv:process.execArgv.filter(function(s){return s!=='--debug'})
});对于(var i = 0; i< 2; ++ i){
if(debug)cluster.settings.execArgv.push(' - debug ='+(5859 + i))
;
cluster.fork();
如果(debug)cluster.settings.execArgv.pop();
}
}
else {
var server = http.createServer(function(req,res){
res.end('OK');
});
server.listen(8000);
}


I'm trying to debug the child Node.JS process created using:

var child = require('child_process');
child .fork(__dirname + '/task.js');

The problem is that when running in IntelliJ/WebStorm both parent and child process start on the same port.

debugger listening on port 40893
debugger listening on port 40893

So it only debugs the parent process.

Is there any way to set IntelliJ to debug the child process or force it to start on a different port so I can connect it in Remote debug?

解决方案

It is a known bug in node.js that has been recently fixed (although not backported to v0.10).

See this issue for more details: https://github.com/joyent/node/issues/5318

There is a workaround where you alter the command-line for each worker process, although the API was not meant to be used this way (the workaround might stop working in the future). Here is the source code from the github issue:

var cluster = require('cluster');
var http = require('http');

if (cluster.isMaster) {
  var debug = process.execArgv.indexOf('--debug') !== -1;
  cluster.setupMaster({
    execArgv: process.execArgv.filter(function(s) { return s !== '--debug' })
  });
  for (var i = 0; i < 2; ++i) {
    if (debug) cluster.settings.execArgv.push('--debug=' + (5859 + i));
    cluster.fork();
    if (debug) cluster.settings.execArgv.pop();
  }
}
else {
  var server = http.createServer(function(req, res) {
    res.end('OK');
  });
  server.listen(8000);
}

这篇关于如何调试Node.JS小孩分叉进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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