build(deps-dev): apply updates
[e-mobility-charging-stations-simulator.git] / src / worker / WorkerStaticPool.ts
index ab8e309f208f4655ff0ba6acb96929fea12c8d3d..7000ac9a45e7f0ea8d5de89a9ba3496782c59b63 100644 (file)
@@ -1,80 +1,57 @@
-import { FixedThreadPool, PoolOptions } from 'poolifier';
+import { FixedThreadPool, type PoolEmitter, type PoolInfo } from 'poolifier';
 
-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 { WorkerAbstract } from './WorkerAbstract';
+import type { WorkerData, WorkerOptions } from './WorkerTypes';
+import { sleep } from './WorkerUtils';
 
-export default class WorkerStaticPool<T> extends WorkerAbstract {
-  private pool: StaticPool;
+export class WorkerStaticPool extends WorkerAbstract<WorkerData> {
+  private readonly pool: FixedThreadPool<WorkerData>;
 
   /**
-   * Create a new `WorkerStaticPool`.
+   * Creates a new `WorkerStaticPool`.
    *
-   * @param {string} workerScript
+   * @param workerScript -
+   * @param workerOptions -
    */
-  constructor(workerScript: string, numberOfThreads: number) {
-    super(workerScript);
-    this.pool = StaticPool.getInstance(numberOfThreads, this.workerScript);
+  constructor(workerScript: string, workerOptions?: WorkerOptions) {
+    super(workerScript, workerOptions);
+    this.pool = new FixedThreadPool(
+      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;
   }
 
-  /**
-   *
-   * @return {Promise<void>}
-   * @public
-   */
-  // eslint-disable-next-line @typescript-eslint/no-empty-function
-  public async start(): Promise<void> { }
-
-  /**
-   *
-   * @return {Promise<void>}
-   * @public
-   */
-  public async stop(): Promise<void> {
-    return this.pool.destroy();
+  get maxElementsPerWorker(): number | undefined {
+    return undefined;
   }
 
-  /**
-   *
-   * @return {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(): PoolEmitter | undefined {
+    return this.pool?.emitter;
   }
-}
 
-class StaticPool extends FixedThreadPool<WorkerData> {
-  private static instance: StaticPool;
+  /** @inheritDoc */
+  public async start(): Promise<void> {
+    // This is intentional
+  }
 
-  private constructor(numberOfThreads: number, workerScript: string, opts?: PoolOptions<Worker>) {
-    super(numberOfThreads, workerScript, opts);
+  /** @inheritDoc */
+  public async stop(): Promise<void> {
+    return this.pool.destroy();
   }
 
-  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;
+  /** @inheritDoc */
+  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 sleep(this.workerOptions.elementStartDelay));
   }
 }