X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fpools%2Fabstract-pool.ts;h=71046d494db7770ade3339d46edcde5d27291efe;hb=9c8526b801b42a5b40bfa5cec6f6e2082ffc9da7;hp=ff7413dd52f2f24e32b7359ce0f14af6180cca67;hpb=7c0ba92006a5c188738ffc5ff642c51f172df3d6;p=poolifier.git diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index ff7413dd..71046d49 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -2,6 +2,7 @@ import type { MessageValue, PromiseWorkerResponseWrapper } from '../utility-types' +import { EMPTY_FUNCTION } from '../utils' import { isKillBehavior, KillBehaviors } from '../worker/worker-options' import type { IPoolInternal } from './pool-internal' import { PoolEmitter, PoolType } from './pool-internal' @@ -12,11 +13,9 @@ import { } from './selection-strategies' /** - * An intentional empty function. + * Callback invoked if the worker has received a message. */ -const EMPTY_FUNCTION: () => void = () => { - /* Intentionally empty */ -} +export type MessageHandler = (this: Worker, m: unknown) => void /** * Callback invoked if the worker raised an error. @@ -37,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. * @@ -71,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. */ @@ -90,7 +100,7 @@ export interface PoolOptions { /** * Pool events emission. * - * Default to true. + * @default true */ enableEvents?: boolean } @@ -122,7 +132,7 @@ export abstract class AbstractPool< /** * The promise map. * - * - `key`: This is the message ID of each submitted task. + * - `key`: This is the message Id of each submitted task. * - `value`: An object that contains the worker, the resolve function and the reject function. * * When we receive a message from the worker we get a map entry and resolve/reject the promise based on the message. @@ -133,7 +143,7 @@ export abstract class AbstractPool< > = new Map>() /** - * ID of the next message. + * Id of the next message. */ protected nextMessageId: number = 0 @@ -180,18 +190,17 @@ export abstract class AbstractPool< () => { const workerCreated = this.createAndSetupWorker() this.registerWorkerMessageListener(workerCreated, message => { - const tasksInProgress = this.tasks.get(workerCreated) if ( isKillBehavior(KillBehaviors.HARD, message.kill) || - tasksInProgress === 0 + this.tasks.get(workerCreated) === 0 ) { // Kill received from the worker, means that no new tasks are submitted to that worker for a while ( > maxInactiveTime) - void this.destroyWorker(workerCreated) + this.destroyWorker(workerCreated) as void } }) return workerCreated }, - opts.workerChoiceStrategy ?? WorkerChoiceStrategies.ROUND_ROBIN + this.opts.workerChoiceStrategy ) } @@ -220,6 +229,8 @@ export abstract class AbstractPool< } private checkPoolOptions (opts: PoolOptions): void { + this.opts.workerChoiceStrategy = + opts.workerChoiceStrategy ?? WorkerChoiceStrategies.ROUND_ROBIN this.opts.enableEvents = opts.enableEvents ?? true } @@ -266,11 +277,11 @@ export abstract class AbstractPool< public execute (data: Data): Promise { // Configure worker to handle message with the specified task const worker = this.chooseWorker() - this.increaseWorkersTask(worker) - this.checkAndEmitBusy() const messageId = ++this.nextMessageId const res = this.internalExecute(worker, messageId) - this.sendToWorker(worker, { data: data || ({} as Data), id: messageId }) + this.checkAndEmitBusy() + data = data ?? ({} as Data) + this.sendToWorker(worker, { data, id: messageId }) return res } @@ -300,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. */ @@ -309,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. */ @@ -318,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. @@ -380,6 +391,7 @@ export abstract class AbstractPool< worker: Worker, messageId: number ): Promise { + this.increaseWorkersTask(worker) return new Promise((resolve, reject) => { this.promiseMap.set(messageId, { resolve, reject, worker }) }) @@ -405,8 +417,9 @@ export abstract class AbstractPool< * @returns New, completely set up worker. */ protected createAndSetupWorker (): Worker { - const worker: Worker = this.createWorker() + const 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) @@ -425,16 +438,16 @@ export abstract class AbstractPool< /** * This function is the listener registered for each worker. * - * @returns The listener function to execute when a message is sent from a worker. + * @returns The listener function to execute when a message is received from a worker. */ protected workerListener (): (message: MessageValue) => void { return message => { - if (message.id) { - const value = this.promiseMap.get(message.id) - if (value) { - this.decreaseWorkersTasks(value.worker) - if (message.error) value.reject(message.error) - else value.resolve(message.data as Response) + if (message.id !== undefined) { + const promise = this.promiseMap.get(message.id) + if (promise !== undefined) { + this.decreaseWorkersTasks(promise.worker) + if (message.error) promise.reject(message.error) + else promise.resolve(message.data as Response) this.promiseMap.delete(message.id) } }