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