X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fabstract-pool.ts;h=2ee3a0ccb968100e23a5deecfe2eb4e049171875;hb=80b875e3940ad36bd494fdadea97a070c46251be;hp=ffbda872af76d22fd7d037b39dd54025568e39c5;hpb=ef41a6e6f04e66c9732334e673758b2f4e4b0730;p=poolifier.git diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index ffbda872..2ee3a0cc 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -6,14 +6,15 @@ import { median } from '../utils' import { KillBehaviors, isKillBehavior } from '../worker/worker-options' +import { CircularArray } from '../circular-array' import { - PoolEvents, type IPool, + PoolEmitter, + PoolEvents, type PoolOptions, - type TasksQueueOptions, - PoolType + PoolType, + type TasksQueueOptions } from './pool' -import { PoolEmitter } from './pool' import type { IWorker, Task, TasksUsage, WorkerNode } from './worker' import { WorkerChoiceStrategies, @@ -21,7 +22,6 @@ import { type WorkerChoiceStrategyOptions } from './selection-strategies/selection-strategies-types' import { WorkerChoiceStrategyContext } from './selection-strategies/worker-choice-strategy-context' -import { CircularArray } from '../circular-array' /** * Base class that implements some shared logic for all poolifier pools. @@ -84,10 +84,10 @@ export abstract class AbstractPool< this.checkFilePath(this.filePath) this.checkPoolOptions(this.opts) - this.chooseWorkerNode.bind(this) - this.executeTask.bind(this) - this.enqueueTask.bind(this) - this.checkAndEmitEvents.bind(this) + this.chooseWorkerNode = this.chooseWorkerNode.bind(this) + this.executeTask = this.executeTask.bind(this) + this.enqueueTask = this.enqueueTask.bind(this) + this.checkAndEmitEvents = this.checkAndEmitEvents.bind(this) this.setupHook() @@ -296,14 +296,11 @@ export abstract class AbstractPool< protected abstract get busy (): boolean protected internalBusy (): boolean { - return this.findFreeWorkerNodeKey() === -1 - } - - /** @inheritDoc */ - public findFreeWorkerNodeKey (): number { - return this.workerNodes.findIndex(workerNode => { - return workerNode.tasksUsage?.running === 0 - }) + return ( + this.workerNodes.findIndex(workerNode => { + return workerNode.tasksUsage?.running === 0 + }) === -1 + ) } /** @inheritDoc */ @@ -390,7 +387,7 @@ export abstract class AbstractPool< worker: Worker, message: MessageValue ): void { - const workerTasksUsage = this.getWorkerTasksUsage(worker) as TasksUsage + const workerTasksUsage = this.getWorkerTasksUsage(worker) --workerTasksUsage.running ++workerTasksUsage.run if (message.error != null) { @@ -431,7 +428,7 @@ export abstract class AbstractPool< ) { // Kill message received from the worker: no new tasks are submitted to that worker for a while ( > maxInactiveTime) this.flushTasksQueueByWorker(workerCreated) - void this.destroyWorker(workerCreated) + void (this.destroyWorker(workerCreated) as Promise) } }) workerNodeKey = this.getWorkerNodeKey(workerCreated) @@ -563,7 +560,7 @@ export abstract class AbstractPool< * @throws Error if the worker is not found in the pool worker nodes. * @returns The worker tasks usage. */ - private getWorkerTasksUsage (worker: Worker): TasksUsage | undefined { + private getWorkerTasksUsage (worker: Worker): TasksUsage { const workerNodeKey = this.getWorkerNodeKey(worker) if (workerNodeKey !== -1) { return this.workerNodes[workerNodeKey].tasksUsage