From b25a42cd89afca28b53dbecbec422595155ddfbc Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 9 Jul 2023 16:32:05 +0200 Subject: [PATCH] fix: fix tasks usage accounting initializer MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/pools/abstract-pool.ts | 2 +- src/pools/worker-node.ts | 38 +++++++++++++++++++++++++++++++++++++- src/pools/worker.ts | 2 +- src/queue.ts | 28 ++++++++++++++++++++++++++++ tests/queue.test.js | 12 ++++++++++++ 5 files changed, 79 insertions(+), 3 deletions(-) diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index bb37dbb7..a7f1579f 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -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( diff --git a/src/pools/worker-node.ts b/src/pools/worker-node.ts index 7a7fc237..240f3045 100644 --- a/src/pools/worker-node.ts +++ b/src/pools/worker-node.ts @@ -76,7 +76,7 @@ implements IWorkerNode { /** @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 { } } + 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. * diff --git a/src/pools/worker.ts b/src/pools/worker.ts index c0f84ce8..41659adb 100644 --- a/src/pools/worker.ts +++ b/src/pools/worker.ts @@ -122,7 +122,7 @@ export interface TaskStatistics { /** * Maximum number of queued tasks. */ - readonly maxQueued: number + readonly maxQueued?: number /** * Number of failed tasks. */ diff --git a/src/queue.ts b/src/queue.ts index ead4b637..dab9f04c 100644 --- a/src/queue.ts +++ b/src/queue.ts @@ -71,4 +71,32 @@ export class Queue { 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 { + 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 + } + } + } + } } diff --git a/tests/queue.test.js b/tests/queue.test.js index 9bb810d3..f4b83d57 100644 --- a/tests/queue.test.js +++ b/tests/queue.test.js @@ -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 + } + }) }) -- 2.34.1