X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fworker-node.ts;h=5d28802c12769c1cbaf0cd519e22708918788016;hb=f30cddb8f07ac919d75140469faab6e0883709da;hp=308215176edb515851c2349ba550d3cc50e2611b;hpb=3ffc32ec66a9c48f39a1b976ebd88cea53824679;p=poolifier.git diff --git a/src/pools/worker-node.ts b/src/pools/worker-node.ts index 30821517..5d28802c 100644 --- a/src/pools/worker-node.ts +++ b/src/pools/worker-node.ts @@ -1,9 +1,16 @@ -import { MessageChannel } from 'node:worker_threads' import { EventEmitter } from 'node:events' +import { MessageChannel } from 'node:worker_threads' + import { CircularArray } from '../circular-array.js' +import { PriorityQueue } from '../priority-queue.js' import type { Task } from '../utility-types.js' import { DEFAULT_TASK_NAME } from '../utils.js' -import { Deque } from '../deque.js' +import { + checkWorkerNodeArguments, + createWorker, + getWorkerId, + getWorkerType +} from './utils.js' import { type EventHandler, type IWorker, @@ -15,12 +22,6 @@ import { WorkerTypes, type WorkerUsage } from './worker.js' -import { - checkWorkerNodeArguments, - createWorker, - getWorkerId, - getWorkerType -} from './utils.js' /** * Worker node. @@ -43,7 +44,7 @@ export class WorkerNode public messageChannel?: MessageChannel /** @inheritdoc */ public tasksQueueBackPressureSize: number - private readonly tasksQueue: Deque> + private readonly tasksQueue: PriorityQueue> private onBackPressureStarted: boolean private readonly taskFunctionsUsage: Map @@ -68,7 +69,7 @@ export class WorkerNode } // eslint-disable-next-line @typescript-eslint/no-non-null-assertion this.tasksQueueBackPressureSize = opts.tasksQueueBackPressureSize! - this.tasksQueue = new Deque>() + this.tasksQueue = new PriorityQueue>(opts.tasksQueueBucketSize) this.onBackPressureStarted = false this.taskFunctionsUsage = new Map() } @@ -80,18 +81,7 @@ export class WorkerNode /** @inheritdoc */ public enqueueTask (task: Task): number { - const tasksQueueSize = this.tasksQueue.push(task) - if (this.hasBackPressure() && !this.onBackPressureStarted) { - this.onBackPressureStarted = true - this.emit('backPressure', { workerId: this.info.id }) - this.onBackPressureStarted = false - } - return tasksQueueSize - } - - /** @inheritdoc */ - public unshiftTask (task: Task): number { - const tasksQueueSize = this.tasksQueue.unshift(task) + const tasksQueueSize = this.tasksQueue.enqueue(task, task.priority) if (this.hasBackPressure() && !this.onBackPressureStarted) { this.onBackPressureStarted = true this.emit('backPressure', { workerId: this.info.id }) @@ -101,13 +91,14 @@ export class WorkerNode } /** @inheritdoc */ - public dequeueTask (): Task | undefined { - return this.tasksQueue.shift() + public dequeueTask (bucket?: number): Task | undefined { + return this.tasksQueue.dequeue(bucket) } /** @inheritdoc */ - public popTask (): Task | undefined { - return this.tasksQueue.pop() + public dequeueLastBucketTask (): Task | undefined { + // Start from the last empty or partially filled bucket + return this.tasksQueue.dequeue(this.tasksQueue.buckets + 1) } /** @inheritdoc */ @@ -120,12 +111,6 @@ export class WorkerNode return this.tasksQueue.size >= this.tasksQueueBackPressureSize } - /** @inheritdoc */ - public resetUsage (): void { - this.usage = this.initWorkerUsage() - this.taskFunctionsUsage.clear() - } - /** @inheritdoc */ public async terminate (): Promise { const waitWorkerExit = new Promise(resolve => { @@ -137,6 +122,7 @@ export class WorkerNode this.removeAllListeners() switch (this.info.type) { case WorkerTypes.thread: + this.worker.unref?.() await this.worker.terminate?.() break case WorkerTypes.cluster: @@ -167,21 +153,21 @@ export class WorkerNode /** @inheritdoc */ public getTaskFunctionWorkerUsage (name: string): WorkerUsage | undefined { - if (!Array.isArray(this.info.taskFunctionNames)) { + if (!Array.isArray(this.info.taskFunctionsProperties)) { throw new Error( - `Cannot get task function worker usage for task function name '${name}' when task function names list is not yet defined` + `Cannot get task function worker usage for task function name '${name}' when task function properties list is not yet defined` ) } if ( - Array.isArray(this.info.taskFunctionNames) && - this.info.taskFunctionNames.length < 3 + Array.isArray(this.info.taskFunctionsProperties) && + this.info.taskFunctionsProperties.length < 3 ) { throw new Error( - `Cannot get task function worker usage for task function name '${name}' when task function names list has less than 3 elements` + `Cannot get task function worker usage for task function name '${name}' when task function properties list has less than 3 elements` ) } if (name === DEFAULT_TASK_NAME) { - name = this.info.taskFunctionNames[1] + name = this.info.taskFunctionsProperties[1].name } if (!this.taskFunctionsUsage.has(name)) { this.taskFunctionsUsage.set(name, this.initTaskFunctionWorkerUsage(name)) @@ -260,7 +246,7 @@ export class WorkerNode if ( (task.name === DEFAULT_TASK_NAME && // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - name === this.info.taskFunctionNames![1]) || + name === this.info.taskFunctionsProperties![1].name) || (task.name !== DEFAULT_TASK_NAME && name === task.name) ) { ++taskFunctionQueueSize