X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fabstract-pool.ts;h=ba05d92959a83f362fd721b4ca3615e66c86d196;hb=76aa2b0f39bd376383f2fdc79046328693719590;hp=6fde2491b6e1a565ba48ce26d821407eff67c8a2;hpb=bb4e9c3d36509745767905e4577d000087a23288;p=poolifier.git diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 6fde2491..ba05d929 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -19,7 +19,8 @@ import { type PoolType, PoolTypes, type TasksQueueOptions, - type WorkerType + type WorkerType, + WorkerTypes } from './pool' import type { IWorker, @@ -784,9 +785,13 @@ export abstract class AbstractPool< return message => { if (message.workerId != null && message.started != null) { // Worker started message received - this.workerNodes[ - this.getWorkerNodeKey(this.getWorkerById(message.workerId) as Worker) - ].info.started = message.started + const worker = this.getWorkerById(message.workerId) + if (worker != null) { + this.workerNodes[this.getWorkerNodeKey(worker)].info.started = + message.started + } else { + throw new Error('Worker started message received from unknown worker') + } } else if (message.id != null) { // Task execution response received const promiseResponse = this.promiseResponseMap.get(message.id) @@ -850,7 +855,7 @@ export abstract class AbstractPool< private pushWorkerNode (worker: Worker): number { this.workerNodes.push({ worker, - info: { id: worker.threadId ?? worker.id, started: false }, + info: { id: this.getWorkerId(worker), started: false }, usage: this.getWorkerUsage(), tasksQueue: new Queue>() }) @@ -862,6 +867,20 @@ export abstract class AbstractPool< return this.workerNodes.length } + /** + * Gets the worker id. + * + * @param worker - The worker. + * @returns The worker id. + */ + private getWorkerId (worker: Worker): number | undefined { + if (this.worker === WorkerTypes.thread) { + return worker.threadId + } else if (this.worker === WorkerTypes.cluster) { + return worker.id + } + } + // /** // * Sets the given worker in the pool worker nodes. // *