),
maxQueuedTasks: this.workerNodes.reduce(
(accumulator, workerNode) =>
- accumulator + workerNode.usage.tasks.maxQueued,
+ accumulator + (workerNode.usage.tasks?.maxQueued ?? 0),
0
),
failedTasks: this.workerNodes.reduce(
/** @inheritdoc */
public getTasksWorkerUsage (name: string): WorkerUsage | undefined {
if (!this.tasksUsage.has(name)) {
- this.tasksUsage.set(name, this.initWorkerUsage())
+ this.tasksUsage.set(name, this.initTaskWorkerUsage(name))
}
return this.tasksUsage.get(name)
}
}
}
+ 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.
*
/**
* Maximum number of queued tasks.
*/
- readonly maxQueued: number
+ readonly maxQueued?: number
/**
* Number of failed tasks.
*/
this.size = 0
this.maxSize = 0
}
+
+ /**
+ * Returns an iterator for the queue.
+ *
+ * @returns An iterator for the queue.
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
+ */
+ [Symbol.iterator] (): Iterator<T> {
+ const items = this.items
+ let i = this.offset
+
+ return {
+ next: () => {
+ if (i >= items.length) {
+ return {
+ value: undefined,
+ done: true
+ }
+ }
+ const value = items[i]
+ i++
+ return {
+ value,
+ done: false
+ }
+ }
+ }
+ }
}
expect(queue.items).toStrictEqual([])
expect(queue.offset).toBe(0)
})
+
+ it('Verify iterator behavior', () => {
+ const queue = new Queue()
+ queue.enqueue(1)
+ queue.enqueue(2)
+ queue.enqueue(3)
+ let i = 1
+ for (const item of queue) {
+ expect(item).toBe(i)
+ ++i
+ }
+ })
})