Skip to content
Snippets Groups Projects
Commit 7b0b7671 authored by Jason Mulligan's avatar Jason Mulligan Committed by GitHub
Browse files

Merge pull request #17 from dude8/master

Debug Port Range
parents 413eb73e c776a5e0
No related branches found
No related tags found
No related merge requests found
...@@ -44,6 +44,43 @@ worker.onmessage = function (ev) { ...@@ -44,6 +44,43 @@ worker.onmessage = function (ev) {
worker.postMessage("Hello World!"); worker.postMessage("Hello World!");
``` ```
# Debugging
To be able to debug a child process, it must have a differnt debug port than the parent.
Tiny worker does this by adding a random port within a range to the parents debug port.
The default Range is `[1, 300]`, it can be changed with the `setRange(min, max)` method.
To disable any automatic port redirection set `options.noDebugRedirection = true`.
### automatic redirection
```javascript
//parent is started with '--debug=1234'
var Worker = require("tiny-worker");
Worker.setRange(2, 20);
var worker = new Worker(function () {
postMessage(process.debugPort);
});
worker.onmessage = function (ev) {
console.log(ev.data); //prints any number between 1236 and 1254
worker.terminate();
}
```
### manual redirection
```javascript
//parent is started with '--debug=1234'
var Worker = require("tiny-worker");
var worker = new Worker(function () {
postMessage(process.debugPort);
}, [], {noDebugRedirection: true, execArgv: ["--debug=1235"]});
worker.onmessage = function (ev) {
console.log(ev.data); //prints 1235
worker.terminate();
}
```
## Properties ## Properties
#### onmessage #### onmessage
Message handler, accepts an `Event` Message handler, accepts an `Event`
...@@ -61,6 +98,10 @@ Broadcasts a message to the `Worker` ...@@ -61,6 +98,10 @@ Broadcasts a message to the `Worker`
#### terminate() #### terminate()
Terminates the `Worker` Terminates the `Worker`
#### static setRange(min, max)
Sets range for debug ports, only affects current process.
Returns true if successful.
## FAQ ## FAQ
1. I have an orphaned child process that lives on past the parent process' lifespan 1. I have an orphaned child process that lives on past the parent process' lifespan
* Most likely a `SIGTERM` or `SIGINT` is not reaching the child process * Most likely a `SIGTERM` or `SIGINT` is not reaching the child process
......
...@@ -9,12 +9,13 @@ var path = require("path"), ...@@ -9,12 +9,13 @@ var path = require("path"),
worker = path.join(__dirname, "worker.js"), worker = path.join(__dirname, "worker.js"),
events = /^(error|message)$/, events = /^(error|message)$/,
defaultPorts = { inspect: 9229, debug: 5858 }; defaultPorts = { inspect: 9229, debug: 5858 };
var range = { min: 1, max: 300 };
var Worker = function () { var Worker = function () {
function Worker(arg) { function Worker(arg) {
var _this = this; var _this = this;
var args = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined; var args = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : { cwd: process.cwd() }; var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : { cwd: process.cwd() };
_classCallCheck(this, Worker); _classCallCheck(this, Worker);
...@@ -34,7 +35,7 @@ var Worker = function () { ...@@ -34,7 +35,7 @@ var Worker = function () {
if (debugVars.length > 0 && !options.noDebugRedirection) { if (debugVars.length > 0 && !options.noDebugRedirection) {
if (!options.execArgv) { if (!options.execArgv) {
//if no execArgs are given copy all arguments //if no execArgs are given copy all arguments
debugVars = process.execArgv; debugVars = Array.from(process.execArgv);
options.execArgv = []; options.execArgv = [];
} }
...@@ -58,7 +59,7 @@ var Worker = function () { ...@@ -58,7 +59,7 @@ var Worker = function () {
if (match[2]) { if (match[2]) {
port = parseInt(match[2]); port = parseInt(match[2]);
} }
debugVars[portIndex] = "--" + match[1] + "=" + (port + 1); //new parameter debugVars[portIndex] = "--" + match[1] + "=" + (port + range.min + Math.floor(Math.random() * (range.max - range.min))); //new parameter
if (debugIndex >= 0 && debugIndex !== portIndex) { if (debugIndex >= 0 && debugIndex !== portIndex) {
//remove "-brk" from debug if there //remove "-brk" from debug if there
...@@ -117,6 +118,17 @@ var Worker = function () { ...@@ -117,6 +118,17 @@ var Worker = function () {
value: function terminate() { value: function terminate() {
this.child.kill("SIGINT"); this.child.kill("SIGINT");
} }
}], [{
key: "setRange",
value: function setRange(min, max) {
if (min >= max) {
return false;
}
range.min = min;
range.max = max;
return true;
}
}]); }]);
return Worker; return Worker;
......
...@@ -3,9 +3,10 @@ const path = require("path"), ...@@ -3,9 +3,10 @@ const path = require("path"),
worker = path.join(__dirname, "worker.js"), worker = path.join(__dirname, "worker.js"),
events = /^(error|message)$/, events = /^(error|message)$/,
defaultPorts = {inspect: 9229, debug: 5858}; defaultPorts = {inspect: 9229, debug: 5858};
let range = {min: 1, max: 300};
class Worker { class Worker {
constructor (arg, args = undefined, options = {cwd: process.cwd()}) { constructor (arg, args = [], options = {cwd: process.cwd()}) {
let isfn = typeof arg === "function", let isfn = typeof arg === "function",
input = isfn ? arg.toString() : arg; input = isfn ? arg.toString() : arg;
...@@ -19,7 +20,7 @@ class Worker { ...@@ -19,7 +20,7 @@ class Worker {
}); });
if (debugVars.length > 0 && !options.noDebugRedirection) { if (debugVars.length > 0 && !options.noDebugRedirection) {
if (!options.execArgv) { //if no execArgs are given copy all arguments if (!options.execArgv) { //if no execArgs are given copy all arguments
debugVars = process.execArgv; debugVars = Array.from(process.execArgv);
options.execArgv = []; options.execArgv = [];
} }
...@@ -39,7 +40,7 @@ class Worker { ...@@ -39,7 +40,7 @@ class Worker {
if (match[2]) { if (match[2]) {
port = parseInt(match[2]); port = parseInt(match[2]);
} }
debugVars[portIndex] = "--" + match[1] + "=" + (port + 1); //new parameter debugVars[portIndex] = "--" + match[1] + "=" + (port + range.min + Math.floor(Math.random() * (range.max - range.min))); //new parameter
if (debugIndex >= 0 && debugIndex !== portIndex) { //remove "-brk" from debug if there if (debugIndex >= 0 && debugIndex !== portIndex) { //remove "-brk" from debug if there
match = (/^(--debug)(?:-brk)?(.*)/).exec(debugVars[debugIndex]); match = (/^(--debug)(?:-brk)?(.*)/).exec(debugVars[debugIndex]);
...@@ -81,6 +82,16 @@ class Worker { ...@@ -81,6 +82,16 @@ class Worker {
this.child.send({input: input, isfn: isfn, cwd: options.cwd}); this.child.send({input: input, isfn: isfn, cwd: options.cwd});
} }
static setRange (min, max) {
if (min >= max) {
return false;
}
range.min = min;
range.max = max;
return true;
}
addEventListener (event, fn) { addEventListener (event, fn) {
if (events.test(event)) { if (events.test(event)) {
this["on" + event] = fn; this["on" + event] = fn;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment