Prepare code for strict type checking
[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';
ded13d97
JB
8
9export default class Bootstrap {
10 private static instance: Bootstrap;
eb87fe87 11 private started: boolean;
ded13d97 12 private workerScript: string;
6e0964c8 13 private workerImplementationInstance: WorkerAbstract | null = null;
ded13d97
JB
14
15 private constructor() {
eb87fe87 16 this.started = false;
bf1866b2 17 this.workerScript = path.join(path.resolve(__dirname, '../'), 'charging-station', 'StationWorker.js');
e57acf6a 18 Configuration.setConfigurationChangeCallback(async () => this.restart());
ded13d97
JB
19 }
20
21 public static getInstance(): Bootstrap {
22 if (!Bootstrap.instance) {
23 Bootstrap.instance = new Bootstrap();
24 }
25 return Bootstrap.instance;
26 }
27
28 public async start(): Promise<void> {
eb87fe87 29 if (isMainThread && !this.started) {
ded13d97
JB
30 try {
31 let numStationsTotal = 0;
6e0964c8 32 await this.getWorkerImplementationInstance()?.start();
ded13d97
JB
33 // Start ChargingStation object in worker thread
34 if (Configuration.getStationTemplateURLs()) {
35 for (const stationURL of Configuration.getStationTemplateURLs()) {
36 try {
37 const nbStations = stationURL.numberOfStations ? stationURL.numberOfStations : 0;
38 for (let index = 1; index <= nbStations; index++) {
39 const workerData: StationWorkerData = {
40 index,
bf1866b2 41 templateFile: path.join(path.resolve(__dirname, '../'), 'assets', 'station-templates', path.basename(stationURL.file))
ded13d97 42 };
6e0964c8 43 await this.getWorkerImplementationInstance()?.addElement(workerData);
ded13d97
JB
44 numStationsTotal++;
45 }
46 } catch (error) {
ded13d97
JB
47 console.error('Charging station start with template file ' + stationURL.file + ' error ', error);
48 }
49 }
50 } else {
51 console.log('No stationTemplateURLs defined in configuration, exiting');
52 }
53 if (numStationsTotal === 0) {
54 console.log('No charging station template enabled in configuration, exiting');
55 } else {
85f78bc0 56 console.log(`Charging station simulator started with ${numStationsTotal.toString()} charging station(s) and ${Utils.workerDynamicPoolInUse() ? `${Configuration.getWorkerPoolMinSize().toString()}/` : ''}${this.getWorkerImplementationInstance().size}${Utils.workerPoolInUse() ? `/${Configuration.getWorkerPoolMaxSize().toString()}` : ''} worker(s) concurrently running in '${Configuration.getWorkerProcess()}' mode${this.getWorkerImplementationInstance().maxElementsPerWorker ? ` (${this.getWorkerImplementationInstance().maxElementsPerWorker} charging station(s) per worker)` : ''}`);
ded13d97 57 }
eb87fe87 58 this.started = true;
ded13d97 59 } catch (error) {
ded13d97
JB
60 console.error('Bootstrap start error ', error);
61 }
62 }
63 }
64
65 public async stop(): Promise<void> {
eb87fe87 66 if (isMainThread && this.started) {
6e0964c8 67 await this.getWorkerImplementationInstance()?.stop();
404f9c66
JB
68 // Nullify to force worker implementation instance creation
69 this.workerImplementationInstance = null;
ded13d97 70 }
eb87fe87 71 this.started = false;
ded13d97
JB
72 }
73
74 public async restart(): Promise<void> {
75 await this.stop();
76 await this.start();
77 }
78
6e0964c8 79 private getWorkerImplementationInstance(): WorkerAbstract | null {
adeb9b56 80 if (!this.workerImplementationInstance) {
322c9192
JB
81 this.workerImplementationInstance = WorkerFactory.getWorkerImplementation<StationWorkerData>(this.workerScript, Configuration.getWorkerProcess(),
82 {
83 startDelay: Configuration.getWorkerStartDelay(),
84 poolMaxSize: Configuration.getWorkerPoolMaxSize(),
85 poolMinSize: Configuration.getWorkerPoolMinSize(),
9efbac5b
JB
86 elementsPerWorker: Configuration.getChargingStationsPerWorker(),
87 poolOptions: {
88 workerChoiceStrategy: Configuration.getWorkerPoolStrategy()
89 }
322c9192 90 });
ded13d97 91 }
adeb9b56 92 return this.workerImplementationInstance;
ded13d97
JB
93 }
94}