remove setMaxListener (not longer needed)
[e-mobility-charging-stations-simulator.git] / src / start.ts
index 61a6b5f9f56c03fe4602ac9723cede9aeacf635e..2bcfbae5153cf03b8a1989db915096381c3283f0 100644 (file)
@@ -1,39 +1,58 @@
 import Configuration from './utils/Configuration';
-import { StationTemplateURL } from './types/ConfigurationData';
+import Constants from './utils/Constants';
 import Utils from './utils/Utils';
+import WorkerData from './types/WorkerData';
 import Wrk from './charging-station/Worker';
-import logger from './utils/Logger';
 
 class Bootstrap {
-  static start() {
+  static async start() {
     try {
-      logger.debug('%s Configuration: %j', Utils.logPrefix(), Configuration.getConfig());
       let numStationsTotal = 0;
+      let numConcurrentWorkers = 0;
+      const chargingStationsPerWorker = Configuration.getChargingStationsPerWorker();
+      let chargingStationsPerWorkerCounter = 0;
+      let worker: Wrk;
       // Start each ChargingStation object in a worker thread
       if (Configuration.getStationTemplateURLs()) {
-        Configuration.getStationTemplateURLs().forEach((stationURL: StationTemplateURL) => {
+        for (const stationURL of Configuration.getStationTemplateURLs()) {
           try {
             const nbStations = stationURL.numberOfStations ? stationURL.numberOfStations : 0;
             numStationsTotal += nbStations;
             for (let index = 1; index <= nbStations; index++) {
-              const worker = new Wrk('./dist/charging-station/StationWorker.js', {
+              const workerData = {
                 index,
-                templateFile: stationURL.file,
-              }, numStationsTotal);
-              worker.start().catch(() => {});
+                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
+                await Utils.sleep(Constants.START_WORKER_DELAY);
+              } else if (!Configuration.useWorkerPool()) {
+                // Add charging station to existing Wrk
+                worker.addWorkerElement(workerData);
+                chargingStationsPerWorkerCounter++;
+              }
             }
           } catch (error) {
             // eslint-disable-next-line no-console
             console.log('Charging station start with template file ' + stationURL.file + ' error ' + JSON.stringify(error, null, ' '));
           }
-        });
+        }
       } else {
         console.log('No stationTemplateURLs defined in configuration, exiting');
       }
       if (numStationsTotal === 0) {
         console.log('No charging station template enabled in configuration, exiting');
       } else {
-        console.log('Charging station simulator started with ' + numStationsTotal.toString() + ' charging station(s)');
+        console.log('Charging station simulator started with ' + numStationsTotal.toString() + ' charging station(s) of ' + numConcurrentWorkers.toString() + ' concurrently running');
       }
     } catch (error) {
       // eslint-disable-next-line no-console
@@ -42,4 +61,8 @@ class Bootstrap {
   }
 }
 
-Bootstrap.start();
+Bootstrap.start().catch(
+  (error) => {
+    console.error(error);
+  }
+);