From 35cf1c03f8379b58e410cad57189912379ee7b36 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 29 Aug 2021 10:52:45 +0200 Subject: [PATCH] Add a pool option to register a message listener on pool workers (#487) MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * Add a pool option to register a message listener on pool workers It's a requirement to build bi-directionnal communications with the main thread with poolifier. Signed-off-by: Jérôme Benoit * Loosen type on MessageHandler type. Signed-off-by: Jérôme Benoit --- CHANGELOG.md | 6 +++++- src/pools/abstract-pool.ts | 25 ++++++++++++++++++---- src/pools/cluster/dynamic.ts | 2 +- src/pools/cluster/fixed.ts | 2 +- src/pools/thread/dynamic.ts | 2 +- src/pools/thread/fixed.ts | 2 +- tests/pools/abstract/abstract-pool.test.js | 15 ++++++++++++- 7 files changed, 44 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d75ac56..c379d795 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [2.1.0] - 2021-dd-mm +## [2.1.0] - 2021-28-08 + +### Added + +- Add an optional pool option `messageHandler` to `PoolOptions` for registering a message handler callback on each worker. ### Breaking Changes diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index e0aa1810..f3a8c1c6 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -12,6 +12,11 @@ import { WorkerChoiceStrategyContext } from './selection-strategies' +/** + * Callback invoked if the worker has received a message. + */ +export type MessageHandler = (this: Worker, m: unknown) => void + /** * Callback invoked if the worker raised an error. */ @@ -31,6 +36,13 @@ export type ExitHandler = (this: Worker, code: number) => void * Basic interface that describes the minimum required implementation of listener events for a pool-worker. */ export interface IWorker { + /** + * Register a listener to the message event. + * + * @param event `'message'`. + * @param handler The message handler. + */ + on(event: 'message', handler: MessageHandler): void /** * Register a listener to the error event. * @@ -65,6 +77,10 @@ export interface IWorker { * Options for a poolifier pool. */ export interface PoolOptions { + /** + * A function that will listen for message event on each worker. + */ + messageHandler?: MessageHandler /** * A function that will listen for error event on each worker. */ @@ -84,7 +100,7 @@ export interface PoolOptions { /** * Pool events emission. * - * Default to true. + * @default true */ enableEvents?: boolean } @@ -295,7 +311,7 @@ export abstract class AbstractPool< protected abstract isMain (): boolean /** - * Increase the number of tasks that the given workers has applied. + * Increase the number of tasks that the given worker has applied. * * @param worker Worker whose tasks are increased. */ @@ -304,7 +320,7 @@ export abstract class AbstractPool< } /** - * Decrease the number of tasks that the given workers has applied. + * Decrease the number of tasks that the given worker has applied. * * @param worker Worker whose tasks are decreased. */ @@ -313,7 +329,7 @@ export abstract class AbstractPool< } /** - * Step the number of tasks that the given workers has applied. + * Step the number of tasks that the given worker has applied. * * @param worker Worker whose tasks are set. * @param step Worker number of tasks step. @@ -403,6 +419,7 @@ export abstract class AbstractPool< protected createAndSetupWorker (): Worker { const worker: Worker = this.createWorker() + worker.on('message', this.opts.messageHandler ?? EMPTY_FUNCTION) worker.on('error', this.opts.errorHandler ?? EMPTY_FUNCTION) worker.on('online', this.opts.onlineHandler ?? EMPTY_FUNCTION) worker.on('exit', this.opts.exitHandler ?? EMPTY_FUNCTION) diff --git a/src/pools/cluster/dynamic.ts b/src/pools/cluster/dynamic.ts index 33c05a92..ec177da6 100644 --- a/src/pools/cluster/dynamic.ts +++ b/src/pools/cluster/dynamic.ts @@ -23,7 +23,7 @@ export class DynamicClusterPool< * @param min Minimum number of workers which are always active. * @param max Maximum number of workers that can be created by this pool. * @param filePath Path to an implementation of a `ClusterWorker` file, which can be relative or absolute. - * @param opts Options for this dynamic cluster pool. Default: `{}` + * @param [opts={}] Options for this dynamic cluster pool. */ public constructor ( min: number, diff --git a/src/pools/cluster/fixed.ts b/src/pools/cluster/fixed.ts index ccffc6bb..95ea82b9 100644 --- a/src/pools/cluster/fixed.ts +++ b/src/pools/cluster/fixed.ts @@ -38,7 +38,7 @@ export class FixedClusterPool< * * @param numberOfWorkers Number of workers for this pool. * @param filePath Path to an implementation of a `ClusterWorker` file, which can be relative or absolute. - * @param opts Options for this fixed cluster pool. Default: `{}` + * @param [opts={}] Options for this fixed cluster pool. */ public constructor ( numberOfWorkers: number, diff --git a/src/pools/thread/dynamic.ts b/src/pools/thread/dynamic.ts index 932a0cd5..361c615c 100644 --- a/src/pools/thread/dynamic.ts +++ b/src/pools/thread/dynamic.ts @@ -24,7 +24,7 @@ export class DynamicThreadPool< * @param min Minimum number of threads which are always active. * @param max Maximum number of threads that can be created by this pool. * @param filePath Path to an implementation of a `ThreadWorker` file, which can be relative or absolute. - * @param opts Options for this dynamic thread pool. Default: `{}` + * @param [opts={}] Options for this dynamic thread pool. */ public constructor ( min: number, diff --git a/src/pools/thread/fixed.ts b/src/pools/thread/fixed.ts index a1302340..e3f9602c 100644 --- a/src/pools/thread/fixed.ts +++ b/src/pools/thread/fixed.ts @@ -30,7 +30,7 @@ export class FixedThreadPool< * * @param numberOfThreads Number of threads for this pool. * @param filePath Path to an implementation of a `ThreadWorker` file, which can be relative or absolute. - * @param opts Options for this fixed thread pool. Default: `{}` + * @param [opts={}] Options for this fixed thread pool. */ public constructor ( numberOfThreads: number, diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index df36568c..b6846cc4 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract/abstract-pool.test.js @@ -109,13 +109,22 @@ describe('Abstract pool test suite', () => { expect(pool.opts.workerChoiceStrategy).toBe( WorkerChoiceStrategies.ROUND_ROBIN ) + expect(pool.opts.messageHandler).toBeUndefined() + expect(pool.opts.errorHandler).toBeUndefined() + expect(pool.opts.onlineHandler).toBeUndefined() + expect(pool.opts.exitHandler).toBeUndefined() pool.destroy() + const testHandler = () => console.log('test handler executed') pool = new FixedThreadPool( numberOfWorkers, './tests/worker-files/thread/testWorker.js', { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED, - enableEvents: false + enableEvents: false, + messageHandler: testHandler, + errorHandler: testHandler, + onlineHandler: testHandler, + exitHandler: testHandler } ) expect(pool.opts.enableEvents).toBe(false) @@ -123,6 +132,10 @@ describe('Abstract pool test suite', () => { expect(pool.opts.workerChoiceStrategy).toBe( WorkerChoiceStrategies.LESS_RECENTLY_USED ) + expect(pool.opts.messageHandler).toStrictEqual(testHandler) + expect(pool.opts.errorHandler).toStrictEqual(testHandler) + expect(pool.opts.onlineHandler).toStrictEqual(testHandler) + expect(pool.opts.exitHandler).toStrictEqual(testHandler) pool.destroy() }) -- 2.34.1