简体   繁体   English

使用cluster.fork()调试Node.js进程

[英]Debugging Node.js processes with cluster.fork()

I've got some code that looks very much like the sample in the Cluster documentation at http://nodejs.org/docs/v0.6.0/api/cluster.html , to wit: 我有一些代码非常类似于群集文档中位于http://nodejs.org/docs/v0.6.0/api/cluster.html的示例,该代码包括:

var cluster = require('cluster');
var server = require('./mycustomserver');
var numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
  var i;
  // Master process
  for (i = 0; i < numCPUs; i++) {
    cluster.fork();
  }
  cluster.on('death', function (worker) {
    console.log('Worker ' + worker.pid + ' died');
  });
} else {
  // Worker process
  server.createServer({port: 80}, function(err, result) {
    if (err) {
      throw err;
    } else {
      console.log('Thread listening on port ' + result.port);
    }
  });
}

I've installed node-inspector and tried using both it and the Eclipse V8 plugin detailed at https://github.com/joyent/node/wiki/Using-Eclipse-as-Node-Applications-Debugger to debug my application, but it looks like I can't hook a debugger up to forked cluster instances to put breakpoints at the interesting server logic--I can only debug the part of the application that spawns the cluster processes. 我已经安装了node-inspector,并尝试使用它和Eclipse V8插件(在https://github.com/joyent/node/wiki/Using-Eclipse-as-Node-Applications-Debugger中详细介绍)来调试我的应用程序,但是看来我无法将调试器连接到派生的群集实例上,以在有趣的服务器逻辑上放置断点-我只能调试产生群集过程的应用程序部分。 Does anybody know if I can in fact do such a thing, or am I going to have to refactor my application to use only a single thread when in debugging mode? 有人知道我是否真的可以做这样的事情,还是在调试模式下必须重构我的应用程序以仅使用单个线程?

I'm a Node.js newbie, so I'm hoping there's something obvious I'm missing here. 我是Node.js新手,所以我希望这里缺少一些明显的东西。

var fixedExecArgv=[];
fixedExecArgv.push('--debug-brk=5859');
cluster.setupMaster({ 
  execArgv: fixedExecArgv 
});

Credit goes to Sergey's post . 归功于Sergey的职位

I changed my server.js to fork only one worker mainly for testing this then added the code above the forking. 我将server.js更改为仅派生一个工人来进行测试,然后在派生上方添加了代码。 This fixed the debugging issue for me. 这为我解决了调试问题。 Thank you Sergey for explaining and providing the solution!!! 感谢Sergey解释和提供解决方案!!!

I already opened a ticket about this here: https://github.com/dannycoates/node-inspector/issues/130 我已经在这里打开了关于此的票证: https : //github.com/dannycoates/node-inspector/issues/130

Although it's not fixed yet, there's a workaround: 尽管尚未修复,但是有一种解决方法:

FWIW: The reason I suspect is that the node debugger needs to bind to the debug port (default: 5858). FWIW:我怀疑原因是节点调试器需要绑定到调试端口(默认值:5858)。 If you are using Cluster, I am guessing the master/controller binds first, and succeeds, causing the bind in the children/workers to fail. 如果您使用的是集群,我猜主控制器/控制器将首先绑定并成功绑定,从而导致子代/工人中的绑定失败。 While a port can be supplied to node --debug=N there seems to be no easy way to do this when node is invoked within Cluster for the worker (it might be possible to programmatically set process.debug_port and then enable debugging, but I haven't got that working yet). 虽然可以将端口提供给节点--debug = N,但是在集群中为工作线程调用节点时,似乎没有简便的方法(可以通过编程方式设置process.debug_port然后启用调试,但是我还没有工作)。 Which leaves a bunch of options: 1) start node without the --debug option, and once it is running, find the pid for the worker process you want to debug/profile, and send it a USR1 signal to enable debugging. 这留下了很多选择:1)在没有--debug选项的情况下启动节点,并且在其运行后,找到要调试/配置文件的工作进程的pid,然后向其发送USR1信号以启用调试。 Another option is to write a wrapper for node that calls the real node binary with --debug set to a unique port each time. 另一个选择是为节点编写一个包装程序,该包装程序每次将--debug设置为唯一端口的真实节点二进制文件调用。 There are possibly options in Cluster that let you pass such as arg as well. Cluster中可能还有一些让您通过的选项,例如arg。

For those who wish to debug child processes in VS Code, just add this to launch.json configuration: 对于那些希望在VS Code中调试子进程的人,只需将其添加到launch.json配置中即可:

"autoAttachChildProcesses": true

https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_remote-debugging https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_remote-debugging

For anyone looking at this in 2018+, no startup arguments are necessary. 对于在2018年以上关注此问题的任何人,都不需要启动参数。

From this Github issue : 这个Github问题

Just a time saver for anyone who might have been in the same boat as me-- the Node.js V8 --inspector Manager (NiM) seems to introduce this issue when it otherwise wouldn't be present-- I spent about an hour banging my head before disabling the Chrome plugin and discovering that everything worked fine when opening from chrome://inspect. 对于那些可能和我同舟共济的人来说,这只是一个节省时间的工作-Node.js V8-检查器管理器(NiM)似乎会在其他情况下不存在时引入此问题-我花了大约一个小时在禁用Chrome插件之前发现我从chrome:// inspect打开时一切正常,然后敲了敲我的头。

I also spent hours reading github posts, tweaking the settings of gulp-typescript and gulp-sourcemaps, etc, only to have that plugin be the issue. 我还花了几个小时阅读github帖子,调整gulp-typescript和gulp-sourcemaps的设置,等等,只是让这个插件成为问题。 Also worth noting is that I had to add port N+1 to the remote targets of chrome://inspect , so localhost:9230 , to debug my worker process. 另外值得注意的是,我必须将端口N + 1添加到chrome://inspect的远程目标(即localhost:9230 )才能调试工作进程。

if you use VSCode to debug, you need to specify the port and and "autoAttachChildProcesses": true in the lanuch.json file. 如果使用VSCode进行调试,则需要在lanuch.json文件中指定端口和"autoAttachChildProcesses": true

If you debug directly in DevTool, you need to add a connection to the corresponding port in the console. 如果直接在DevTool中进行调试,则需要将连接添加到控制台中的相应端口。

在此处输入图片说明

对于节点版本高于或等于7.7.0的节点,请使用--inspect标志来调试node js进程,如果有人想了解有关如何调试集群处理以及为Node JS设置chrome调试器工具的更多信息,请在此处关注我的帖子。

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

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