feat: expose pool/set information
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 2 Jun 2023 20:04:34 +0000 (22:04 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Fri, 2 Jun 2023 20:04:34 +0000 (22:04 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/worker/WorkerAbstract.ts
src/worker/WorkerDynamicPool.ts
src/worker/WorkerSet.ts
src/worker/WorkerStaticPool.ts
src/worker/WorkerTypes.ts

index 4934cff9e331b949991bb04d02a9e708598e2f60..d42f629db3ff0b5ced69039c28c2b180dab7731b 100644 (file)
@@ -1,12 +1,15 @@
 import type EventEmitterAsyncResource from 'node:events';
 import fs from 'node:fs';
 
+import type { PoolInfo } from 'poolifier';
+
 import { WorkerConstants } from './WorkerConstants';
-import type { WorkerData, WorkerOptions } from './WorkerTypes';
+import type { SetInfo, WorkerData, WorkerOptions } from './WorkerTypes';
 
 export abstract class WorkerAbstract<T extends WorkerData> {
   protected readonly workerScript: string;
   protected readonly workerOptions: WorkerOptions;
+  public abstract readonly info: PoolInfo | SetInfo;
   public abstract readonly size: number;
   public abstract readonly maxElementsPerWorker: number | undefined;
   public abstract readonly emitter: EventEmitterAsyncResource | undefined;
index b7b39819e82ed7e67e45acafec8bd3b419f61686..38d85aea06d0252531076338382cf161e447d991 100644 (file)
@@ -1,7 +1,7 @@
 import type EventEmitterAsyncResource from 'node:events';
 import type { Worker } from 'node:worker_threads';
 
-import { DynamicThreadPool, type ErrorHandler, type ExitHandler } from 'poolifier';
+import { DynamicThreadPool, type ErrorHandler, type ExitHandler, type PoolInfo } from 'poolifier';
 
 import { WorkerAbstract } from './WorkerAbstract';
 import type { WorkerData, WorkerOptions } from './WorkerTypes';
@@ -33,8 +33,12 @@ export class WorkerDynamicPool extends WorkerAbstract<WorkerData> {
     );
   }
 
+  get info(): PoolInfo {
+    return this.pool.info;
+  }
+
   get size(): number {
-    return this.pool.workerNodes.length;
+    return this.pool.info.workerNodes;
   }
 
   get maxElementsPerWorker(): number | undefined {
index ff172d63cd158b5ce7ed3541f2eaf7702bace5b5..b16b95424ec7d8eca5996e7b05ddf4759ef2df13 100644 (file)
@@ -7,6 +7,7 @@ import { WorkerAbstract } from './WorkerAbstract';
 import { WorkerConstants } from './WorkerConstants';
 import {
   type MessageHandler,
+  type SetInfo,
   type WorkerData,
   WorkerMessageEvents,
   type WorkerOptions,
@@ -30,6 +31,17 @@ export class WorkerSet extends WorkerAbstract<WorkerData> {
     this.emitter = new EventEmitterAsyncResource();
   }
 
+  get info(): SetInfo {
+    return {
+      size: this.size,
+      runningElements: [...this.workerSet].reduce(
+        (accumulator, workerSetElement) => accumulator + workerSetElement.numberOfWorkerElements,
+        0
+      ),
+      elementsPerWorker: this.maxElementsPerWorker,
+    };
+  }
+
   get size(): number {
     return this.workerSet.size;
   }
index af611b6387777843d9d9fc3ef98d3dfdfd047360..f5c2a6e89f60658dc0e30082d4aea62ad8e0ad62 100644 (file)
@@ -1,7 +1,7 @@
 import type EventEmitterAsyncResource from 'node:events';
 import type { Worker } from 'node:worker_threads';
 
-import { type ErrorHandler, type ExitHandler, FixedThreadPool } from 'poolifier';
+import { type ErrorHandler, type ExitHandler, FixedThreadPool, type PoolInfo } from 'poolifier';
 
 import { WorkerAbstract } from './WorkerAbstract';
 import type { WorkerData, WorkerOptions } from './WorkerTypes';
@@ -32,8 +32,12 @@ export class WorkerStaticPool extends WorkerAbstract<WorkerData> {
     );
   }
 
+  get info(): PoolInfo {
+    return this.pool.info;
+  }
+
   get size(): number {
-    return this.pool.workerNodes.length;
+    return this.pool.info.workerNodes;
   }
 
   get maxElementsPerWorker(): number | undefined {
index 1b7cbb300cf95a7ac5c9ba22bc4e8ac4a622afe6..6f780bce30dc6c6f274578a38972f7b28844adb3 100644 (file)
@@ -8,6 +8,12 @@ export enum WorkerProcessType {
   staticPool = 'staticPool',
 }
 
+export type SetInfo = {
+  size: number;
+  runningElements: number;
+  elementsPerWorker: number;
+};
+
 export type MessageHandler<T> = (this: T, message: unknown) => void;
 
 export type WorkerOptions = {