Add some version handling automation
[e-mobility-charging-stations-simulator.git] / src / charging-station / Bootstrap.ts
1 import Configuration from '../utils/Configuration';
2 import { StationWorkerData } from '../types/Worker';
3 import Utils from '../utils/Utils';
4 import WorkerAbstract from '../worker/WorkerAbstract';
5 import WorkerFactory from '../worker/WorkerFactory';
6 import { isMainThread } from 'worker_threads';
7 import path from 'path';
8 import { version } from '../../package.json';
9
10 export default class Bootstrap {
11 private static instance: Bootstrap;
12 private version: string = version as string;
13 private started: boolean;
14 private workerScript: string;
15 private workerImplementationInstance: WorkerAbstract | null = null;
16
17 private constructor() {
18 this.started = false;
19 this.workerScript = path.join(path.resolve(__dirname, '../'), 'charging-station', 'StationWorker.js');
20 Configuration.setConfigurationChangeCallback(async () => this.restart());
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> {
31 if (isMainThread && !this.started) {
32 try {
33 let numStationsTotal = 0;
34 await this.getWorkerImplementationInstance()?.start();
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,
43 templateFile: path.join(path.resolve(__dirname, '../'), 'assets', 'station-templates', path.basename(stationURL.file))
44 };
45 await this.getWorkerImplementationInstance()?.addElement(workerData);
46 numStationsTotal++;
47 }
48 } catch (error) {
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 {
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)` : ''}`);
59 }
60 this.started = true;
61 } catch (error) {
62 console.error('Bootstrap start error ', error);
63 }
64 }
65 }
66
67 public async stop(): Promise<void> {
68 if (isMainThread && this.started) {
69 await this.getWorkerImplementationInstance()?.stop();
70 // Nullify to force worker implementation instance creation
71 this.workerImplementationInstance = null;
72 }
73 this.started = false;
74 }
75
76 public async restart(): Promise<void> {
77 await this.stop();
78 await this.start();
79 }
80
81 private getWorkerImplementationInstance(): WorkerAbstract | null {
82 if (!this.workerImplementationInstance) {
83 this.workerImplementationInstance = WorkerFactory.getWorkerImplementation<StationWorkerData>(this.workerScript, Configuration.getWorkerProcess(),
84 {
85 startDelay: Configuration.getWorkerStartDelay(),
86 poolMaxSize: Configuration.getWorkerPoolMaxSize(),
87 poolMinSize: Configuration.getWorkerPoolMinSize(),
88 elementsPerWorker: Configuration.getChargingStationsPerWorker(),
89 poolOptions: {
90 workerChoiceStrategy: Configuration.getWorkerPoolStrategy()
91 }
92 });
93 }
94 return this.workerImplementationInstance;
95 }
96 }