X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fabstract-pool.ts;h=5efecb58209d3b07e3f1d6cc25cfec0e71fc3281;hb=df422579fde9c2638e5802e94daa4f6eda698b2b;hp=e0aa1810c4d839f39d29eee350ae010a6945ffab;hpb=e088a00c285c320395d883d57d1db51d42300b10;p=poolifier.git diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index e0aa1810..5efecb58 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 } @@ -173,14 +189,14 @@ export abstract class AbstractPool< this, () => { const workerCreated = this.createAndSetupWorker() - this.registerWorkerMessageListener(workerCreated, async message => { + this.registerWorkerMessageListener(workerCreated, message => { const tasksInProgress = this.tasks.get(workerCreated) if ( isKillBehavior(KillBehaviors.HARD, message.kill) || tasksInProgress === 0 ) { // Kill received from the worker, means that no new tasks are submitted to that worker for a while ( > maxInactiveTime) - await this.destroyWorker(workerCreated) + this.destroyWorker(workerCreated) as void } }) return workerCreated @@ -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)