Add some sanity checks to Wrk class.
[e-mobility-charging-stations-simulator.git] / src / start.ts
1 import Configuration from './utils/Configuration';
2 import Constants from './utils/Constants';
3 import Utils from './utils/Utils';
4 import WorkerData from './types/WorkerData';
5 import Wrk from './charging-station/Worker';
6
7 class Bootstrap {
8 static async start() {
9 try {
10 let numStationsTotal = 0;
11 let numConcurrentWorkers = 0;
12 const chargingStationsPerWorker = Configuration.getChargingStationsPerWorker();
13 let chargingStationsPerWorkerCounter = 0;
14 let worker: Wrk;
15 // Start each ChargingStation object in a worker thread
16 if (Configuration.getStationTemplateURLs()) {
17 for (const stationURL of Configuration.getStationTemplateURLs()) {
18 try {
19 const nbStations = stationURL.numberOfStations ? stationURL.numberOfStations : 0;
20 for (let index = 1; index <= nbStations; index++) {
21 const workerData = {
22 index,
23 templateFile: stationURL.file
24 } as WorkerData;
25 if (Configuration.useWorkerPool()) {
26 worker = new Wrk('./dist/charging-station/StationWorker.js', workerData);
27 worker.start().catch(() => { });
28 numConcurrentWorkers = worker.getWorkerPoolSize();
29 numStationsTotal = numConcurrentWorkers;
30 // Start Wrk sequentially to optimize memory at start time
31 await Utils.sleep(Constants.START_WORKER_DELAY);
32 } else if (!Configuration.useWorkerPool() && (chargingStationsPerWorkerCounter === 0 || chargingStationsPerWorkerCounter >= chargingStationsPerWorker)) {
33 // Start new Wrk with one charging station
34 worker = new Wrk('./dist/charging-station/StationWorker.js', workerData, chargingStationsPerWorker);
35 worker.start().catch(() => { });
36 numConcurrentWorkers++;
37 chargingStationsPerWorkerCounter = 1;
38 numStationsTotal++;
39 // Start Wrk sequentially to optimize memory at start time
40 await Utils.sleep(Constants.START_WORKER_DELAY);
41 } else if (!Configuration.useWorkerPool()) {
42 // Add charging station to existing Wrk
43 worker.addWorkerElement(workerData);
44 chargingStationsPerWorkerCounter++;
45 numStationsTotal++;
46 }
47 }
48 } catch (error) {
49 // eslint-disable-next-line no-console
50 console.log('Charging station start with template file ' + stationURL.file + ' error ' + JSON.stringify(error, null, ' '));
51 }
52 }
53 } else {
54 console.log('No stationTemplateURLs defined in configuration, exiting');
55 }
56 if (numStationsTotal === 0) {
57 console.log('No charging station template enabled in configuration, exiting');
58 } else if (Configuration.useWorkerPool()) {
59 console.log('Charging station simulator started with ' + numStationsTotal.toString() + ' charging station(s) and ' + numConcurrentWorkers.toString() + '/' + Configuration.getWorkerPoolMaxSize().toString() + ' worker(s) concurrently running');
60 } else {
61 console.log('Charging station simulator started with ' + numStationsTotal.toString() + ' charging station(s) and ' + numConcurrentWorkers.toString() + ' worker(s) concurrently running');
62 }
63 } catch (error) {
64 // eslint-disable-next-line no-console
65 console.log('Bootstrap start error ' + JSON.stringify(error, null, ' '));
66 }
67 }
68 }
69
70 Bootstrap.start().catch(
71 (error) => {
72 console.error(error);
73 }
74 );