Fix worker startup and cleanup some attributes in Wrk class.
[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
JB
19 const nbStations = stationURL.numberOfStations ? stationURL.numberOfStations : 0;
20 numStationsTotal += nbStations;
21 for (let index = 1; index <= nbStations; index++) {
3d2ff9e4 22 const workerData = {
7dde0b73 23 index,
3d2ff9e4
J
24 templateFile: stationURL.file
25 } as WorkerData;
97ed5cec
JB
26 if (Configuration.useWorkerPool()) {
27 worker = new Wrk('./dist/charging-station/StationWorker.js', workerData);
5fdab605 28 worker.start().catch(() => { });
97ed5cec 29 numConcurrentWorkers = Configuration.getWorkerPoolSize();
4faad557
JB
30 } else if (!Configuration.useWorkerPool() && (chargingStationsPerWorkerCounter === 0 || chargingStationsPerWorkerCounter === chargingStationsPerWorker)) {
31 // Start new Wrk with one charging station
32 worker = new Wrk('./dist/charging-station/StationWorker.js', workerData);
33 worker.start().catch(() => { });
34 numConcurrentWorkers++;
35 chargingStationsPerWorkerCounter = 1;
36 // Start Wrk sequentially to optimize memory at start time
37 await Utils.sleep(Constants.START_WORKER_DELAY);
38 } else if (!Configuration.useWorkerPool()) {
39 // Add charging station to existing Wrk
40 worker.addWorkerElement(workerData);
41 chargingStationsPerWorkerCounter++;
3d2ff9e4 42 }
7dde0b73
JB
43 }
44 } catch (error) {
45 // eslint-disable-next-line no-console
2e6f5966 46 console.log('Charging station start with template file ' + stationURL.file + ' error ' + JSON.stringify(error, null, ' '));
7dde0b73 47 }
3d2ff9e4 48 }
7dde0b73 49 } else {
d3a7883e 50 console.log('No stationTemplateURLs defined in configuration, exiting');
7dde0b73 51 }
6ecb15e4
JB
52 if (numStationsTotal === 0) {
53 console.log('No charging station template enabled in configuration, exiting');
d4a73fb7 54 } else {
ad3de6c4 55 console.log('Charging station simulator started with ' + numStationsTotal.toString() + ' charging station(s) of ' + numConcurrentWorkers.toString() + ' concurrently running');
6ecb15e4 56 }
7dde0b73
JB
57 } catch (error) {
58 // eslint-disable-next-line no-console
59 console.log('Bootstrap start error ' + JSON.stringify(error, null, ' '));
60 }
61 }
62}
63
4faad557
JB
64Bootstrap.start().catch(
65 (error) => {
66 console.error(error);
67 }
68);