Skip to content

Commit f4758d7

Browse files
Rafal Augustyniakrafixer
authored andcommitted
add getWorkersQueue method
1 parent 9f5aead commit f4758d7

File tree

8 files changed

+53
-10
lines changed

8 files changed

+53
-10
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [2.2.0] - 2020-09-17
9+
10+
### Added
11+
12+
- new method: `workerNodes.getUsedWorkersReference` that return list with used workers in pool
13+
814
## [2.1.0] - 2020-07-07
915

1016
### Added

Readme.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ $ npm install worker-nodes
2626
* [.call](#WorkerNodes+call) : <code>Proxy</code>
2727
* [.ready()](#WorkerNodes+ready) ⇒ <code>Promise</code>
2828
* [.terminate()](#WorkerNodes+terminate) ⇒ <code>Promise</code>
29-
* [.profiler(duration)](#WorkerNodes+profiler)
30-
* [.takeSnapshot()](#WorkerNodes+takeSnapshot)
29+
* [.profiler(duration)](#WorkerNodes+profiler) ⇒ <code>void</code>
30+
* [.takeSnapshot()](#WorkerNodes+takeSnapshot) ⇒ <code>void</code>
31+
* [.getUsedWorkers()](#WorkerNodes+getUsedWorkers) ⇒ <code>Array.&lt;Worker&gt;</code>
3132

3233
<a name="new_WorkerNodes_new"></a>
3334

@@ -62,7 +63,7 @@ Starts the process of terminating this instance.
6263
**Returns**: <code>Promise</code> - - resolved when the instance is terminated.
6364
<a name="WorkerNodes+profiler"></a>
6465

65-
### workerNodes.profiler(duration)
66+
### workerNodes.profiler(duration) ⇒ <code>void</code>
6667
Run CPU Profiler and save result on main process directory
6768

6869
**Kind**: instance method of [<code>WorkerNodes</code>](#WorkerNodes)
@@ -73,9 +74,15 @@ Run CPU Profiler and save result on main process directory
7374

7475
<a name="WorkerNodes+takeSnapshot"></a>
7576

76-
### workerNodes.takeSnapshot()
77+
### workerNodes.takeSnapshot() ⇒ <code>void</code>
7778
Take Heap Snapshot and save result on main process directory
7879

80+
**Kind**: instance method of [<code>WorkerNodes</code>](#WorkerNodes)
81+
<a name="WorkerNodes+getUsedWorkers"></a>
82+
83+
### workerNodes.getUsedWorkers() ⇒ <code>Array.&lt;Worker&gt;</code>
84+
Return list with used workers in pool
85+
7986
**Kind**: instance method of [<code>WorkerNodes</code>](#WorkerNodes)
8087

8188
<a name="WorkerNodesOptions"></a>

e2e/main.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const test = require('ava');
33
const WorkerNodes = require('../');
44
const errors = require('../lib/errors');
55
const { fixture, unique, repeatCall } = require('./utils');
6+
const { Worker } = require('worker_threads');
67

78
test('should be exposed as a constructor function', t => {
89
t.throws(() => WorkerNodes(), { instanceOf: TypeError }, "cannot be invoked without 'new'")
@@ -101,3 +102,18 @@ test('should kill worker that got stuck in an infinite loop', async t => {
101102
// then
102103
await t.throwsAsync(workerNodes.call.infiniteLoop, { instanceOf: errors.TimeoutError });
103104
});
105+
106+
test('should return used workers list', async t => {
107+
// given
108+
const workerNodes = new WorkerNodes(fixture('process-info'), {
109+
autoStart: true,
110+
maxWorkers: 10,
111+
minWorkers: 10
112+
});
113+
await workerNodes.ready();
114+
115+
const workersList = workerNodes.getUsedWorkers();
116+
117+
// then
118+
workersList.forEach(worker => t.true(worker instanceof Worker));
119+
});

lib/pool.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,9 @@ class WorkerNodes extends EventEmitter {
347347

348348
/**
349349
* Run CPU Profiler and save result on main process directory
350-
*
351-
* @param {number} duration
350+
*
351+
* @param {number} duration
352+
* @returns {void}
352353
*/
353354
profiler(duration) {
354355
const worker = this.pickWorker();
@@ -360,7 +361,8 @@ class WorkerNodes extends EventEmitter {
360361

361362
/**
362363
* Take Heap Snapshot and save result on main process directory
363-
*
364+
*
365+
* @returns {void}
364366
*/
365367
takeSnapshot() {
366368
const worker = this.pickWorker();
@@ -369,6 +371,15 @@ class WorkerNodes extends EventEmitter {
369371
worker.takeSnapshot();
370372
}
371373
}
374+
375+
/**
376+
* Return list with used workers in pool
377+
*
378+
* @returns {Array.<Worker>}
379+
*/
380+
getUsedWorkers() {
381+
return this.workersQueue.storage.map(worker => worker.process.child);
382+
}
372383
}
373384

374385
module.exports = WorkerNodes;

lib/worker/child-loader.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ function callSplitter(requestData) {
2828
return handleHeapSnapshot(requestData);
2929
}
3030

31-
return handleCall(requestData);
31+
if (cmd === 'request') {
32+
return handleCall(requestData);
33+
}
3234
}
3335

3436
function handleCall(requestData) {

lib/worker/message.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const errorMap = {
99

1010
class Request {
1111
constructor({ callId, workerId, method, args }) {
12+
this.cmd = 'request';
1213
this.callId = callId;
1314
this.workerId = workerId;
1415
this.method = method;

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "worker-nodes",
3-
"version": "2.1.0",
3+
"version": "2.2.0",
44
"description": "A library to run cpu-intensive tasks without blocking the event loop.",
55
"keywords": [
66
"workers",

0 commit comments

Comments
 (0)