Skip to content

Instantly share code, notes, and snippets.

@jbielick
Last active August 29, 2015 14:10
Show Gist options
  • Save jbielick/9e6af73f61c4ed14dfef to your computer and use it in GitHub Desktop.
Save jbielick/9e6af73f61c4ed14dfef to your computer and use it in GitHub Desktop.
Example of the difficulty in attaching a debugger to Node.js cluster forks if they're cycled (development environments)
var cluster = require("cluster");
var worker;
if (cluster.isMaster) {
function spawnWorker(i) {
var child;
cluster.settings.execArgv || (cluster.settings.execArgv = []);
// this has no effect
cluster.settings.execArgv.push('--debug=' + 5859);
child = cluster.fork();
console.log('forked');
cluster.settings.execArgv.pop();
child.on('exit', function() {
spawnWorker(i);
});
setTimeout(function() {
child.kill();
}, i * 2000);
return child;
}
spawnWorker(1);
} else {
console.log('im a fork');
debugger
}
/*
output:
┌[josh@jarvice] [130]
└[~/code/clustertest]> node --debug=5000 app.js
Debugger listening on port 5000
forked
Debugger listening on port 5001
im a fork
forked
Debugger listening on port 5002
im a fork
forked
Debugger listening on port 5003
im a fork
forked
Debugger listening on port 5004
im a fork
forked
Debugger listening on port 5005
im a fork
master debug is reachable at 5000, but every fork gets a new port no matter what.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment