Fixed counters + start workers with delay
[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.getPoolSize();
29 numStationsTotal = numConcurrentWorkers;
30 await Utils.sleep(Constants.START_WORKER_DELAY);
31 } else if (!Configuration.useWorkerPool() && (chargingStationsPerWorkerCounter === 0 || chargingStationsPerWorkerCounter === chargingStationsPerWorker)) {
32 // Start new Wrk with one charging station
33 worker = new Wrk('./dist/charging-station/StationWorker.js', workerData);
34 worker.start().catch(() => { });
35 numConcurrentWorkers++;
36 numStationsTotal++;
37 chargingStationsPerWorkerCounter = 1;
38 // Start Wrk sequentially to optimize memory at start time
39 await Utils.sleep(Constants.START_WORKER_DELAY);
40 } else if (!Configuration.useWorkerPool()) {
41 // Add charging station to existing Wrk
42 worker.addWorkerElement(workerData);
43 chargingStationsPerWorkerCounter++;
44 numStationsTotal++;
45 }
46 }
47 } catch (error) {
48 // eslint-disable-next-line no-console
49 console.log('Charging station start with template file ' + stationURL.file + ' error ' + JSON.stringify(error, null, ' '));
50 }
51 }
52 } else {
53 console.log('No stationTemplateURLs defined in configuration, exiting');
54 }
55 if (numStationsTotal === 0) {
56 console.log('No charging station template enabled in configuration, exiting');
57 } else {
58 if (Configuration.useWorkerPool()) {
59 console.log('Charging station simulator started with ' + numStationsTotal.toString() + ' charging station(s) and ' + numConcurrentWorkers.toString() + '/' + Configuration.getWorkerMaxPoolSize() + ' 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 }
64 } catch (error) {
65 // eslint-disable-next-line no-console
66 console.log('Bootstrap start error ' + JSON.stringify(error, null, ' '));
67 }
68 }
69 }
70
71 Bootstrap.start().catch(
72 (error) => {
73 console.error(error);
74 }
75 );