Cleanup workers handling classes.
[e-mobility-charging-stations-simulator.git] / src / start.ts
index 2bcfbae5153cf03b8a1989db915096381c3283f0..947a1abfd15011ae208529f92f7edd1ed41cb98e 100644 (file)
@@ -2,7 +2,8 @@ import Configuration from './utils/Configuration';
 import Constants from './utils/Constants';
 import Utils from './utils/Utils';
 import WorkerData from './types/WorkerData';
-import Wrk from './charging-station/Worker';
+import WorkerGroup from './charging-station/WorkerGroup';
+import WorkerPool from './charging-station/WorkerPool';
 
 class Bootstrap {
   static async start() {
@@ -11,34 +12,44 @@ class Bootstrap {
       let numConcurrentWorkers = 0;
       const chargingStationsPerWorker = Configuration.getChargingStationsPerWorker();
       let chargingStationsPerWorkerCounter = 0;
-      let worker: Wrk;
+      let workerImplementation: WorkerGroup | WorkerPool;
+      if (Configuration.useWorkerPool()) {
+        workerImplementation = new WorkerPool('./dist/charging-station/StationWorker.js');
+        void workerImplementation.start();
+      }
       // Start each ChargingStation object in a worker thread
       if (Configuration.getStationTemplateURLs()) {
         for (const stationURL of Configuration.getStationTemplateURLs()) {
           try {
             const nbStations = stationURL.numberOfStations ? stationURL.numberOfStations : 0;
-            numStationsTotal += nbStations;
             for (let index = 1; index <= nbStations; index++) {
-              const workerData = {
+              const workerData: WorkerData = {
                 index,
                 templateFile: stationURL.file
-              } as WorkerData;
+              };
               if (Configuration.useWorkerPool()) {
-                worker = new Wrk('./dist/charging-station/StationWorker.js', workerData);
-                worker.start().catch(() => { });
-                numConcurrentWorkers = Configuration.getWorkerPoolSize();
-              } else if (!Configuration.useWorkerPool() && (chargingStationsPerWorkerCounter === 0 || chargingStationsPerWorkerCounter === chargingStationsPerWorker)) {
-                // Start new Wrk with one charging station
-                worker = new Wrk('./dist/charging-station/StationWorker.js', workerData);
-                worker.start().catch(() => { });
-                numConcurrentWorkers++;
-                chargingStationsPerWorkerCounter = 1;
-                // Start Wrk sequentially to optimize memory at start time
+                void workerImplementation.addElement(workerData);
+                numConcurrentWorkers = workerImplementation.size;
+                numStationsTotal = workerImplementation.size;
+                // Start worker sequentially to optimize memory at start time
                 await Utils.sleep(Constants.START_WORKER_DELAY);
-              } else if (!Configuration.useWorkerPool()) {
-                // Add charging station to existing Wrk
-                worker.addWorkerElement(workerData);
-                chargingStationsPerWorkerCounter++;
+              } else {
+                // eslint-disable-next-line no-lonely-if
+                if (chargingStationsPerWorkerCounter === 0 || chargingStationsPerWorkerCounter >= chargingStationsPerWorker) {
+                  // Start new WorkerGroup with one charging station
+                  workerImplementation = new WorkerGroup('./dist/charging-station/StationWorker.js', workerData, chargingStationsPerWorker);
+                  void workerImplementation.start();
+                  numConcurrentWorkers++;
+                  chargingStationsPerWorkerCounter = 1;
+                  numStationsTotal++;
+                  // Start worker sequentially to optimize memory at start time
+                  await Utils.sleep(Constants.START_WORKER_DELAY);
+                } else {
+                  // Add charging station to existing WorkerGroup
+                  void workerImplementation.addElement(workerData);
+                  chargingStationsPerWorkerCounter++;
+                  numStationsTotal++;
+                }
               }
             }
           } catch (error) {
@@ -51,8 +62,10 @@ class Bootstrap {
       }
       if (numStationsTotal === 0) {
         console.log('No charging station template enabled in configuration, exiting');
+      } else if (Configuration.useWorkerPool()) {
+        console.log('Charging station simulator started with ' + numStationsTotal.toString() + ' charging station(s) and ' + numConcurrentWorkers.toString() + '/' + Configuration.getWorkerPoolMaxSize().toString() + ' worker(s) concurrently running');
       } else {
-        console.log('Charging station simulator started with ' + numStationsTotal.toString() + ' charging station(s) of ' + numConcurrentWorkers.toString() + ' concurrently running');
+        console.log('Charging station simulator started with ' + numStationsTotal.toString() + ' charging station(s) and ' + numConcurrentWorkers.toString() + ' worker(s) concurrently running');
       }
     } catch (error) {
       // eslint-disable-next-line no-console