Small cleanups
[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;
fd1fdf1b 13 private workerImplementationInstance: WorkerAbstract;
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;
adeb9b56 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 };
adeb9b56 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) {
adeb9b56 67 if (this.getWorkerImplementationInstance()) {
967511dc 68 await this.getWorkerImplementationInstance().stop();
ded13d97 69 // Nullify to force worker implementation instance creation
adeb9b56 70 this.workerImplementationInstance = null;
ded13d97
JB
71 }
72 }
eb87fe87 73 this.started = false;
ded13d97
JB
74 }
75
76 public async restart(): Promise<void> {
77 await this.stop();
78 await this.start();
79 }
80
fd1fdf1b 81 private getWorkerImplementationInstance(): WorkerAbstract {
adeb9b56
JB
82 if (!this.workerImplementationInstance) {
83 this.workerImplementationInstance = WorkerFactory.getWorkerImplementation<StationWorkerData>(this.workerScript, Configuration.getWorkerProcess(), {
ded13d97
JB
84 poolMaxSize: Configuration.getWorkerPoolMaxSize(),
85 poolMinSize: Configuration.getWorkerPoolMinSize(),
86 elementsPerWorker: Configuration.getChargingStationsPerWorker()
87 });
88 }
adeb9b56 89 return this.workerImplementationInstance;
ded13d97
JB
90 }
91}