From: Jérôme Benoit Date: Sun, 9 Apr 2023 12:16:22 +0000 (+0200) Subject: refactor: change tasks queue helpers scope X-Git-Tag: v2.4.5~3 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=416fd65cf4573917610e16e3339b2f1303ce8eda;p=poolifier.git refactor: change tasks queue helpers scope Signed-off-by: Jérôme Benoit --- diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 93516fee..34aa634e 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -244,10 +244,12 @@ export abstract class AbstractPool< let currentTask: Task = submittedTask if ( this.opts.enableTasksQueue === true && - (this.busy || this.tasksQueueLength(workerNodeKey) > 0) + (this.busy || this.tasksQueueSize(workerNodeKey) > 0) ) { - this.enqueueTask(workerNodeKey, submittedTask) - currentTask = this.dequeueTask(workerNodeKey) as Task + currentTask = this.enqueueDequeueTask( + workerNodeKey, + submittedTask + ) as Task } this.sendToWorker(workerNode.worker, currentTask) this.checkAndEmitEvents() @@ -442,7 +444,7 @@ export abstract class AbstractPool< const workerNodeKey = this.getWorkerNodeKey(promiseResponse.worker) if ( this.opts.enableTasksQueue === true && - this.tasksQueueLength(workerNodeKey) > 0 + this.tasksQueueSize(workerNodeKey) > 0 ) { this.sendToWorker( promiseResponse.worker, @@ -542,26 +544,34 @@ export abstract class AbstractPool< * * @param worker - The worker. */ - protected removeWorkerNode (worker: Worker): void { + private removeWorkerNode (worker: Worker): void { const workerNodeKey = this.getWorkerNodeKey(worker) this.workerNodes.splice(workerNodeKey, 1) this.workerChoiceStrategyContext.remove(workerNodeKey) } - protected enqueueTask (workerNodeKey: number, task: Task): void { + private enqueueDequeueTask ( + workerNodeKey: number, + task: Task + ): Task | undefined { + this.enqueueTask(workerNodeKey, task) + return this.dequeueTask(workerNodeKey) + } + + private enqueueTask (workerNodeKey: number, task: Task): void { this.workerNodes[workerNodeKey].tasksQueue.push(task) } - protected dequeueTask (workerNodeKey: number): Task | undefined { + private dequeueTask (workerNodeKey: number): Task | undefined { return this.workerNodes[workerNodeKey].tasksQueue.shift() } - protected tasksQueueLength (workerNodeKey: number): number { + private tasksQueueSize (workerNodeKey: number): number { return this.workerNodes[workerNodeKey].tasksQueue.length } - protected flushTasksQueue (workerNodeKey: number): void { - if (this.tasksQueueLength(workerNodeKey) > 0) { + private flushTasksQueue (workerNodeKey: number): void { + if (this.tasksQueueSize(workerNodeKey) > 0) { for (const task of this.workerNodes[workerNodeKey].tasksQueue) { this.sendToWorker(this.workerNodes[workerNodeKey].worker, task) } @@ -569,7 +579,7 @@ export abstract class AbstractPool< } } - protected flushTasksQueueByWorker (worker: Worker): void { + private flushTasksQueueByWorker (worker: Worker): void { const workerNodeKey = this.getWorkerNodeKey(worker) this.flushTasksQueue(workerNodeKey) }