1 import { CircularArray
} from
'../circular-array'
2 import { Queue
} from
'../queue'
13 export class WorkerNode
<Worker
extends IWorker
, Data
= unknown
>
14 implements IWorkerNode
<Worker
, Data
> {
15 public readonly worker
: Worker
16 public readonly info
: WorkerInfo
17 public usage
: WorkerUsage
18 private readonly tasksQueue
: Queue
<Task
<Data
>>
20 constructor (worker
: Worker
, workerType
: WorkerType
) {
22 this.info
= this.initWorkerInfo(worker
, workerType
)
23 this.usage
= this.initWorkerUsage()
24 this.tasksQueue
= new Queue
<Task
<Data
>>()
28 public tasksQueueSize (): number {
29 return this.tasksQueue
.size
33 * Worker node tasks queue maximum size.
35 * @returns The tasks queue maximum size.
37 private tasksQueueMaxSize (): number {
38 return this.tasksQueue
.maxSize
42 public enqueueTask (task
: Task
<Data
>): number {
43 return this.tasksQueue
.enqueue(task
)
47 public dequeueTask (): Task
<Data
> | undefined {
48 return this.tasksQueue
.dequeue()
52 public clearTasksQueue (): void {
53 this.tasksQueue
.clear()
56 public resetUsage (): void {
57 this.usage
= this.initWorkerUsage()
60 private initWorkerInfo (worker
: Worker
, workerType
: WorkerType
): WorkerInfo
{
62 id
: this.getWorkerId(worker
, workerType
),
69 private initWorkerUsage (): WorkerUsage
{
70 const getTasksQueueSize
= (): number => {
71 return this.tasksQueueSize()
73 const getTasksMaxQueueSize
= (): number => {
74 return this.tasksQueueMaxSize()
80 get
queued (): number {
81 return getTasksQueueSize()
83 get
maxQueued (): number {
84 return getTasksMaxQueueSize()
89 history
: new CircularArray()
92 history
: new CircularArray()
96 history
: new CircularArray()
99 history
: new CircularArray()
106 * Gets the worker id.
108 * @param worker - The worker.
109 * @returns The worker id.
111 private getWorkerId (
113 workerType
: WorkerType
114 ): number | undefined {
115 if (workerType
=== WorkerTypes
.thread
) {
116 return worker
.threadId
117 } else if (workerType
=== WorkerTypes
.cluster
) {