feat: ensure charging station add op return its station info
[e-mobility-charging-stations-simulator.git] / src / worker / WorkerDynamicPool.ts
index a9aa56f09765912a3a4193bb8acc299654d1d3a2..5217d7fe16a6772a81b946969702fe1189f0d5dc 100644 (file)
@@ -1,84 +1,67 @@
-import { DynamicThreadPool, PoolOptions } from 'poolifier';
+import type { EventEmitterAsyncResource } from 'node:events'
 
-import Constants from '../utils/Constants';
-import Utils from '../utils/Utils';
-import { Worker } from 'worker_threads';
-import WorkerAbstract from './WorkerAbstract';
-import { WorkerData } from '../types/Worker';
+import { DynamicThreadPool, type PoolInfo } from 'poolifier'
 
-export default class WorkerDynamicPool<T> extends WorkerAbstract {
-  private pool: DynamicPool;
+import { WorkerAbstract } from './WorkerAbstract.js'
+import type { WorkerData, WorkerOptions } from './WorkerTypes.js'
+import { randomizeDelay, sleep } from './WorkerUtils.js'
+
+export class WorkerDynamicPool<D extends WorkerData, R extends WorkerData> extends WorkerAbstract<
+D,
+R
+> {
+  private readonly pool: DynamicThreadPool<D, R>
 
   /**
-   * Create a new `WorkerDynamicPool`.
+   * Creates a new `WorkerDynamicPool`.
    *
-   * @param {string} workerScript
-   * @param {number} min
-   * @param {number} max
+   * @param workerScript -
+   * @param workerOptions -
    */
-  constructor(workerScript: string, min: number, max: number,) {
-    super(workerScript);
-    this.pool = DynamicPool.getInstance(min, max, this.workerScript);
+  constructor (workerScript: string, workerOptions: WorkerOptions) {
+    super(workerScript, workerOptions)
+    this.pool = new DynamicThreadPool<D, R>(
+      this.workerOptions.poolMinSize,
+      this.workerOptions.poolMaxSize,
+      this.workerScript,
+      this.workerOptions.poolOptions
+    )
   }
 
-  get size(): number {
-    return this.pool.workers.length;
+  get info (): PoolInfo {
+    return this.pool.info
   }
 
-  get maxElementsPerWorker(): number {
-    return null;
+  get size (): number {
+    return this.pool.info.workerNodes
   }
 
-  /**
-   *
-   * @returns {Promise<void>}
-   * @public
-   */
-  // eslint-disable-next-line @typescript-eslint/no-empty-function
-  public async start(): Promise<void> { }
-
-  /**
-   *
-   * @returns {Promise<void>}
-   * @public
-   */
-  // eslint-disable-next-line @typescript-eslint/require-await
-  public async stop(): Promise<void> {
-    return this.pool.destroy();
+  get maxElementsPerWorker (): number | undefined {
+    return undefined
   }
 
-  /**
-   *
-   * @param elementData
-   * @returns {Promise<void>}
-   * @public
-   */
-  public async addElement(elementData: T): Promise<void> {
-    await this.pool.execute(elementData);
-    // Start worker sequentially to optimize memory at startup
-    await Utils.sleep(Constants.START_WORKER_DELAY);
+  get emitter (): EventEmitterAsyncResource | undefined {
+    return this.pool.emitter
   }
-}
 
-class DynamicPool extends DynamicThreadPool<WorkerData> {
-  private static instance: DynamicPool;
+  /** @inheritDoc */
+  public start (): void {
+    this.pool.start()
+  }
 
-  private constructor(min: number, max: number, workerScript: string, opts?: PoolOptions<Worker>) {
-    super(min, max, workerScript, opts);
+  /** @inheritDoc */
+  public async stop (): Promise<void> {
+    await this.pool.destroy()
   }
 
-  public static getInstance(min: number, max: number, workerScript: string): DynamicPool {
-    if (!DynamicPool.instance) {
-      DynamicPool.instance = new DynamicPool(min, max, workerScript,
-        {
-          exitHandler: (code) => {
-            if (code !== 0) {
-              console.error(`Worker stopped with exit code ${code}`);
-            }
-          }
-        }
-      );
-    }
-    return DynamicPool.instance;
+  /** @inheritDoc */
+  public async addElement (elementData: D): Promise<R> {
+    const response = await this.pool.execute(elementData)
+    // Start element sequentially to optimize memory at startup
+    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+    this.workerOptions.elementAddDelay! > 0 &&
+      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+      (await sleep(randomizeDelay(this.workerOptions.elementAddDelay!)))
+    return response
   }
 }