X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fabstract-pool.ts;h=6d71e87e34378abe5c75ad1c1806eacaad979a3b;hb=2115798751b31f025acae1ff8c4f5cbcec8ad3af;hp=4c08671e5d99ab362f4b0263a423cfe42b4af5e5;hpb=f58b60b9e2433207e84a464b71aea4f17eee34ba;p=poolifier.git diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 4c08671e..6d71e87e 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -510,7 +510,9 @@ export abstract class AbstractPool< * @throws {@link https://nodejs.org/api/errors.html#class-error} If the worker id is invalid. */ private checkMessageWorkerId (message: MessageValue): void { - if ( + if (message.workerId == null) { + throw new Error('Worker message received without worker id') + } else if ( message.workerId != null && this.getWorkerNodeKeyByWorkerId(message.workerId) === -1 ) { @@ -672,7 +674,11 @@ export abstract class AbstractPool< ) { reject(new TypeError('name argument must not be an empty string')) } - if (name != null && !this.taskFunctions.includes(name)) { + if ( + name != null && + this.taskFunctions != null && + !this.taskFunctions.includes(name) + ) { reject( new Error(`Task function '${name}' is not registered in the pool`) ) @@ -717,6 +723,7 @@ export abstract class AbstractPool< await this.destroyWorkerNode(workerNodeKey) }) ) + this.emitter?.emit(PoolEvents.destroy) } protected async sendKillMessageToWorker ( @@ -928,6 +935,7 @@ export abstract class AbstractPool< protected createAndSetupWorkerNode (): number { const worker = this.createWorker() + worker.on('online', this.opts.onlineHandler ?? EMPTY_FUNCTION) worker.on('message', this.opts.messageHandler ?? EMPTY_FUNCTION) worker.on('error', this.opts.errorHandler ?? EMPTY_FUNCTION) worker.on('error', (error) => { @@ -947,7 +955,6 @@ export abstract class AbstractPool< this.redistributeQueuedTasks(workerNodeKey) } }) - worker.on('online', this.opts.onlineHandler ?? EMPTY_FUNCTION) worker.on('exit', this.opts.exitHandler ?? EMPTY_FUNCTION) worker.once('exit', () => { this.removeWorkerNode(worker) @@ -1118,6 +1125,9 @@ export abstract class AbstractPool< } private handleWorkerReadyResponse (message: MessageValue): void { + if (message.ready === false) { + throw new Error(`Worker ${message.workerId} failed to initialize`) + } this.getWorkerInfo( this.getWorkerNodeKeyByWorkerId(message.workerId) ).ready = message.ready as boolean