Memory optimization + worker configuration
[e-mobility-charging-stations-simulator.git] / src / start.ts
CommitLineData
6af9012e 1import Configuration from './utils/Configuration';
e118beaa 2import { StationTemplateURL } from './types/ConfigurationData';
3d2ff9e4 3import Utils from './utils/Utils';
6af9012e 4import Wrk from './charging-station/Worker';
3d2ff9e4
J
5import WorkerData from './types/WorkerData';
6import fs from 'fs';
7dde0b73
JB
7
8class Bootstrap {
3d2ff9e4 9 static async start() {
7dde0b73 10 try {
6ecb15e4 11 let numStationsTotal = 0;
ad3de6c4 12 let numConcurrentWorkers = 0;
3d2ff9e4
J
13 let worker: Wrk;
14 let chargingStationsPerWorker = Configuration.getChargingStationsPerWorker();
15 let counter = 0;
7dde0b73 16 // Start each ChargingStation object in a worker thread
2e6f5966 17 if (Configuration.getStationTemplateURLs()) {
3d2ff9e4 18 for await (const stationURL of Configuration.getStationTemplateURLs()) {
7dde0b73 19 try {
e118beaa
JB
20 const nbStations = stationURL.numberOfStations ? stationURL.numberOfStations : 0;
21 numStationsTotal += nbStations;
22 for (let index = 1; index <= nbStations; index++) {
3d2ff9e4 23 const workerData = {
7dde0b73 24 index,
3d2ff9e4
J
25 templateFile: stationURL.file
26 } as WorkerData;
27 if(counter === 0 || counter === chargingStationsPerWorker) {
28 // Start new worker with one charging station
29 worker = await new Wrk('./dist/charging-station/StationWorker.js', workerData, numStationsTotal);
30 worker.start().catch(() => {});
31 counter = 0;
32 // Start workers sequentially to optimize memory at start time
33 await Utils.sleep(500);
34 } else {
35 // Add new charging station to existing Worker
36 worker.startNewChargingStation(workerData, numStationsTotal)
37 }
38 counter++;
39 // Start charging station sequentially to optimize memory at start time
ad3de6c4 40 numConcurrentWorkers = worker.concurrentWorkers;
7dde0b73
JB
41 }
42 } catch (error) {
43 // eslint-disable-next-line no-console
2e6f5966 44 console.log('Charging station start with template file ' + stationURL.file + ' error ' + JSON.stringify(error, null, ' '));
7dde0b73 45 }
3d2ff9e4 46 }
7dde0b73 47 } else {
d3a7883e 48 console.log('No stationTemplateURLs defined in configuration, exiting');
7dde0b73 49 }
6ecb15e4
JB
50 if (numStationsTotal === 0) {
51 console.log('No charging station template enabled in configuration, exiting');
d4a73fb7 52 } else {
ad3de6c4 53 console.log('Charging station simulator started with ' + numStationsTotal.toString() + ' charging station(s) of ' + numConcurrentWorkers.toString() + ' concurrently running');
6ecb15e4 54 }
7dde0b73
JB
55 } catch (error) {
56 // eslint-disable-next-line no-console
57 console.log('Bootstrap start error ' + JSON.stringify(error, null, ' '));
58 }
59 }
60}
61
62Bootstrap.start();