X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fworker-node.ts;h=4349ed72471b3614e227ab4d374df7f0aba17186;hb=71b2b6d8c0678320a3b6390c058f0d7338b10918;hp=c2acd65b26290f576a1deb64630ceca5cb580466;hpb=60664f486ce7ff5a437b52c545cf219eca5aebd2;p=poolifier.git diff --git a/src/pools/worker-node.ts b/src/pools/worker-node.ts index c2acd65b..4349ed72 100644 --- a/src/pools/worker-node.ts +++ b/src/pools/worker-node.ts @@ -1,9 +1,11 @@ +import { MessageChannel } from 'node:worker_threads' import { CircularArray } from '../circular-array' import { Queue } from '../queue' +import type { Task } from '../utility-types' +import { DEFAULT_TASK_NAME } from '../utils' import { type IWorker, type IWorkerNode, - type Task, type WorkerInfo, type WorkerType, WorkerTypes, @@ -21,6 +23,7 @@ implements IWorkerNode { public readonly worker: Worker public readonly info: WorkerInfo public usage: WorkerUsage + private readonly tasksUsage: Map private readonly tasksQueue: Queue> /** @@ -28,12 +31,12 @@ implements IWorkerNode { * * @param worker - The worker. * @param workerType - The worker type. - * @internal */ constructor (worker: Worker, workerType: WorkerType) { this.worker = worker this.info = this.initWorkerInfo(worker, workerType) this.usage = this.initWorkerUsage() + this.tasksUsage = new Map() this.tasksQueue = new Queue>() } @@ -43,7 +46,7 @@ implements IWorkerNode { } /** - * Worker node tasks queue maximum size. + * Tasks queue maximum size. * * @returns The tasks queue maximum size. */ @@ -66,8 +69,41 @@ implements IWorkerNode { this.tasksQueue.clear() } + /** @inheritdoc */ public resetUsage (): void { this.usage = this.initWorkerUsage() + this.tasksUsage.clear() + } + + /** @inheritdoc */ + public closeChannel (): void { + if (this.info.messageChannel != null) { + this.info.messageChannel?.port1.unref() + this.info.messageChannel?.port2.unref() + this.info.messageChannel?.port1.close() + this.info.messageChannel?.port2.close() + delete this.info.messageChannel + } + } + + /** @inheritdoc */ + public getTaskWorkerUsage (name: string): WorkerUsage | undefined { + if (name === DEFAULT_TASK_NAME && !Array.isArray(this.info.taskFunctions)) { + throw new Error( + 'Cannot get task worker usage for default task function name when task function names list is not yet defined' + ) + } + if ( + name === DEFAULT_TASK_NAME && + Array.isArray(this.info.taskFunctions) && + this.info.taskFunctions.length > 1 + ) { + name = this.info.taskFunctions[1] + } + if (!this.tasksUsage.has(name)) { + this.tasksUsage.set(name, this.initTaskWorkerUsage(name)) + } + return this.tasksUsage.get(name) } private initWorkerInfo (worker: Worker, workerType: WorkerType): WorkerInfo { @@ -75,7 +111,10 @@ implements IWorkerNode { id: this.getWorkerId(worker, workerType), type: workerType, dynamic: false, - started: true + ready: false, + ...(workerType === WorkerTypes.thread && { + messageChannel: new MessageChannel() + }) } } @@ -115,6 +154,42 @@ implements IWorkerNode { } } + private initTaskWorkerUsage (name: string): WorkerUsage { + const getTaskQueueSize = (): number => { + let taskQueueSize = 0 + for (const task of this.tasksQueue) { + if (task.name === name) { + ++taskQueueSize + } + } + return taskQueueSize + } + return { + tasks: { + executed: 0, + executing: 0, + get queued (): number { + return getTaskQueueSize() + }, + failed: 0 + }, + runTime: { + history: new CircularArray() + }, + waitTime: { + history: new CircularArray() + }, + elu: { + idle: { + history: new CircularArray() + }, + active: { + history: new CircularArray() + } + } + } + } + /** * Gets the worker id. *