Skip to content
Snippets Groups Projects
Commit c776a5e0 authored by Jakob Moosbrugger's avatar Jakob Moosbrugger
Browse files

added debug info in readme, range debug port pick

parent 7209f6f3
No related branches found
No related tags found
No related merge requests found
......@@ -44,6 +44,43 @@ worker.onmessage = function (ev) {
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
#### onmessage
Message handler, accepts an `Event`
......@@ -61,6 +98,10 @@ Broadcasts a message to the `Worker`
#### terminate()
Terminates the `Worker`
#### static setRange(min, max)
Sets range for debug ports, only affects current process.
Returns true if successful.
## FAQ
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
......
......@@ -9,13 +9,13 @@ var path = require("path"),
worker = path.join(__dirname, "worker.js"),
events = /^(error|message)$/,
defaultPorts = { inspect: 9229, debug: 5858 };
var forkedChilds = 0;
var range = { min: 1, max: 300 };
var Worker = function () {
function Worker(arg) {
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() };
_classCallCheck(this, Worker);
......@@ -59,8 +59,7 @@ var Worker = function () {
if (match[2]) {
port = parseInt(match[2]);
}
debugVars[portIndex] = "--" + match[1] + "=" + (port + 1 + forkedChilds); //new parameter
forkedChilds++;
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
......@@ -119,6 +118,17 @@ var Worker = function () {
value: function terminate() {
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;
......
......@@ -3,10 +3,10 @@ const path = require("path"),
worker = path.join(__dirname, "worker.js"),
events = /^(error|message)$/,
defaultPorts = {inspect: 9229, debug: 5858};
let forkedChilds = 0;
let range = {min: 1, max: 300};
class Worker {
constructor (arg, args = undefined, options = {cwd: process.cwd()}) {
constructor (arg, args = [], options = {cwd: process.cwd()}) {
let isfn = typeof arg === "function",
input = isfn ? arg.toString() : arg;
......@@ -40,8 +40,7 @@ class Worker {
if (match[2]) {
port = parseInt(match[2]);
}
debugVars[portIndex] = "--" + match[1] + "=" + (port + 1 + forkedChilds); //new parameter
forkedChilds++;
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
match = (/^(--debug)(?:-brk)?(.*)/).exec(debugVars[debugIndex]);
......@@ -83,6 +82,16 @@ class Worker {
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) {
if (events.test(event)) {
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