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: https://github.com/nodejs/node/pull/38659
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
This commit is contained in:
James M Snell authored and Michaël Zasso committed 2021-09-04 09:23:24 +02:00
1 parent 93af04afbb
commit 99735a6fe8
3 files changed
+24

No files matched your search

+9
View File
@@ -455,6 +455,15 @@ of the custom deprecation.
The `*-deprecation` command-line flags only affect warnings that use the name
`'DeprecationWarning'`.
### Event: `'worker'`
<!-- YAML
added: REPLACEME
-->
* `worker` {Worker} The {Worker} that was created.
The `'worker'` event is emitted after a new {Worker} thread has been created.
#### Emitting custom warnings
See the [`process.emitWarning()`][process_emit_warning] method for issuing
+2
View File
@@ -261,6 +261,8 @@ class Worker extends EventEmitter {
};
// Actually start the new thread now that everything is in place.
this[kHandle].startThread();
process.nextTick(() => process.emit('worker', this));
}
[kOnExit](code, customErr, customErrReason) {
+13
View File
@@ -0,0 +1,13 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const {
Worker,
} = require('worker_threads');
process.on('worker', common.mustCall(({ threadId }) => {
assert.strictEqual(threadId, 1);
}));
new Worker('', { eval: true });