Use object factory design pattern for code handling workers.
[e-mobility-charging-stations-simulator.git] / src / start.ts
1 import Configuration from './utils/Configuration';
2 import WorkerData from './types/WorkerData';
3 import WorkerFactory from './worker/WorkerFactory';
4 import Wrk from './worker/Worker';
5
6 class Bootstrap {
7 static start() {
8 try {
9 let numStationsTotal = 0;
10 const workerImplementation: Wrk = WorkerFactory.getWorkerImpl('./dist/charging-station/StationWorker.js');
11 void workerImplementation.start();
12 // Start ChargingStation object in worker thread
13 if (Configuration.getStationTemplateURLs()) {
14 for (const stationURL of Configuration.getStationTemplateURLs()) {
15 try {
16 const nbStations = stationURL.numberOfStations ? stationURL.numberOfStations : 0;
17 for (let index = 1; index <= nbStations; index++) {
18 const workerData: WorkerData = {
19 index,
20 templateFile: stationURL.file
21 };
22 void workerImplementation.addElement(workerData);
23 numStationsTotal++;
24 }
25 } catch (error) {
26 // eslint-disable-next-line no-console
27 console.error('Charging station start with template file ' + stationURL.file + ' error ', error);
28 }
29 }
30 } else {
31 console.log('No stationTemplateURLs defined in configuration, exiting');
32 }
33 if (numStationsTotal === 0) {
34 console.log('No charging station template enabled in configuration, exiting');
35 } else {
36 console.log(`Charging station simulator started with ${numStationsTotal.toString()} charging station(s) and ${workerImplementation.size}${Configuration.useWorkerPool() ? `/${Configuration.getWorkerPoolMaxSize().toString()}` : ''} worker(s) concurrently running (${workerImplementation.maxElementsPerWorker} charging station(s) per worker)`);
37 }
38 } catch (error) {
39 // eslint-disable-next-line no-console
40 console.error('Bootstrap start error ', error);
41 }
42 }
43 }
44
45 Bootstrap.start();