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