Also rename the sonar project name.
[e-mobility-charging-stations-simulator.git] / src / charging-station / Bootstrap.ts
CommitLineData
ded13d97
JB
1import Configuration from '../utils/Configuration';
2import { StationWorkerData } from '../types/Worker';
3import Utils from '../utils/Utils';
fd1fdf1b 4import WorkerAbstract from '../worker/WorkerAbstract';
ded13d97 5import WorkerFactory from '../worker/WorkerFactory';
ded13d97 6import { isMainThread } from 'worker_threads';
bf1866b2 7import path from 'path';
84e52c2c 8import { version } from '../../package.json';
ded13d97
JB
9
10export default class Bootstrap {
535aaa27
JB
11 private static instance: Bootstrap | null = null;
12 private static workerImplementation: WorkerAbstract | null = null;
aef1b33a 13 private version: string = version;
eb87fe87 14 private started: boolean;
ded13d97 15 private workerScript: string;
ded13d97
JB
16
17 private constructor() {
eb87fe87 18 this.started = false;
bf1866b2 19 this.workerScript = path.join(path.resolve(__dirname, '../'), 'charging-station', 'StationWorker.js');
8df3f0a9 20 this.initWorkerImplementation();
7874b0b1 21 Configuration.setConfigurationChangeCallback(async () => Bootstrap.getInstance().restart());
ded13d97
JB
22 }
23
24 public static getInstance(): Bootstrap {
25 if (!Bootstrap.instance) {
26 Bootstrap.instance = new Bootstrap();
27 }
28 return Bootstrap.instance;
29 }
30
31 public async start(): Promise<void> {
eb87fe87 32 if (isMainThread && !this.started) {
ded13d97
JB
33 try {
34 let numStationsTotal = 0;
535aaa27 35 await Bootstrap.workerImplementation.start();
ded13d97
JB
36 // Start ChargingStation object in worker thread
37 if (Configuration.getStationTemplateURLs()) {
38 for (const stationURL of Configuration.getStationTemplateURLs()) {
39 try {
40 const nbStations = stationURL.numberOfStations ? stationURL.numberOfStations : 0;
41 for (let index = 1; index <= nbStations; index++) {
42 const workerData: StationWorkerData = {
43 index,
bf1866b2 44 templateFile: path.join(path.resolve(__dirname, '../'), 'assets', 'station-templates', path.basename(stationURL.file))
ded13d97 45 };
535aaa27 46 await Bootstrap.workerImplementation.addElement(workerData);
ded13d97
JB
47 numStationsTotal++;
48 }
49 } catch (error) {
ded13d97
JB
50 console.error('Charging station start with template file ' + stationURL.file + ' error ', error);
51 }
52 }
53 } else {
54 console.log('No stationTemplateURLs defined in configuration, exiting');
55 }
56 if (numStationsTotal === 0) {
57 console.log('No charging station template enabled in configuration, exiting');
58 } else {
535aaa27 59 console.log(`Charging station simulator ${this.version} started with ${numStationsTotal.toString()} charging station(s) and ${Utils.workerDynamicPoolInUse() ? `${Configuration.getWorkerPoolMinSize().toString()}/` : ''}${Bootstrap.workerImplementation.size}${Utils.workerPoolInUse() ? `/${Configuration.getWorkerPoolMaxSize().toString()}` : ''} worker(s) concurrently running in '${Configuration.getWorkerProcess()}' mode${Bootstrap.workerImplementation.maxElementsPerWorker ? ` (${Bootstrap.workerImplementation.maxElementsPerWorker} charging station(s) per worker)` : ''}`);
ded13d97 60 }
eb87fe87 61 this.started = true;
ded13d97 62 } catch (error) {
ded13d97
JB
63 console.error('Bootstrap start error ', error);
64 }
65 }
66 }
67
68 public async stop(): Promise<void> {
eb87fe87 69 if (isMainThread && this.started) {
535aaa27 70 await Bootstrap.workerImplementation.stop();
ded13d97 71 }
eb87fe87 72 this.started = false;
ded13d97
JB
73 }
74
75 public async restart(): Promise<void> {
76 await this.stop();
535aaa27 77 this.initWorkerImplementation();
ded13d97
JB
78 await this.start();
79 }
80
535aaa27
JB
81 private initWorkerImplementation() {
82 Bootstrap.workerImplementation = WorkerFactory.getWorkerImplementation<StationWorkerData>(this.workerScript, Configuration.getWorkerProcess(),
8df3f0a9
JB
83 {
84 startDelay: Configuration.getWorkerStartDelay(),
85 poolMaxSize: Configuration.getWorkerPoolMaxSize(),
86 poolMinSize: Configuration.getWorkerPoolMinSize(),
87 elementsPerWorker: Configuration.getChargingStationsPerWorker(),
88 poolOptions: {
89 workerChoiceStrategy: Configuration.getWorkerPoolStrategy()
90 }
535aaa27
JB
91 });
92 if (!Bootstrap.workerImplementation) {
8df3f0a9 93 throw new Error('Worker implementation not found');
ded13d97 94 }
ded13d97
JB
95 }
96}