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