Skip to content

Commit 6bf949e

Browse files
authored
docs: add Worker API Documentation (#474)
* docs: update abort listener info * docs: add worker api section, move abort listener docs, add comment header to worker api * doc: add abortListenerTimeout docs * ref: fix last commit * doc: cleanup * doc: change comment on abort listener example in README
1 parent e9b7767 commit 6bf949e

File tree

2 files changed

+71
-34
lines changed

2 files changed

+71
-34
lines changed

README.md

+63-31
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ The following options are available:
197197
- In case of `'process'`, `child_process` will be used. Only available in a node.js environment.
198198
- In case of `'thread'`, `worker_threads` will be used. If `worker_threads` are not available, an error is thrown. Only available in a node.js environment.
199199
- `workerTerminateTimeout: number`. The timeout in milliseconds to wait for a worker to cleanup it's resources on termination before stopping it forcefully. Default value is `1000`.
200+
- `abortListenerTimeout: number`. The timeout in milliseconds to wait for abort listener's before stopping it forcefully, triggering cleanup. Default value is `1000`.
200201
- `forkArgs: String[]`. For `process` worker type. An array passed as `args` to [child_process.fork](https://nodejs.org/api/child_process.html#child_processforkmodulepath-args-options)
201202
- `forkOpts: Object`. For `process` worker type. An object passed as `options` to [child_process.fork](https://nodejs.org/api/child_process.html#child_processforkmodulepath-args-options). See nodejs documentation for available options.
202203
- `workerOpts: Object`. For `web` worker type. An object passed to the [constructor of the web worker](https://html.spec.whatwg.org/multipage/workers.html#dom-worker). See [WorkerOptions specification](https://html.spec.whatwg.org/multipage/workers.html#workeroptions) for available options.
@@ -393,7 +394,62 @@ workerpool.worker({
393394
});
394395
```
395396

396-
Tasks may configure an `abort handler` to perform cleanup operations when `timeout` or `cancel` is called on a `task`. the `abortListenerTimeout` option can be configured to control when cleanup should be aborted in the case an `abortHandler` never resolves. This timeout trigger will cause the given worker to be cleaned up. Allowing a new worker to be created if need be.
397+
### Events
398+
399+
You can send data back from workers to the pool while the task is being executed using the `workerEmit` function:
400+
401+
`workerEmit(payload: any) : unknown`
402+
403+
This function only works inside a worker **and** during a task.
404+
405+
Example:
406+
407+
```js
408+
// file myWorker.js
409+
const workerpool = require('workerpool');
410+
411+
function eventExample(delay) {
412+
workerpool.workerEmit({
413+
status: 'in_progress',
414+
});
415+
416+
workerpool.workerEmit({
417+
status: 'complete',
418+
});
419+
420+
return true;
421+
}
422+
423+
// create a worker and register functions
424+
workerpool.worker({
425+
eventExample: eventExample,
426+
});
427+
```
428+
429+
To receive those events, you can use the `on` option of the pool `exec` method:
430+
431+
```js
432+
pool.exec('eventExample', [], {
433+
on: function (payload) {
434+
if (payload.status === 'in_progress') {
435+
console.log('In progress...');
436+
} else if (payload.status === 'complete') {
437+
console.log('Done!');
438+
}
439+
},
440+
});
441+
```
442+
443+
### Worker API
444+
Workers have access to a `worker` api which contains the following methods
445+
446+
- `emit: (payload: unknown | Transfer): void`
447+
- `addAbortListener: (listener: () => Promise<void>): void`
448+
449+
450+
Worker termination may be recoverable through `abort listeners` which are registered through `worker.addAbortListener`. If all registered listeners resolve then the worker will not be terminated, allowing for worker reuse in some cases.
451+
452+
NOTE: For operations to successfully clean up, a worker implementation should be *async*. If the worker thread is blocked, then the worker will be killed.
397453

398454
```js
399455
function asyncTimeout() {
@@ -402,11 +458,9 @@ function asyncTimeout() {
402458
let timeout = setTimeout(() => {
403459
resolve();
404460
}, 5000);
405-
406-
// An abort listener allows for cleanup for a given worker
407-
// such that it may be resused for future tasks
408-
// if an execption is thrown within scope of the handler
409-
// the worker instance will be destroyed.
461+
462+
// Register a listener which will resolve before the time out
463+
// above triggers.
410464
me.worker.addAbortListener(async function () {
411465
clearTimeout(timeout);
412466
resolve();
@@ -425,25 +479,17 @@ workerpool.worker(
425479
);
426480
```
427481

428-
### Events
429-
430-
You can send data back from workers to the pool while the task is being executed using the `workerEmit` function:
431-
432-
`workerEmit(payload: any) : unknown`
433-
434-
This function only works inside a worker **and** during a task.
435482

436-
Example:
483+
Events may also be emitted from the `worker` api through `worker.emit`
437484

438485
```js
439486
// file myWorker.js
440487
const workerpool = require('workerpool');
441488

442489
function eventExample(delay) {
443-
workerpool.workerEmit({
444-
status: 'in_progress',
490+
this.worker.emit({
491+
status: "in_progress",
445492
});
446-
447493
workerpool.workerEmit({
448494
status: 'complete',
449495
});
@@ -457,20 +503,6 @@ workerpool.worker({
457503
});
458504
```
459505

460-
To receive those events, you can use the `on` option of the pool `exec` method:
461-
462-
```js
463-
pool.exec('eventExample', [], {
464-
on: function (payload) {
465-
if (payload.status === 'in_progress') {
466-
console.log('In progress...');
467-
} else if (payload.status === 'complete') {
468-
console.log('Done!');
469-
}
470-
},
471-
});
472-
```
473-
474506
### Utilities
475507

476508
Following properties are available for convenience:

src/worker.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,18 @@ var worker = {
3535
// works in both node.js and the browser
3636
var publicWorker = {
3737
/**
38-
*
39-
* @param {() => Promise<void>} listener
40-
*/
38+
* Registers listeners which will trigger when a task is timed out or cancled. If all listeners resolve, the worker executing the given task will not be terminated.
39+
* *Note*: If there is a blocking operation within a listener, the worker will be terminated.
40+
* @param {() => Promise<void>} listener
41+
*/
4142
addAbortListener: function(listener) {
4243
worker.abortListeners.push(listener);
4344
},
4445

46+
/**
47+
* Emit an event from the worker thread to the main thread.
48+
* @param {any} payload
49+
*/
4550
emit: worker.emit
4651
};
4752

0 commit comments

Comments
 (0)