feat: add event emitter to worker pool/set code
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 2 Jun 2023 07:45:05 +0000 (09:45 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Fri, 2 Jun 2023 07:45:05 +0000 (09:45 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
package.json
src/charging-station/ChargingStationWorker.ts
src/worker/WorkerAbstract.ts
src/worker/WorkerDynamicPool.ts
src/worker/WorkerSet.ts
src/worker/WorkerStaticPool.ts

index 049eee85714dc970fde832424307182ac96bd190..a12c55354089426cd749906fa3e2bfd1d93d9ec1 100644 (file)
@@ -2,7 +2,7 @@
   "name": "e-mobility-charging-stations-simulator",
   "version": "1.2.15",
   "engines": {
-    "node": ">=16.9.0",
+    "node": ">=16.14.0",
     "pnpm": ">=8.6.0"
   },
   "volta": {
index 3703de9c10a31abde58063ac4d5882afbe5353fb..de0b7a9fd95be118b6f6e154e9bc5a25504cf2be 100644 (file)
@@ -25,18 +25,14 @@ class ChargingStationWorker extends AsyncResource {
     // Add message listener to create and start charging station from the main thread
     parentPort?.on('message', (message: WorkerMessage<ChargingStationWorkerData>) => {
       if (message.id === WorkerMessageEvents.startWorkerElement) {
-        this.run(message.data);
+        this.runInAsyncScope(
+          startChargingStation.bind(this) as (data: ChargingStationWorkerData) => void,
+          this,
+          message.data
+        );
       }
     });
   }
-
-  private run(data: ChargingStationWorkerData): void {
-    this.runInAsyncScope(
-      startChargingStation.bind(this) as (data: ChargingStationWorkerData) => void,
-      this,
-      data
-    );
-  }
 }
 
 export let chargingStationWorker: ChargingStationWorker;
index 7753e64d392f8f960f24f6b41c29340e0e71f79b..4934cff9e331b949991bb04d02a9e708598e2f60 100644 (file)
@@ -1,3 +1,4 @@
+import type EventEmitterAsyncResource from 'node:events';
 import fs from 'node:fs';
 
 import { WorkerConstants } from './WorkerConstants';
@@ -8,6 +9,7 @@ export abstract class WorkerAbstract<T extends WorkerData> {
   protected readonly workerOptions: WorkerOptions;
   public abstract readonly size: number;
   public abstract readonly maxElementsPerWorker: number | undefined;
+  public abstract readonly emitter: EventEmitterAsyncResource | undefined;
 
   /**
    * `WorkerAbstract` constructor.
index af8b7e2add2e882195baa8983fc7a2e4465d15c4..b7b39819e82ed7e67e45acafec8bd3b419f61686 100644 (file)
@@ -1,3 +1,4 @@
+import type EventEmitterAsyncResource from 'node:events';
 import type { Worker } from 'node:worker_threads';
 
 import { DynamicThreadPool, type ErrorHandler, type ExitHandler } from 'poolifier';
@@ -40,6 +41,10 @@ export class WorkerDynamicPool extends WorkerAbstract<WorkerData> {
     return undefined;
   }
 
+  get emitter(): EventEmitterAsyncResource | undefined {
+    return this.pool?.emitter;
+  }
+
   /** @inheritDoc */
   public async start(): Promise<void> {
     // This is intentional
index da1e5dd5b7b47cbf44291f2aa446ad9b154ae4c7..ff172d63cd158b5ce7ed3541f2eaf7702bace5b5 100644 (file)
@@ -1,5 +1,6 @@
 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
 
+import EventEmitterAsyncResource from 'node:events';
 import { Worker } from 'node:worker_threads';
 
 import { WorkerAbstract } from './WorkerAbstract';
@@ -14,6 +15,7 @@ import {
 import { defaultErrorHandler, defaultExitHandler, sleep } from './WorkerUtils';
 
 export class WorkerSet extends WorkerAbstract<WorkerData> {
+  public readonly emitter: EventEmitterAsyncResource;
   private readonly workerSet: Set<WorkerSetElement>;
 
   /**
@@ -25,6 +27,7 @@ export class WorkerSet extends WorkerAbstract<WorkerData> {
   constructor(workerScript: string, workerOptions?: WorkerOptions) {
     super(workerScript, workerOptions);
     this.workerSet = new Set<WorkerSetElement>();
+    this.emitter = new EventEmitterAsyncResource();
   }
 
   get size(): number {
@@ -40,22 +43,13 @@ export class WorkerSet extends WorkerAbstract<WorkerData> {
     if (!this.workerSet) {
       throw new Error("Cannot add a WorkerSet element: workers' set does not exist");
     }
-    let lastWorkerSetElement = this.getLastWorkerSetElement();
-    if (
-      this.workerSet.size === 0 ||
-      lastWorkerSetElement.numberOfWorkerElements >= this.workerOptions.elementsPerWorker
-    ) {
-      this.startWorker();
-      // Start worker sequentially to optimize memory at startup
-      this.workerOptions.workerStartDelay > 0 && (await sleep(this.workerOptions.workerStartDelay));
-      lastWorkerSetElement = this.getLastWorkerSetElement();
-    }
-    lastWorkerSetElement.worker.postMessage({
+    const workerSetElement = await this.getWorkerSetElement();
+    workerSetElement.worker.postMessage({
       id: WorkerMessageEvents.startWorkerElement,
       data: elementData,
     });
-    ++lastWorkerSetElement.numberOfWorkerElements;
-    // Start element sequentially to optimize memory at startup
+    ++workerSetElement.numberOfWorkerElements;
+    // Add element sequentially to optimize memory at startup
     if (this.workerOptions.elementStartDelay > 0) {
       await sleep(this.workerOptions.elementStartDelay);
     }
@@ -63,8 +57,8 @@ export class WorkerSet extends WorkerAbstract<WorkerData> {
 
   /** @inheritDoc */
   public async start(): Promise<void> {
-    this.startWorker();
-    // Start worker sequentially to optimize memory at startup
+    this.addWorkerSetElement();
+    // Add worker set element sequentially to optimize memory at startup
     this.workerOptions.workerStartDelay > 0 && (await sleep(this.workerOptions.workerStartDelay));
   }
 
@@ -77,9 +71,9 @@ export class WorkerSet extends WorkerAbstract<WorkerData> {
   }
 
   /**
-   * Start a new `Worker`.
+   * Add a new `WorkerSetElement`.
    */
-  private startWorker(): void {
+  private addWorkerSetElement(): WorkerSetElement {
     const worker = new Worker(this.workerScript);
     worker.on(
       'message',
@@ -88,21 +82,34 @@ export class WorkerSet extends WorkerAbstract<WorkerData> {
       ) as MessageHandler<Worker>
     );
     worker.on('error', defaultErrorHandler.bind(this) as (err: Error) => void);
-    worker.on('error', () => this.startWorker());
+    worker.on('error', (error) => {
+      this.emitter.emit('error', error);
+      this.addWorkerSetElement();
+    });
     worker.on('exit', defaultExitHandler.bind(this) as (exitCode: number) => void);
     worker.on('exit', () => this.workerSet.delete(this.getWorkerSetElementByWorker(worker)));
-    this.workerSet.add({ worker, numberOfWorkerElements: 0 });
+    const workerSetElement: WorkerSetElement = { worker, numberOfWorkerElements: 0 };
+    this.workerSet.add(workerSetElement);
+    return workerSetElement;
   }
 
-  private getLastWorkerSetElement(): WorkerSetElement {
-    let workerSetElement: WorkerSetElement;
-    for (workerSetElement of this.workerSet) {
-      /* This is intentional */
+  private async getWorkerSetElement(): Promise<WorkerSetElement> {
+    let chosenWorkerSetElement: WorkerSetElement;
+    for (const workerSetElement of this.workerSet) {
+      if (workerSetElement.numberOfWorkerElements < this.workerOptions.elementsPerWorker) {
+        chosenWorkerSetElement = workerSetElement;
+        break;
+      }
     }
-    return workerSetElement;
+    if (!chosenWorkerSetElement) {
+      chosenWorkerSetElement = this.addWorkerSetElement();
+      // Add worker set element sequentially to optimize memory at startup
+      this.workerOptions.workerStartDelay > 0 && (await sleep(this.workerOptions.workerStartDelay));
+    }
+    return chosenWorkerSetElement;
   }
 
-  private getWorkerSetElementByWorker(worker: Worker): WorkerSetElement {
+  private getWorkerSetElementByWorker(worker: Worker): WorkerSetElement | undefined {
     let workerSetElt: WorkerSetElement;
     for (const workerSetElement of this.workerSet) {
       if (workerSetElement.worker.threadId === worker.threadId) {
index 6034c6e041ac77a487e252ccc0ac8c6a3c045030..af611b6387777843d9d9fc3ef98d3dfdfd047360 100644 (file)
@@ -1,3 +1,4 @@
+import type EventEmitterAsyncResource from 'node:events';
 import type { Worker } from 'node:worker_threads';
 
 import { type ErrorHandler, type ExitHandler, FixedThreadPool } from 'poolifier';
@@ -39,6 +40,10 @@ export class WorkerStaticPool extends WorkerAbstract<WorkerData> {
     return undefined;
   }
 
+  get emitter(): EventEmitterAsyncResource | undefined {
+    return this.pool?.emitter;
+  }
+
   /** @inheritDoc */
   public async start(): Promise<void> {
     // This is intentional