Refine TS and linter configuration
[e-mobility-charging-stations-simulator.git] / src / worker / WorkerStaticPool.ts
index c78768e01360a788f131029025433ee8590a5f9b..6fc91524173e614e1fb4ff1a7bae586c5e216b22 100644 (file)
@@ -1,44 +1,50 @@
-import { FixedThreadPool, PoolOptions } from 'poolifier';
+import { FixedThreadPool } from 'poolifier';
 
+import type { WorkerData, WorkerOptions } from '../types/Worker';
 import Utils from '../utils/Utils';
-import { Worker } from 'worker_threads';
 import WorkerAbstract from './WorkerAbstract';
-import { WorkerData } from '../types/Worker';
+import { WorkerUtils } from './WorkerUtils';
 
-export default class WorkerStaticPool<T> extends WorkerAbstract {
-  private pool: StaticPool;
+export default class WorkerStaticPool extends WorkerAbstract<WorkerData> {
+  private readonly pool: FixedThreadPool<WorkerData>;
 
   /**
    * Create a new `WorkerStaticPool`.
    *
-   * @param {string} workerScript
-   * @param {number} numberOfThreads
-   * @param {number} startWorkerDelay
+   * @param workerScript
+   * @param workerOptions
    */
-  constructor(workerScript: string, numberOfThreads: number, startWorkerDelay?: number) {
-    super(workerScript, startWorkerDelay);
-    this.pool = StaticPool.getInstance(numberOfThreads, this.workerScript);
+  constructor(workerScript: string, workerOptions?: WorkerOptions) {
+    super(workerScript, workerOptions);
+    this.workerOptions.poolOptions.exitHandler =
+      this.workerOptions?.poolOptions?.exitHandler ?? WorkerUtils.defaultExitHandler;
+    this.pool = new FixedThreadPool(
+      this.workerOptions.poolMaxSize,
+      this.workerScript,
+      this.workerOptions.poolOptions
+    );
   }
 
   get size(): number {
     return this.pool.workers.length;
   }
 
-  get maxElementsPerWorker(): number {
+  get maxElementsPerWorker(): number | null {
     return null;
   }
 
   /**
    *
-   * @returns {Promise<void>}
+   * @returns
    * @public
    */
-  // eslint-disable-next-line @typescript-eslint/no-empty-function
-  public async start(): Promise<void> { }
+  public async start(): Promise<void> {
+    // This is intentional
+  }
 
   /**
    *
-   * @returns {Promise<void>}
+   * @returns
    * @public
    */
   public async stop(): Promise<void> {
@@ -48,35 +54,13 @@ export default class WorkerStaticPool<T> extends WorkerAbstract {
   /**
    *
    * @param elementData
-   * @returns {Promise<void>}
+   * @returns
    * @public
    */
-  public async addElement(elementData: T): Promise<void> {
+  public async addElement(elementData: WorkerData): Promise<void> {
     await this.pool.execute(elementData);
-    // Start worker sequentially to optimize memory at startup
-    await Utils.sleep(this.workerStartDelay);
-  }
-}
-
-class StaticPool extends FixedThreadPool<WorkerData> {
-  private static instance: StaticPool;
-
-  private constructor(numberOfThreads: number, workerScript: string, opts?: PoolOptions<Worker>) {
-    super(numberOfThreads, workerScript, opts);
-  }
-
-  public static getInstance(numberOfThreads: number, workerScript: string): StaticPool {
-    if (!StaticPool.instance) {
-      StaticPool.instance = new StaticPool(numberOfThreads, workerScript,
-        {
-          exitHandler: (code) => {
-            if (code !== 0) {
-              console.error(`Worker stopped with exit code ${code}`);
-            }
-          }
-        }
-      );
-    }
-    return StaticPool.instance;
+    // Start element sequentially to optimize memory at startup
+    this.workerOptions.elementStartDelay > 0 &&
+      (await Utils.sleep(this.workerOptions.elementStartDelay));
   }
 }