Move workers handling code in its own directory.
[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 WorkerGroup from './worker/WorkerGroup';
6 import WorkerPool from './worker/WorkerPool';
7
8 class Bootstrap {
9 static async start() {
10 try {
11 let numStationsTotal = 0;
12 let numConcurrentWorkers = 0;
13 const chargingStationsPerWorker = Configuration.getChargingStationsPerWorker();
14 let chargingStationsPerWorkerCounter = 0;
15 let workerImplementation: WorkerGroup | WorkerPool;
16 if (Configuration.useWorkerPool()) {
17 workerImplementation = new WorkerPool('./dist/charging-station/StationWorker.js');
18 void workerImplementation.start();
19 }
20 // Start each ChargingStation object in a worker thread
21 if (Configuration.getStationTemplateURLs()) {
22 for (const stationURL of Configuration.getStationTemplateURLs()) {
23 try {
24 const nbStations = stationURL.numberOfStations ? stationURL.numberOfStations : 0;
25 for (let index = 1; index <= nbStations; index++) {
26 const workerData: WorkerData = {
27 index,
28 templateFile: stationURL.file
29 };
30 if (Configuration.useWorkerPool()) {
31 void workerImplementation.addElement(workerData);
32 numConcurrentWorkers = workerImplementation.size;
33 // Start worker sequentially to optimize memory at start time
34 await Utils.sleep(Constants.START_WORKER_DELAY);
35 } else {
36 // eslint-disable-next-line no-lonely-if
37 if (chargingStationsPerWorkerCounter === 0 || chargingStationsPerWorkerCounter >= chargingStationsPerWorker) {
38 // Start new WorkerGroup with one charging station
39 workerImplementation = new WorkerGroup('./dist/charging-station/StationWorker.js', workerData, chargingStationsPerWorker);
40 void workerImplementation.start();
41 numConcurrentWorkers++;
42 chargingStationsPerWorkerCounter = 1;
43 // Start worker sequentially to optimize memory at start time
44 await Utils.sleep(Constants.START_WORKER_DELAY);
45 } else {
46 // Add charging station to existing WorkerGroup
47 void workerImplementation.addElement(workerData);
48 chargingStationsPerWorkerCounter++;
49 }
50 }
51 numStationsTotal++;
52 }
53 } catch (error) {
54 // eslint-disable-next-line no-console
55 console.log('Charging station start with template file ' + stationURL.file + ' error ' + JSON.stringify(error, null, ' '));
56 }
57 }
58 } else {
59 console.log('No stationTemplateURLs defined in configuration, exiting');
60 }
61 if (numStationsTotal === 0) {
62 console.log('No charging station template enabled in configuration, exiting');
63 } else if (Configuration.useWorkerPool()) {
64 console.log(`Charging station simulator started with ${numStationsTotal.toString()} charging station(s) and ${numConcurrentWorkers.toString()}/${Configuration.getWorkerPoolMaxSize().toString()} worker(s) concurrently running`);
65 } else {
66 console.log(`Charging station simulator started with ${numStationsTotal.toString()} charging station(s) and ${numConcurrentWorkers.toString()} worker(s) concurrently running (${chargingStationsPerWorker} charging station(s) per worker)`);
67 }
68 } catch (error) {
69 // eslint-disable-next-line no-console
70 console.log('Bootstrap start error ' + JSON.stringify(error, null, ' '));
71 }
72 }
73 }
74
75 Bootstrap.start().catch(
76 (error) => {
77 console.error(error);
78 }
79 );