Skip to content

Commit 2c80048

Browse files
committed
process: add 'worker' event
Provides a new `process.on('worker', (worker) =>{})` event that is triggered by the creation of a new `worker_thread.Worker`. The use case is to allow hooks to be installed for monitoring workers without having to modify the call sites around those. Signed-off-by: James M Snell <[email protected]> PR-URL: #38659 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Gerhard Stöbich <[email protected]>
1 parent aefc621 commit 2c80048

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

‎doc/api/process.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,15 @@ of the custom deprecation.
457457
The `*-deprecation` command-line flags only affect warnings that use the name
458458
`'DeprecationWarning'`.
459459

460+
### Event: `'worker'`
461+
<!-- YAML
462+
added: REPLACEME
463+
-->
464+
465+
*`worker`{Worker} The{Worker} that was created.
466+
467+
The `'worker'` event is emitted after a new{Worker} thread has been created.
468+
460469
#### Emitting custom warnings
461470

462471
See the [`process.emitWarning()`][process_emit_warning] method for issuing

‎lib/internal/worker.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ class Worker extends EventEmitter{
266266
};
267267
// Actually start the new thread now that everything is in place.
268268
this[kHandle].startThread();
269+
270+
process.nextTick(()=>process.emit('worker',this));
269271
}
270272

271273
[kOnExit](code,customErr,customErrReason){

‎test/parallel/test-worker-event.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
constcommon=require('../common');
4+
constassert=require('assert');
5+
const{
6+
Worker,
7+
}=require('worker_threads');
8+
9+
process.on('worker',common.mustCall(({ threadId })=>{
10+
assert.strictEqual(threadId,1);
11+
}));
12+
13+
newWorker('',{eval: true});

0 commit comments

Comments
(0)