Skip to content

Commit 583a1df

Browse files
Rafal Augustyniakrafixer
authored andcommitted
async worker initialization
1 parent 70a526e commit 583a1df

16 files changed

+92
-19
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.3.0] - 2020-11-24
9+
10+
### Added
11+
12+
- new options `asyncWorkerInitialization` that enable async initialization of worker
13+
814
## [2.2.1] - 2020-09-17
915

1016
### Changed

Readme.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ Describes a WorkerNodes options.
9595
* [WorkerNodesOptions](#WorkerNodesOptions)
9696
* [.autoStart](#WorkerNodesOptions+autoStart) : <code>Boolean</code>
9797
* [.lazyStart](#WorkerNodesOptions+lazyStart) : <code>Boolean</code>
98+
* [.asyncWorkerInitialization](#WorkerNodesOptions+asyncWorkerInitialization) : <code>Boolean</code>
9899
* [.minWorkers](#WorkerNodesOptions+minWorkers) : <code>Number</code>
99100
* [.maxWorkers](#WorkerNodesOptions+maxWorkers) : <code>Number</code>
100101
* [.maxTasks](#WorkerNodesOptions+maxTasks) : <code>Number</code>
@@ -119,6 +120,15 @@ If true, depending on the [lazyStart](#WorkerNodesOptions+lazyStart) option, it
119120
### options.lazyStart : <code>Boolean</code>
120121
Whether should start a new worker only if all the others are busy.
121122

123+
**Kind**: instance property of [<code>WorkerNodesOptions</code>](#WorkerNodesOptions)
124+
**Default**: <code>false</code>
125+
<a name="WorkerNodesOptions+asyncWorkerInitialization"></a>
126+
127+
### options.asyncWorkerInitialization : <code>Boolean</code>
128+
Enables async initialization of worker.
129+
To start handling task over worker, need to invoke `sendWorkerMessage('ready')` function when it fully initialized.
130+
For examples please refer to [the test cases](https://github.com/allegro/node-worker-nodes/blob/master/e2e/async-initialization.spec.js)
131+
122132
**Kind**: instance property of [<code>WorkerNodesOptions</code>](#WorkerNodesOptions)
123133
**Default**: <code>false</code>
124134
<a name="WorkerNodesOptions+minWorkers"></a>
@@ -199,7 +209,7 @@ const myModuleWorkerNodes = new WorkerNodes('/home/joe.doe/workspace/my-module')
199209
myModuleWorkerNodes.call().then(msg => console.log(msg)); // -> 'hello from separate process!'
200210
```
201211

202-
For more advanced examples please refer to [the test cases](https://github.com/allegro/node-worker-nodes/blob/master/tests/e2e.spec.js).
212+
For more advanced examples please refer to [the test cases](https://github.com/allegro/node-worker-nodes/tree/master/e2e).
203213

204214

205215
## Running tests

docs-src/Readme.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const myModuleWorkerNodes = new WorkerNodes('/home/joe.doe/workspace/my-module')
4141
myModuleWorkerNodes.call().then(msg => console.log(msg)); // -> 'hello from separate process!'
4242
```
4343

44-
For more advanced examples please refer to [the test cases](https://github.com/allegro/node-worker-nodes/blob/master/tests/e2e.spec.js).
44+
For more advanced examples please refer to [the test cases](https://github.com/allegro/node-worker-nodes/tree/master/e2e).
4545

4646

4747
## Running tests

e2e/async-initialization.spec.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const test = require('ava');
2+
3+
const WorkerNodes = require('../');
4+
const { fixture } = require('./utils');
5+
6+
test('should not mark worker as ready until module fully initialized', t => {
7+
// given
8+
const workerNodes = new WorkerNodes(fixture('async-initialization'), {
9+
maxWorkers: 1,
10+
asyncWorkerInitialization: true,
11+
autoStart: true
12+
});
13+
14+
// then
15+
t.falsy(workerNodes.pickWorker());
16+
});
17+
18+
test('should correctly handle task after initialization', async t => {
19+
// given
20+
const workerNodes = new WorkerNodes(fixture('async-initialization'), {
21+
maxWorkers: 1,
22+
asyncWorkerInitialization: true,
23+
autoStart: true
24+
});
25+
26+
// when
27+
const result = await workerNodes.call.result();
28+
29+
// then
30+
t.is(result, 'result');
31+
});

e2e/call-timeout.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ test('should result with rejection of all the calls that the worker was processi
4949
// when
5050

5151
const failingCall = workerNodes.call.task500ms().catch(error => error);
52-
await wait(150);
52+
await wait(200);
5353

5454
const secondCall = workerNodes.call.task100ms().catch(error => error);
5555
const results = await Promise.all([failingCall, secondCall]);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const sendWorkerMessage = require('../../lib/util/send-worker-message');
2+
3+
let result = '';
4+
5+
setTimeout(() => {
6+
result = 'result';
7+
sendWorkerMessage('ready');
8+
}, 200);
9+
10+
module.exports = {
11+
result: () => result,
12+
};

index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
interface Options {
22
autoStart?: boolean;
33
lazyStart?: boolean;
4+
asyncWorkerInitialization?: boolean;
45
minWorkers?: number;
56
maxWorkers?: number;
67
maxTasks?: number;
@@ -17,6 +18,7 @@ interface WorkerNodesInstance {
1718
terminate: () => Promise<WorkerNodesInstance>;
1819
profiler: (duration?: number) => void;
1920
takeSnapshot: () => void;
21+
getUsedWorkers: () => Array<Worker>;
2022
}
2123

2224
interface CallProperty {

lib/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
module.exports = require('./pool');
1+
module.exports = require('./pool');
2+
module.exports.sendWorkerMessage = require('./util/send-worker-message');

lib/options.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class WorkerNodesOptions {
99
constructor({
1010
autoStart = false,
1111
lazyStart = false,
12+
asyncWorkerInitialization = false,
1213
minWorkers = 0,
1314
maxWorkers = system.cpus().length,
1415
maxTasks = Infinity,
@@ -37,6 +38,16 @@ class WorkerNodesOptions {
3738
*/
3839
this.lazyStart = Boolean(lazyStart);
3940

41+
/**
42+
* Enables async initialization of worker.
43+
* To start handling task over worker, need to invoke `sendWorkerMessage('ready')` function when it fully initialized.
44+
* For examples please refer to [the test cases](https://github.com/allegro/node-worker-nodes/blob/master/e2e/async-initialization.spec.js)
45+
*
46+
* @type {Boolean}
47+
* @default false
48+
*/
49+
this.asyncWorkerInitialization = Boolean(asyncWorkerInitialization);
50+
4051
/**
4152
* The minimum number of workers that needs to be running to consider the whole pool as operational.
4253
*
@@ -127,7 +138,8 @@ class WorkerNodesOptions {
127138
srcFilePath,
128139
maxTasks: this.maxTasksPerWorker,
129140
endurance: this.workerEndurance,
130-
stopTimeout: this.workerStopTimeout
141+
stopTimeout: this.workerStopTimeout,
142+
asyncWorkerInitialization: this.asyncWorkerInitialization
131143
}
132144
}
133145
}

lib/status.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)