1 import { CircularArray
} from
'../circular-array'
2 import { Queue
} from
'../queue'
3 import type { Task
} from
'../utility-types'
16 * @typeParam Worker - Type of worker.
17 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
19 export class WorkerNode
<Worker
extends IWorker
, Data
= unknown
>
20 implements IWorkerNode
<Worker
, Data
> {
21 public readonly worker
: Worker
22 public readonly info
: WorkerInfo
23 public usage
: WorkerUsage
24 private readonly tasksUsage
: Map
<string, WorkerUsage
>
25 private readonly tasksQueue
: Queue
<Task
<Data
>>
28 * Constructs a new worker node.
30 * @param worker - The worker.
31 * @param workerType - The worker type.
33 constructor (worker
: Worker
, workerType
: WorkerType
) {
35 this.info
= this.initWorkerInfo(worker
, workerType
)
36 this.usage
= this.initWorkerUsage()
37 this.tasksUsage
= new Map
<string, WorkerUsage
>()
38 this.tasksQueue
= new Queue
<Task
<Data
>>()
42 public tasksQueueSize (): number {
43 return this.tasksQueue
.size
47 * Tasks queue maximum size.
49 * @returns The tasks queue maximum size.
51 private tasksQueueMaxSize (): number {
52 return this.tasksQueue
.maxSize
56 public enqueueTask (task
: Task
<Data
>): number {
57 return this.tasksQueue
.enqueue(task
)
61 public dequeueTask (): Task
<Data
> | undefined {
62 return this.tasksQueue
.dequeue()
66 public clearTasksQueue (): void {
67 this.tasksQueue
.clear()
71 public resetUsage (): void {
72 this.usage
= this.initWorkerUsage()
73 this.tasksUsage
.clear()
77 public getTaskWorkerUsage (name
: string): WorkerUsage
| undefined {
78 if (!this.tasksUsage
.has(name
)) {
79 this.tasksUsage
.set(name
, this.initTaskWorkerUsage(name
))
81 return this.tasksUsage
.get(name
)
84 private initWorkerInfo (worker
: Worker
, workerType
: WorkerType
): WorkerInfo
{
86 id
: this.getWorkerId(worker
, workerType
),
93 private initWorkerUsage (): WorkerUsage
{
94 const getTasksQueueSize
= (): number => {
95 return this.tasksQueueSize()
97 const getTasksQueueMaxSize
= (): number => {
98 return this.tasksQueueMaxSize()
104 get
queued (): number {
105 return getTasksQueueSize()
107 get
maxQueued (): number {
108 return getTasksQueueMaxSize()
113 history
: new CircularArray()
116 history
: new CircularArray()
120 history
: new CircularArray()
123 history
: new CircularArray()
129 private initTaskWorkerUsage (name
: string): WorkerUsage
{
130 const getTaskQueueSize
= (): number => {
131 let taskQueueSize
= 0
132 for (const task
of this.tasksQueue
) {
133 if (task
.name
=== name
) {
143 get
queued (): number {
144 return getTaskQueueSize()
149 history
: new CircularArray()
152 history
: new CircularArray()
156 history
: new CircularArray()
159 history
: new CircularArray()
166 * Gets the worker id.
168 * @param worker - The worker.
169 * @param workerType - The worker type.
170 * @returns The worker id.
172 private getWorkerId (
174 workerType
: WorkerType
175 ): number | undefined {
176 if (workerType
=== WorkerTypes
.thread
) {
177 return worker
.threadId
178 } else if (workerType
=== WorkerTypes
.cluster
) {