Fix workerSet process mode.
[e-mobility-charging-stations-simulator.git] / src / start.ts
1 import Configuration from './utils/Configuration';
2 import { StationWorkerData } from './types/Worker';
3 import Utils from './utils/Utils';
4 import WorkerFactory from './worker/WorkerFactory';
5 import Wrk from './worker/Wrk';
6
7 class Bootstrap {
8 static async start() {
9 try {
10 let numStationsTotal = 0;
11 const workerImplementation: Wrk = WorkerFactory.getWorkerImpl('./dist/charging-station/StationWorker.js', Configuration.getWorkerProcess(), {
12 poolMaxSize: Configuration.getWorkerPoolMaxSize(),
13 poolMinSize: Configuration.getWorkerPoolMinSize(),
14 elementsPerWorker: Configuration.getChargingStationsPerWorker()
15 });
16 await workerImplementation.start();
17 // Start ChargingStation object in worker thread
18 if (Configuration.getStationTemplateURLs()) {
19 for (const stationURL of Configuration.getStationTemplateURLs()) {
20 try {
21 const nbStations = stationURL.numberOfStations ? stationURL.numberOfStations : 0;
22 for (let index = 1; index <= nbStations; index++) {
23 const workerData: StationWorkerData = {
24 index,
25 templateFile: stationURL.file
26 };
27 await workerImplementation.addElement(workerData);
28 numStationsTotal++;
29 }
30 } catch (error) {
31 // eslint-disable-next-line no-console
32 console.error('Charging station start with template file ' + stationURL.file + ' error ', error);
33 }
34 }
35 } else {
36 console.log('No stationTemplateURLs defined in configuration, exiting');
37 }
38 if (numStationsTotal === 0) {
39 console.log('No charging station template enabled in configuration, exiting');
40 } else {
41 console.log(`Charging station simulator started with ${numStationsTotal.toString()} charging station(s) and ${Utils.workerDynamicPoolInUse() ? `${Configuration.getWorkerPoolMinSize().toString()}/` : ''}${workerImplementation.size}${Utils.workerPoolInUse() ? `/${Configuration.getWorkerPoolMaxSize().toString()}` : ''} worker(s) concurrently running in '${Configuration.getWorkerProcess()}' mode (${workerImplementation.maxElementsPerWorker} charging station(s) per worker)`);
42 }
43 } catch (error) {
44 // eslint-disable-next-line no-console
45 console.error('Bootstrap start error ', error);
46 }
47 }
48 }
49
50 Bootstrap.start().catch(
51 (error) => {
52 console.error(error);
53 }
54 );