perf: lookup connector status once at OCPP responses handling
[e-mobility-charging-stations-simulator.git] / src / worker / WorkerStaticPool.ts
index 23112a6c566ff0b95f6f07cf48b606332ab1b7fd..acc61b8fad0fadac0fa66a7f7433a9e143db4658 100644 (file)
@@ -1,68 +1,58 @@
-import { FixedThreadPool } from 'poolifier';
+import { FixedThreadPool, type PoolEmitter, type PoolInfo } from 'poolifier';
 
-import type { WorkerData, WorkerOptions } from '../types/Worker';
-import Utils from '../utils/Utils';
-import WorkerAbstract from './WorkerAbstract';
-import { WorkerUtils } from './WorkerUtils';
+import { WorkerAbstract } from './WorkerAbstract';
+import type { WorkerData, WorkerOptions } from './WorkerTypes';
+import { sleep } from './WorkerUtils';
 
-export default class WorkerStaticPool extends WorkerAbstract<WorkerData> {
+export class WorkerStaticPool extends WorkerAbstract<WorkerData> {
   private readonly pool: FixedThreadPool<WorkerData>;
 
   /**
-   * Create a new `WorkerStaticPool`.
+   * Creates a new `WorkerStaticPool`.
    *
-   * @param workerScript
-   * @param workerOptions
+   * @param workerScript -
+   * @param workerOptions -
    */
-  constructor(workerScript: string, workerOptions?: WorkerOptions) {
+  constructor(workerScript: string, workerOptions: WorkerOptions) {
     super(workerScript, workerOptions);
-    this.workerOptions.poolOptions.errorHandler =
-      this.workerOptions?.poolOptions?.errorHandler ?? WorkerUtils.defaultErrorHandler;
-    this.workerOptions.poolOptions.exitHandler =
-      this.workerOptions?.poolOptions?.exitHandler ?? WorkerUtils.defaultExitHandler;
     this.pool = new FixedThreadPool(
       this.workerOptions.poolMaxSize,
       this.workerScript,
-      this.workerOptions.poolOptions
+      this.workerOptions.poolOptions,
     );
   }
 
+  get info(): PoolInfo {
+    return this.pool.info;
+  }
+
   get size(): number {
-    return this.pool.workers.length;
+    return this.pool.info.workerNodes;
   }
 
-  get maxElementsPerWorker(): number | null {
-    return null;
+  get maxElementsPerWorker(): number | undefined {
+    return undefined;
   }
 
-  /**
-   *
-   * @returns
-   * @public
-   */
+  get emitter(): PoolEmitter | undefined {
+    return this.pool?.emitter;
+  }
+
+  /** @inheritDoc */
   public async start(): Promise<void> {
     // This is intentional
   }
 
-  /**
-   *
-   * @returns
-   * @public
-   */
+  /** @inheritDoc */
   public async stop(): Promise<void> {
     return this.pool.destroy();
   }
 
-  /**
-   *
-   * @param elementData
-   * @returns
-   * @public
-   */
+  /** @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 Utils.sleep(this.workerOptions.elementStartDelay));
+    this.workerOptions.elementStartDelay! > 0 &&
+      (await sleep(this.workerOptions.elementStartDelay!));
   }
 }