+ private checkNumberOfWorkers (numberOfWorkers: number): void {
+ if (numberOfWorkers == null) {
+ throw new Error(
+ 'Cannot instantiate a pool without specifying the number of workers'
+ )
+ } else if (!Number.isSafeInteger(numberOfWorkers)) {
+ throw new TypeError(
+ 'Cannot instantiate a pool with a non integer number of workers'
+ )
+ } else if (numberOfWorkers < 0) {
+ throw new RangeError(
+ 'Cannot instantiate a pool with a negative number of workers'
+ )
+ } else if (this.type === PoolType.FIXED && numberOfWorkers === 0) {
+ throw new Error('Cannot instantiate a fixed pool with no worker')
+ }
+ }
+
+ private checkPoolOptions (opts: PoolOptions<Worker>): void {
+ this.opts.workerChoiceStrategy =
+ opts.workerChoiceStrategy ?? WorkerChoiceStrategies.ROUND_ROBIN
+ this.opts.enableEvents = opts.enableEvents ?? true
+ }
+
+ /** {@inheritDoc} */
+ public abstract get type (): PoolType
+
+ /** {@inheritDoc} */
+ public get numberOfRunningTasks (): number {
+ return this.promiseMap.size
+ }
+
+ /**
+ * Gets the given worker key.
+ *
+ * @param worker - The worker.
+ * @returns The worker key.
+ */
+ private getWorkerKey (worker: Worker): number | undefined {
+ return [...this.workers].find(([, value]) => value.worker === worker)?.[0]
+ }
+
+ /** {@inheritDoc} */
+ public getWorkerRunningTasks (worker: Worker): number | undefined {
+ return this.getWorkerTasksUsage(worker)?.running
+ }
+
+ /** {@inheritDoc} */
+ public getWorkerRunTasks (worker: Worker): number | undefined {
+ return this.getWorkerTasksUsage(worker)?.run
+ }
+
+ /** {@inheritDoc} */
+ public getWorkerAverageTasksRunTime (worker: Worker): number | undefined {
+ return this.getWorkerTasksUsage(worker)?.avgRunTime
+ }
+
+ /** {@inheritDoc} */
+ public setWorkerChoiceStrategy (
+ workerChoiceStrategy: WorkerChoiceStrategy
+ ): void {
+ this.opts.workerChoiceStrategy = workerChoiceStrategy
+ for (const [key, value] of this.workers) {
+ this.setWorker(key, value.worker, {
+ run: 0,
+ running: 0,
+ runTime: 0,
+ avgRunTime: 0
+ })
+ }
+ this.workerChoiceStrategyContext.setWorkerChoiceStrategy(
+ workerChoiceStrategy
+ )
+ }
+
+ /** {@inheritDoc} */
+ public abstract get busy (): boolean
+
+ protected internalGetBusyStatus (): boolean {
+ return (
+ this.numberOfRunningTasks >= this.numberOfWorkers &&
+ this.findFreeWorker() === false
+ )
+ }