X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fabstract-pool.ts;h=473789809d2dde4ea57571dd1a2ded5dd7499477;hb=0e7c56b0e6ec34e134f703c5f39047d78890a57d;hp=3bc00a6fffd36c987dd015fdeecd6b62f1e6a662;hpb=8f52842f4b4fa9059f25fc4a90e8bbe8db2f5e63;p=poolifier.git diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 3bc00a6f..47378980 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -7,13 +7,13 @@ import { } from '../utils' import { KillBehaviors, isKillBehavior } from '../worker/worker-options' import { + PoolEmitter, PoolEvents, type IPool, type PoolOptions, type TasksQueueOptions, PoolType } from './pool' -import { PoolEmitter } from './pool' import type { IWorker, Task, TasksUsage, WorkerNode } from './worker' import { WorkerChoiceStrategies, @@ -69,7 +69,7 @@ export abstract class AbstractPool< * Constructs a new poolifier pool. * * @param numberOfWorkers - Number of workers that this pool should manage. - * @param filePath - Path to the worker-file. + * @param filePath - Path to the worker file. * @param opts - Options for the pool. */ public constructor ( @@ -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() @@ -256,9 +256,7 @@ export abstract class AbstractPool< tasksQueueOptions?: TasksQueueOptions ): void { if (this.opts.enableTasksQueue === true && !enable) { - for (const [workerNodeKey] of this.workerNodes.entries()) { - this.flushTasksQueue(workerNodeKey) - } + this.flushTasksQueues() } this.opts.enableTasksQueue = enable this.setTasksQueueOptions(tasksQueueOptions as TasksQueueOptions) @@ -298,18 +296,15 @@ 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 */ - public async execute (data: Data): Promise { + public async execute (data?: Data): Promise { const [workerNodeKey, workerNode] = this.chooseWorkerNode() const submittedTask: Task = { // eslint-disable-next-line @typescript-eslint/consistent-type-assertions @@ -433,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) @@ -656,4 +651,10 @@ export abstract class AbstractPool< const workerNodeKey = this.getWorkerNodeKey(worker) this.flushTasksQueue(workerNodeKey) } + + private flushTasksQueues (): void { + for (const [workerNodeKey] of this.workerNodes.entries()) { + this.flushTasksQueue(workerNodeKey) + } + } }