fix: fix tasks usage accounting initializer
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 9 Jul 2023 14:32:05 +0000 (16:32 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 9 Jul 2023 14:32:05 +0000 (16:32 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/pools/abstract-pool.ts
src/pools/worker-node.ts
src/pools/worker.ts
src/queue.ts
tests/queue.test.js

index bb37dbb70fe4aac89115fc5da929270f0a39dd8d..a7f1579fde52512735224c1f47738f8ab7c211f2 100644 (file)
@@ -309,7 +309,7 @@ export abstract class AbstractPool<
       ),
       maxQueuedTasks: this.workerNodes.reduce(
         (accumulator, workerNode) =>
-          accumulator + workerNode.usage.tasks.maxQueued,
+          accumulator + (workerNode.usage.tasks?.maxQueued ?? 0),
         0
       ),
       failedTasks: this.workerNodes.reduce(
index 7a7fc237180d32c28b74250d5943867dcf80d9de..240f304588e3a369964d143ae3a8180b2aafbdaa 100644 (file)
@@ -76,7 +76,7 @@ implements IWorkerNode<Worker, Data> {
   /** @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)
   }
@@ -126,6 +126,42 @@ implements IWorkerNode<Worker, Data> {
     }
   }
 
+  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.
    *
index c0f84ce8819bc04e9078cb5d264a2ab4d9acd39f..41659adbdbec5b8d6b489d8dbfcb24f0820aff56 100644 (file)
@@ -122,7 +122,7 @@ export interface TaskStatistics {
   /**
    * Maximum number of queued tasks.
    */
-  readonly maxQueued: number
+  readonly maxQueued?: number
   /**
    * Number of failed tasks.
    */
index ead4b6375388398483eda10abaaae50e2c779928..dab9f04c2783f5a5aa039a3af894780f1b5c639f 100644 (file)
@@ -71,4 +71,32 @@ export class Queue<T> {
     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
+        }
+      }
+    }
+  }
 }
index 9bb810d3a9d90464e7253384f8be0e25e4423913..f4b83d575a27abb9cd8ae3c9578727b6e703b0d3 100644 (file)
@@ -62,4 +62,16 @@ describe('Queue test suite', () => {
     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
+    }
+  })
 })