Cleanup worker internal API
[e-mobility-charging-stations-simulator.git] / src / worker / WorkerStaticPool.ts
index 9c2bc41d4adac0d905a4603e3adfd9c57187a646..4e1e807c0953a171d8ecfc12fe3019a4cd919d73 100644 (file)
@@ -1,70 +1,60 @@
-import { FixedThreadPool, FixedThreadPoolOptions } from 'poolifier';
+import { WorkerData, WorkerOptions } from '../types/Worker';
 
-import Constants from '../utils/Constants';
+import { FixedThreadPool } from 'poolifier';
 import Utils from '../utils/Utils';
-import { WorkerData } from '../types/Worker';
-import Wrk from './Wrk';
+import WorkerAbstract from './WorkerAbstract';
+import { WorkerUtils } from './WorkerUtils';
 
-export default class WorkerStaticPool extends Wrk {
-  private pool: StaticPool;
+export default class WorkerStaticPool extends WorkerAbstract<WorkerData> {
+  private readonly pool: FixedThreadPool<WorkerData>;
 
   /**
    * Create a new `WorkerStaticPool`.
    *
-   * @param {string} workerScript
+   * @param workerScript
+   * @param workerOptions
    */
-  constructor(workerScript: string, numThreads: number) {
-    super(workerScript);
-    this.pool = StaticPool.getInstance(numThreads, this.workerScript);
+  constructor(workerScript: string, workerOptions?: WorkerOptions) {
+    super(workerScript, workerOptions);
+    this.workerOptions.poolOptions.exitHandler = 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 {
-    return 1;
+  get maxElementsPerWorker(): number | null {
+    return null;
   }
 
   /**
    *
-   * @return {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
+  }
 
   /**
    *
-   * @return {Promise<void>}
+   * @returns
    * @public
    */
-  public async addElement(elementData: WorkerData): Promise<void> {
-    await this.pool.execute(elementData);
-    // Start worker sequentially to optimize memory at startup
-    await Utils.sleep(Constants.START_WORKER_DELAY);
-  }
-}
-
-class StaticPool extends FixedThreadPool<WorkerData> {
-  private static instance: StaticPool;
-
-  private constructor(numThreads: number, workerScript: string, opts?: FixedThreadPoolOptions) {
-    super(numThreads, workerScript, opts);
+  public async stop(): Promise<void> {
+    return this.pool.destroy();
   }
 
-  public static getInstance(numThreads: number, workerScript: string): StaticPool {
-    if (!StaticPool.instance) {
-      StaticPool.instance = new StaticPool(numThreads, workerScript,
-        {
-          exitHandler: (code) => {
-            if (code !== 0) {
-              console.error(`Worker stopped with exit code ${code}`);
-            }
-          }
-        }
-      );
-    }
-    return StaticPool.instance;
+  /**
+   *
+   * @param elementData
+   * @returns
+   * @public
+   */
+  public async addElement(elementData: WorkerData): Promise<void> {
+    await this.pool.execute(elementData);
+    // Start element sequentially to optimize memory at startup
+    this.workerOptions.elementStartDelay > 0 && await Utils.sleep(this.workerOptions.elementStartDelay);
   }
 }