Add some sanity checks to Wrk class.
[e-mobility-charging-stations-simulator.git] / src / start.ts
index 1e0916c23bdbc80b7e46c9f5cc86c126f84d5e57..1baea43212e7668446ec068a83d55fe1f8e0920f 100644 (file)
@@ -25,16 +25,17 @@ class Bootstrap {
               if (Configuration.useWorkerPool()) {
                 worker = new Wrk('./dist/charging-station/StationWorker.js', workerData);
                 worker.start().catch(() => { });
-                numConcurrentWorkers = worker.getPoolSize();
+                numConcurrentWorkers = worker.getWorkerPoolSize();
                 numStationsTotal = numConcurrentWorkers;
+                // Start Wrk sequentially to optimize memory at start time
                 await Utils.sleep(Constants.START_WORKER_DELAY);
-              } else if (!Configuration.useWorkerPool() && (chargingStationsPerWorkerCounter === 0 || chargingStationsPerWorkerCounter === chargingStationsPerWorker)) {
+              } 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 = new Wrk('./dist/charging-station/StationWorker.js', workerData, chargingStationsPerWorker);
                 worker.start().catch(() => { });
                 numConcurrentWorkers++;
-                numStationsTotal++;
                 chargingStationsPerWorkerCounter = 1;
+                numStationsTotal++;
                 // Start Wrk sequentially to optimize memory at start time
                 await Utils.sleep(Constants.START_WORKER_DELAY);
               } else if (!Configuration.useWorkerPool()) {
@@ -54,12 +55,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 {
-        if (Configuration.useWorkerPool()) {
-          console.log('Charging station simulator started with ' + numStationsTotal.toString() + ' charging station(s) and ' + numConcurrentWorkers.toString() + '/' + Configuration.getWorkerMaxPoolSize() + ' worker(s) concurrently running');
-        } else {
-          console.log('Charging station simulator started with ' + numStationsTotal.toString() + ' charging station(s) and ' + numConcurrentWorkers.toString() + ' worker(s) 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