Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tiny-worker
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
sweng-group-15
tiny-worker
Commits
7b0b7671
Commit
7b0b7671
authored
7 years ago
by
Jason Mulligan
Committed by
GitHub
7 years ago
Browse files
Options
Downloads
Plain Diff
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
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
README.md
+41
-0
41 additions, 0 deletions
README.md
lib/index.js
+15
-3
15 additions, 3 deletions
lib/index.js
src/index.js
+14
-3
14 additions, 3 deletions
src/index.js
with
70 additions
and
6 deletions
README.md
+
41
−
0
View file @
7b0b7671
...
...
@@ -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
...
...
This diff is collapsed.
Click to expand it.
lib/index.js
+
15
−
3
View file @
7b0b7671
...
...
@@ -9,12 +9,13 @@ var path = require("path"),
worker
=
path
.
join
(
__dirname
,
"
worker.js
"
),
events
=
/^
(
error|message
)
$/
,
defaultPorts
=
{
inspect
:
9229
,
debug
:
5858
};
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
);
...
...
@@ -34,7 +35,7 @@ var Worker = function () {
if
(
debugVars
.
length
>
0
&&
!
options
.
noDebugRedirection
)
{
if
(
!
options
.
execArgv
)
{
//if no execArgs are given copy all arguments
debugVars
=
process
.
execArgv
;
debugVars
=
Array
.
from
(
process
.
execArgv
)
;
options
.
execArgv
=
[];
}
...
...
@@ -58,7 +59,7 @@ var Worker = function () {
if
(
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
...
...
@@ -117,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
;
...
...
This diff is collapsed.
Click to expand it.
src/index.js
+
14
−
3
View file @
7b0b7671
...
...
@@ -3,9 +3,10 @@ const path = require("path"),
worker
=
path
.
join
(
__dirname
,
"
worker.js
"
),
events
=
/^
(
error|message
)
$/
,
defaultPorts
=
{
inspect
:
9229
,
debug
:
5858
};
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
;
...
...
@@ -19,7 +20,7 @@ class Worker {
});
if
(
debugVars
.
length
>
0
&&
!
options
.
noDebugRedirection
)
{
if
(
!
options
.
execArgv
)
{
//if no execArgs are given copy all arguments
debugVars
=
process
.
execArgv
;
debugVars
=
Array
.
from
(
process
.
execArgv
)
;
options
.
execArgv
=
[];
}
...
...
@@ -39,7 +40,7 @@ class Worker {
if
(
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
match
=
(
/^
(
--debug
)(?:
-brk
)?(
.*
)
/
).
exec
(
debugVars
[
debugIndex
]);
...
...
@@ -81,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
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment