Skip to content

Commit 99735a6

Browse files
jasnelltargos
authored andcommittedSep 4, 2021
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 <jasnell@gmail.com> PR-URL: #38659 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
1 parent 93af04a commit 99735a6

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed
 

‎doc/api/process.md

+9
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,15 @@ of the custom deprecation.
455455
The `*-deprecation` command-line flags only affect warnings that use the name
456456
`'DeprecationWarning'`.
457457

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

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

‎lib/internal/worker.js

+2
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ class Worker extends EventEmitter {
261261
};
262262
// Actually start the new thread now that everything is in place.
263263
this[kHandle].startThread();
264+
265+
process.nextTick(() => process.emit('worker', this));
264266
}
265267

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

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

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = 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+
new Worker('', { eval: true });

0 commit comments

Comments
 (0)
Please sign in to comment.