X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fworker-node.ts;h=de65f27012634c493d697a0ae9e3972a7f5234db;hb=5aa31a743fde300612ae89a242b809ae7db083ed;hp=f3ec3be3d83f4541da3713cbd46406d19be41ac0;hpb=6e9c39d3eedabc9f12808490983d36e551a75a28;p=poolifier.git diff --git a/src/pools/worker-node.ts b/src/pools/worker-node.ts index f3ec3be3..de65f270 100644 --- a/src/pools/worker-node.ts +++ b/src/pools/worker-node.ts @@ -5,20 +5,22 @@ import { DEFAULT_TASK_NAME, EMPTY_FUNCTION, exponentialDelay, + getWorkerId, + getWorkerType, sleep } from '../utils' import { Deque } from '../deque' import { type IWorker, type IWorkerNode, + type StrategyData, type WorkerInfo, + type WorkerNodeEventDetail, type WorkerType, WorkerTypes, type WorkerUsage } from './worker' - -type EmptyQueueCallback = (workerId: number) => void -type BackPressureCallback = EmptyQueueCallback +import { checkWorkerNodeArguments } from './utils' /** * Worker node. @@ -27,7 +29,8 @@ type BackPressureCallback = EmptyQueueCallback * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. */ export class WorkerNode -implements IWorkerNode { + extends EventTarget + implements IWorkerNode { /** @inheritdoc */ public readonly worker: Worker /** @inheritdoc */ @@ -35,14 +38,13 @@ implements IWorkerNode { /** @inheritdoc */ public usage: WorkerUsage /** @inheritdoc */ + public strategyData?: StrategyData + /** @inheritdoc */ public messageChannel?: MessageChannel /** @inheritdoc */ public tasksQueueBackPressureSize: number - /** @inheritdoc */ - public onBackPressure?: BackPressureCallback - /** @inheritdoc */ - public onEmptyQueue?: EmptyQueueCallback private readonly tasksQueue: Deque> + private onBackPressureStarted: boolean private onEmptyQueueCount: number private readonly taskFunctionsUsage: Map @@ -50,40 +52,20 @@ implements IWorkerNode { * Constructs a new worker node. * * @param worker - The worker. - * @param workerType - The worker type. * @param tasksQueueBackPressureSize - The tasks queue back pressure size. */ - constructor ( - worker: Worker, - workerType: WorkerType, - tasksQueueBackPressureSize: number - ) { - if (worker == null) { - throw new TypeError('Cannot construct a worker node without a worker') - } - if (workerType == null) { - throw new TypeError( - 'Cannot construct a worker node without a worker type' - ) - } - if (tasksQueueBackPressureSize == null) { - throw new TypeError( - 'Cannot construct a worker node without a tasks queue back pressure size' - ) - } - if (!Number.isSafeInteger(tasksQueueBackPressureSize)) { - throw new TypeError( - 'Cannot construct a worker node with a tasks queue back pressure size that is not an integer' - ) - } + constructor (worker: Worker, tasksQueueBackPressureSize: number) { + super() + checkWorkerNodeArguments(worker, tasksQueueBackPressureSize) this.worker = worker - this.info = this.initWorkerInfo(worker, workerType) + this.info = this.initWorkerInfo(worker) this.usage = this.initWorkerUsage() - if (workerType === WorkerTypes.thread) { + if (this.info.type === WorkerTypes.thread) { this.messageChannel = new MessageChannel() } this.tasksQueueBackPressureSize = tasksQueueBackPressureSize this.tasksQueue = new Deque>() + this.onBackPressureStarted = false this.onEmptyQueueCount = 0 this.taskFunctionsUsage = new Map() } @@ -96,8 +78,14 @@ implements IWorkerNode { /** @inheritdoc */ public enqueueTask (task: Task): number { const tasksQueueSize = this.tasksQueue.push(task) - if (this.onBackPressure != null && this.hasBackPressure()) { - this.onBackPressure(this.info.id as number) + if (this.hasBackPressure() && !this.onBackPressureStarted) { + this.onBackPressureStarted = true + this.dispatchEvent( + new CustomEvent('backpressure', { + detail: { workerId: this.info.id as number } + }) + ) + this.onBackPressureStarted = false } return tasksQueueSize } @@ -105,8 +93,14 @@ implements IWorkerNode { /** @inheritdoc */ public unshiftTask (task: Task): number { const tasksQueueSize = this.tasksQueue.unshift(task) - if (this.onBackPressure != null && this.hasBackPressure()) { - this.onBackPressure(this.info.id as number) + if (this.hasBackPressure() && !this.onBackPressureStarted) { + this.onBackPressureStarted = true + this.dispatchEvent( + new CustomEvent('backpressure', { + detail: { workerId: this.info.id as number } + }) + ) + this.onBackPressureStarted = false } return tasksQueueSize } @@ -114,7 +108,7 @@ implements IWorkerNode { /** @inheritdoc */ public dequeueTask (): Task | undefined { const task = this.tasksQueue.shift() - if (this.onEmptyQueue != null && this.tasksQueue.size === 0) { + if (this.tasksQueue.size === 0 && this.onEmptyQueueCount === 0) { this.startOnEmptyQueue().catch(EMPTY_FUNCTION) } return task @@ -123,7 +117,7 @@ implements IWorkerNode { /** @inheritdoc */ public popTask (): Task | undefined { const task = this.tasksQueue.pop() - if (this.onEmptyQueue != null && this.tasksQueue.size === 0) { + if (this.tasksQueue.size === 0 && this.onEmptyQueueCount === 0) { this.startOnEmptyQueue().catch(EMPTY_FUNCTION) } return task @@ -148,31 +142,31 @@ implements IWorkerNode { /** @inheritdoc */ public closeChannel (): void { if (this.messageChannel != null) { - this.messageChannel?.port1.unref() - this.messageChannel?.port2.unref() - this.messageChannel?.port1.close() - this.messageChannel?.port2.close() + this.messageChannel.port1.unref() + this.messageChannel.port2.unref() + this.messageChannel.port1.close() + this.messageChannel.port2.close() delete this.messageChannel } } /** @inheritdoc */ public getTaskFunctionWorkerUsage (name: string): WorkerUsage | undefined { - if (!Array.isArray(this.info.taskFunctions)) { + if (!Array.isArray(this.info.taskFunctionNames)) { throw new Error( `Cannot get task function worker usage for task function name '${name}' when task function names list is not yet defined` ) } if ( - Array.isArray(this.info.taskFunctions) && - this.info.taskFunctions.length < 3 + Array.isArray(this.info.taskFunctionNames) && + this.info.taskFunctionNames.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` ) } if (name === DEFAULT_TASK_NAME) { - name = this.info.taskFunctions[1] + name = this.info.taskFunctionNames[1] } if (!this.taskFunctionsUsage.has(name)) { this.taskFunctionsUsage.set(name, this.initTaskFunctionWorkerUsage(name)) @@ -180,6 +174,11 @@ implements IWorkerNode { return this.taskFunctionsUsage.get(name) } + /** @inheritdoc */ + public deleteTaskFunctionWorkerUsage (name: string): boolean { + return this.taskFunctionsUsage.delete(name) + } + private async startOnEmptyQueue (): Promise { if ( this.onEmptyQueueCount > 0 && @@ -188,16 +187,20 @@ implements IWorkerNode { this.onEmptyQueueCount = 0 return } - (this.onEmptyQueue as EmptyQueueCallback)(this.info.id as number) ++this.onEmptyQueueCount + this.dispatchEvent( + new CustomEvent('emptyqueue', { + detail: { workerId: this.info.id as number } + }) + ) await sleep(exponentialDelay(this.onEmptyQueueCount)) await this.startOnEmptyQueue() } - private initWorkerInfo (worker: Worker, workerType: WorkerType): WorkerInfo { + private initWorkerInfo (worker: Worker): WorkerInfo { return { - id: this.getWorkerId(worker, workerType), - type: workerType, + id: getWorkerId(worker), + type: getWorkerType(worker) as WorkerType, dynamic: false, ready: false } @@ -224,17 +227,17 @@ implements IWorkerNode { failed: 0 }, runTime: { - history: new CircularArray() + history: new CircularArray() }, waitTime: { - history: new CircularArray() + history: new CircularArray() }, elu: { idle: { - history: new CircularArray() + history: new CircularArray() }, active: { - history: new CircularArray() + history: new CircularArray() } } } @@ -246,7 +249,7 @@ implements IWorkerNode { for (const task of this.tasksQueue) { if ( (task.name === DEFAULT_TASK_NAME && - name === (this.info.taskFunctions as string[])[1]) || + name === (this.info.taskFunctionNames as string[])[1]) || (task.name !== DEFAULT_TASK_NAME && name === task.name) ) { ++taskFunctionQueueSize @@ -265,37 +268,19 @@ implements IWorkerNode { failed: 0 }, runTime: { - history: new CircularArray() + history: new CircularArray() }, waitTime: { - history: new CircularArray() + history: new CircularArray() }, elu: { idle: { - history: new CircularArray() + history: new CircularArray() }, active: { - history: new CircularArray() + history: new CircularArray() } } } } - - /** - * Gets the worker id. - * - * @param worker - The worker. - * @param workerType - The worker type. - * @returns The worker id. - */ - private getWorkerId ( - worker: Worker, - workerType: WorkerType - ): number | undefined { - if (workerType === WorkerTypes.thread) { - return worker.threadId - } else if (workerType === WorkerTypes.cluster) { - return worker.id - } - } }