perf: run charging station as async resource in the worker set mode
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStationWorker.ts
index 81e0296d8761eca012b63085553df13615b14980..3fd74e361a663f2d04880d3ae78ac44835a2d4ac 100644 (file)
@@ -1,42 +1,51 @@
-// Partial Copyright Jerome Benoit. 2021. All Rights Reserved.
+// Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
 
-import { ChargingStationWorkerData, WorkerMessage, WorkerMessageEvents } from '../types/Worker';
-import { parentPort, workerData } from 'worker_threads';
+import { AsyncResource } from 'node:async_hooks';
+import { parentPort, workerData } from 'node:worker_threads';
 
-import ChargingStation from './ChargingStation';
-import Constants from '../utils/Constants';
 import { ThreadWorker } from 'poolifier';
-import Utils from '../utils/Utils';
+
+import { ChargingStation } from './ChargingStation';
+import type { ChargingStationWorkerData } from '../types';
+import { Configuration } from '../utils';
+import { WorkerConstants, type WorkerMessage, WorkerMessageEvents } from '../worker';
+
+/**
+ * Create and start a charging station instance
+ *
+ * @param data - workerData
+ */
+const startChargingStation = (data: ChargingStationWorkerData): void => {
+  new ChargingStation(data.index, data.templateFile).start();
+};
 
 // Conditionally export ThreadWorker instance for pool usage
 export let threadWorker: ThreadWorker;
-if (Utils.workerPoolInUse()) {
-  threadWorker = new ThreadWorker<ChargingStationWorkerData>(startChargingStation, { maxInactiveTime: Constants.WORKER_POOL_MAX_INACTIVE_TIME, async: false });
+if (Configuration.workerPoolInUse()) {
+  threadWorker = new ThreadWorker<ChargingStationWorkerData>(startChargingStation, {
+    maxInactiveTime: WorkerConstants.POOL_MAX_INACTIVE_TIME,
+  });
 } else {
-  // Add message listener to start charging station from main thread
-  addMessageListener();
-  if (!Utils.isUndefined(workerData)) {
-    startChargingStation({ index: workerData.index as number, templateFile: workerData.templateFile as string });
-  }
-}
+  class ChargingStationWorker extends AsyncResource {
+    constructor() {
+      super('ChargingStationWorker');
+    }
 
-/**
- * Listen messages send by the main thread
- */
-function addMessageListener(): void {
-  parentPort?.on('message', (message: WorkerMessage) => {
-    if (message.id === WorkerMessageEvents.START_WORKER_ELEMENT) {
+    public run(data: ChargingStationWorkerData): void {
+      this.runInAsyncScope(
+        startChargingStation.bind(this) as (data: ChargingStationWorkerData) => void,
+        this,
+        data
+      );
+    }
+  }
+  // Add message listener to start charging station from main thread
+  parentPort?.on('message', (message: WorkerMessage<ChargingStationWorkerData>) => {
+    if (message.id === WorkerMessageEvents.startWorkerElement) {
       startChargingStation(message.data);
     }
   });
-}
-
-/**
- * Create and start a charging station instance
- *
- * @param data workerData
- */
-function startChargingStation(data: ChargingStationWorkerData): void {
-  const station = new ChargingStation(data.index, data.templateFile);
-  station.start();
+  if (workerData !== undefined) {
+    new ChargingStationWorker().run(workerData as ChargingStationWorkerData);
+  }
 }