1 import { MessageChannel
} from
'node:worker_threads'
2 import { CircularArray
} from
'../circular-array'
3 import { Queue
} from
'../queue'
4 import type { Task
} from
'../utility-types'
17 * @typeParam Worker - Type of worker.
18 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
20 export class WorkerNode
<Worker
extends IWorker
, Data
= unknown
>
21 implements IWorkerNode
<Worker
, Data
> {
22 public readonly worker
: Worker
23 public readonly info
: WorkerInfo
24 public usage
: WorkerUsage
25 private readonly tasksUsage
: Map
<string, WorkerUsage
>
26 private readonly tasksQueue
: Queue
<Task
<Data
>>
29 * Constructs a new worker node.
31 * @param worker - The worker.
32 * @param workerType - The worker type.
34 constructor (worker
: Worker
, workerType
: WorkerType
) {
36 this.info
= this.initWorkerInfo(worker
, workerType
)
37 this.usage
= this.initWorkerUsage()
38 this.tasksUsage
= new Map
<string, WorkerUsage
>()
39 this.tasksQueue
= new Queue
<Task
<Data
>>()
43 public tasksQueueSize (): number {
44 return this.tasksQueue
.size
48 * Tasks queue maximum size.
50 * @returns The tasks queue maximum size.
52 private tasksQueueMaxSize (): number {
53 return this.tasksQueue
.maxSize
57 public enqueueTask (task
: Task
<Data
>): number {
58 return this.tasksQueue
.enqueue(task
)
62 public dequeueTask (): Task
<Data
> | undefined {
63 return this.tasksQueue
.dequeue()
67 public clearTasksQueue (): void {
68 this.tasksQueue
.clear()
72 public resetUsage (): void {
73 this.usage
= this.initWorkerUsage()
74 this.tasksUsage
.clear()
78 public getTaskWorkerUsage (name
: string): WorkerUsage
| undefined {
79 if (!this.tasksUsage
.has(name
)) {
80 this.tasksUsage
.set(name
, this.initTaskWorkerUsage(name
))
82 return this.tasksUsage
.get(name
)
85 private initWorkerInfo (worker
: Worker
, workerType
: WorkerType
): WorkerInfo
{
87 id
: this.getWorkerId(worker
, workerType
),
91 ...(workerType
=== WorkerTypes
.thread
&& {
92 messageChannel
: new MessageChannel()
97 private initWorkerUsage (): WorkerUsage
{
98 const getTasksQueueSize
= (): number => {
99 return this.tasksQueueSize()
101 const getTasksQueueMaxSize
= (): number => {
102 return this.tasksQueueMaxSize()
108 get
queued (): number {
109 return getTasksQueueSize()
111 get
maxQueued (): number {
112 return getTasksQueueMaxSize()
117 history
: new CircularArray()
120 history
: new CircularArray()
124 history
: new CircularArray()
127 history
: new CircularArray()
133 private initTaskWorkerUsage (name
: string): WorkerUsage
{
134 const getTaskQueueSize
= (): number => {
135 let taskQueueSize
= 0
136 for (const task
of this.tasksQueue
) {
137 if (task
.name
=== name
) {
147 get
queued (): number {
148 return getTaskQueueSize()
153 history
: new CircularArray()
156 history
: new CircularArray()
160 history
: new CircularArray()
163 history
: new CircularArray()
170 * Gets the worker id.
172 * @param worker - The worker.
173 * @param workerType - The worker type.
174 * @returns The worker id.
176 private getWorkerId (
178 workerType
: WorkerType
179 ): number | undefined {
180 if (workerType
=== WorkerTypes
.thread
) {
181 return worker
.threadId
182 } else if (workerType
=== WorkerTypes
.cluster
) {