refactor: refine .cfignore
[e-mobility-charging-stations-simulator.git] / src / worker / WorkerDynamicPool.ts
index 306bcd4f38f415b1823a205198e05a8ddb3a38a0..817536f0237251b88af5c242fb0800914e4574d5 100644 (file)
@@ -1,64 +1,63 @@
-import { DynamicThreadPool, PoolOptions } from 'poolifier';
+import type { EventEmitterAsyncResource } from 'node:events'
 
-import Utils from '../utils/Utils';
-import { Worker } from 'worker_threads';
-import WorkerAbstract from './WorkerAbstract';
-import { WorkerData } from '../types/Worker';
-import { WorkerUtils } from './WorkerUtils';
+import { DynamicThreadPool, type PoolInfo } from 'poolifier'
 
-export default class WorkerDynamicPool<T> extends WorkerAbstract {
-  private pool: DynamicThreadPool<WorkerData>;
+import { WorkerAbstract } from './WorkerAbstract.js'
+import type { WorkerData, WorkerOptions } from './WorkerTypes.js'
+import { randomizeDelay, sleep } from './WorkerUtils.js'
+
+export class WorkerDynamicPool extends WorkerAbstract<WorkerData> {
+  private readonly pool: DynamicThreadPool<WorkerData>
 
   /**
-   * Create a new `WorkerDynamicPool`.
+   * Creates a new `WorkerDynamicPool`.
    *
-   * @param {string} workerScript
-   * @param {number} min
-   * @param {number} max
-   * @param {number} workerStartDelay
-   * @param {PoolOptions} opts
+   * @param workerScript -
+   * @param workerOptions -
    */
-  constructor(workerScript: string, min: number, max: number, workerStartDelay?: number, opts?: PoolOptions<Worker>) {
-    super(workerScript, workerStartDelay);
-    opts.exitHandler = opts?.exitHandler ?? WorkerUtils.defaultExitHandler;
-    this.pool = new DynamicThreadPool<WorkerData>(min, max, this.workerScript, opts);
+  constructor (workerScript: string, workerOptions: WorkerOptions) {
+    super(workerScript, workerOptions)
+    this.pool = new DynamicThreadPool<WorkerData>(
+      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 | null {
-    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> {}
+  get maxElementsPerWorker (): number | undefined {
+    return undefined
+  }
 
-  /**
-   *
-   * @returns {Promise<void>}
-   * @public
-   */
-  // eslint-disable-next-line @typescript-eslint/require-await
-  public async stop(): Promise<void> {
-    return this.pool.destroy();
+  get emitter (): EventEmitterAsyncResource | undefined {
+    return this.pool.emitter
   }
 
-  /**
-   *
-   * @param {T} 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(this.workerStartDelay);
+  /** @inheritDoc */
+  public async start (): Promise<void> {
+    // This is intentional
+  }
+
+  /** @inheritDoc */
+  public async stop (): Promise<void> {
+    await this.pool.destroy()
+  }
+
+  /** @inheritDoc */
+  public async addElement (elementData: WorkerData): Promise<void> {
+    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.elementStartDelay! > 0 &&
+      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+      (await sleep(randomizeDelay(this.workerOptions.elementStartDelay!)))
   }
 }