Commit | Line | Data |
---|---|---|
b4d34251 JB |
1 | // Partial Copyright Jerome Benoit. 2021. All Rights Reserved. |
2 | ||
98dc07fa | 3 | import { ChargingStationWorkerData, ChargingStationWorkerMessage, ChargingStationWorkerMessageEvents } from '../types/ChargingStationWorker'; |
81797102 | 4 | |
ded13d97 | 5 | import Configuration from '../utils/Configuration'; |
a6b3c6c3 JB |
6 | import { Storage } from '../performance/storage/Storage'; |
7 | import { StorageFactory } from '../performance/storage/StorageFactory'; | |
4198ad5c JB |
8 | import { UIServiceUtils } from './UIWebSocketServices/UIServiceUtils'; |
9 | import UIWebSocketServer from './UIWebSocketServer'; | |
ded13d97 | 10 | import Utils from '../utils/Utils'; |
fd1fdf1b | 11 | import WorkerAbstract from '../worker/WorkerAbstract'; |
ded13d97 | 12 | import WorkerFactory from '../worker/WorkerFactory'; |
8eac9a09 | 13 | import chalk from 'chalk'; |
ded13d97 | 14 | import { isMainThread } from 'worker_threads'; |
bf1866b2 | 15 | import path from 'path'; |
84e52c2c | 16 | import { version } from '../../package.json'; |
ded13d97 JB |
17 | |
18 | export default class Bootstrap { | |
535aaa27 | 19 | private static instance: Bootstrap | null = null; |
a4bc2942 | 20 | private workerImplementation: WorkerAbstract | null = null; |
6a49ad23 JB |
21 | private readonly uiWebSocketServer!: UIWebSocketServer; |
22 | private readonly storage!: Storage; | |
a4bc2942 | 23 | private numberOfChargingStations: number; |
9e23580d | 24 | private readonly version: string = version; |
eb87fe87 | 25 | private started: boolean; |
9e23580d | 26 | private readonly workerScript: string; |
ded13d97 JB |
27 | |
28 | private constructor() { | |
eb87fe87 | 29 | this.started = false; |
07f35004 | 30 | this.workerScript = path.join(path.resolve(__dirname, '../'), 'charging-station', 'ChargingStationWorker.js'); |
8df3f0a9 | 31 | this.initWorkerImplementation(); |
6a49ad23 JB |
32 | Configuration.getUIWebSocketServer().enabled && (this.uiWebSocketServer = new UIWebSocketServer({ |
33 | ...Configuration.getUIWebSocketServer().options, handleProtocols: UIServiceUtils.handleProtocols | |
34 | })); | |
35 | Configuration.getPerformanceStorage().enabled && (this.storage = StorageFactory.getStorage( | |
36 | Configuration.getPerformanceStorage().type, | |
37 | Configuration.getPerformanceStorage().URI, | |
38 | this.logPrefix() | |
39 | )); | |
7874b0b1 | 40 | Configuration.setConfigurationChangeCallback(async () => Bootstrap.getInstance().restart()); |
ded13d97 JB |
41 | } |
42 | ||
43 | public static getInstance(): Bootstrap { | |
44 | if (!Bootstrap.instance) { | |
45 | Bootstrap.instance = new Bootstrap(); | |
46 | } | |
47 | return Bootstrap.instance; | |
48 | } | |
49 | ||
50 | public async start(): Promise<void> { | |
eb87fe87 | 51 | if (isMainThread && !this.started) { |
ded13d97 | 52 | try { |
a4bc2942 | 53 | this.numberOfChargingStations = 0; |
6a49ad23 | 54 | await this.storage?.open(); |
a4bc2942 | 55 | await this.workerImplementation.start(); |
6a49ad23 | 56 | this.uiWebSocketServer?.start(); |
ded13d97 JB |
57 | // Start ChargingStation object in worker thread |
58 | if (Configuration.getStationTemplateURLs()) { | |
59 | for (const stationURL of Configuration.getStationTemplateURLs()) { | |
60 | try { | |
c63c21bc | 61 | const nbStations = stationURL.numberOfStations ?? 0; |
ded13d97 | 62 | for (let index = 1; index <= nbStations; index++) { |
07f35004 | 63 | const workerData: ChargingStationWorkerData = { |
ded13d97 | 64 | index, |
bf1866b2 | 65 | templateFile: path.join(path.resolve(__dirname, '../'), 'assets', 'station-templates', path.basename(stationURL.file)) |
ded13d97 | 66 | }; |
a4bc2942 JB |
67 | await this.workerImplementation.addElement(workerData); |
68 | this.numberOfChargingStations++; | |
ded13d97 JB |
69 | } |
70 | } catch (error) { | |
8eac9a09 | 71 | console.error(chalk.red('Charging station start with template file ' + stationURL.file + ' error '), error); |
ded13d97 JB |
72 | } |
73 | } | |
74 | } else { | |
8eac9a09 | 75 | console.warn(chalk.yellow('No stationTemplateURLs defined in configuration, exiting')); |
ded13d97 | 76 | } |
a4bc2942 | 77 | if (this.numberOfChargingStations === 0) { |
8eac9a09 | 78 | console.warn(chalk.yellow('No charging station template enabled in configuration, exiting')); |
ded13d97 | 79 | } else { |
a4bc2942 | 80 | console.log(chalk.green(`Charging stations simulator ${this.version} started with ${this.numberOfChargingStations.toString()} charging station(s) and ${Utils.workerDynamicPoolInUse() ? `${Configuration.getWorkerPoolMinSize().toString()}/` : ''}${this.workerImplementation.size}${Utils.workerPoolInUse() ? `/${Configuration.getWorkerPoolMaxSize().toString()}` : ''} worker(s) concurrently running in '${Configuration.getWorkerProcess()}' mode${this.workerImplementation.maxElementsPerWorker ? ` (${this.workerImplementation.maxElementsPerWorker} charging station(s) per worker)` : ''}`)); |
ded13d97 | 81 | } |
eb87fe87 | 82 | this.started = true; |
ded13d97 | 83 | } catch (error) { |
8eac9a09 | 84 | console.error(chalk.red('Bootstrap start error '), error); |
ded13d97 | 85 | } |
b322b8b4 JB |
86 | } else { |
87 | console.error(chalk.red('Cannot start an already started charging stations simulator')); | |
ded13d97 JB |
88 | } |
89 | } | |
90 | ||
91 | public async stop(): Promise<void> { | |
eb87fe87 | 92 | if (isMainThread && this.started) { |
a4bc2942 | 93 | await this.workerImplementation.stop(); |
6a49ad23 JB |
94 | this.uiWebSocketServer?.stop(); |
95 | await this.storage?.close(); | |
b322b8b4 JB |
96 | } else { |
97 | console.error(chalk.red('Trying to stop the charging stations simulator while not started')); | |
ded13d97 | 98 | } |
eb87fe87 | 99 | this.started = false; |
ded13d97 JB |
100 | } |
101 | ||
102 | public async restart(): Promise<void> { | |
103 | await this.stop(); | |
535aaa27 | 104 | this.initWorkerImplementation(); |
ded13d97 JB |
105 | await this.start(); |
106 | } | |
107 | ||
2a370053 | 108 | private initWorkerImplementation(): void { |
a4bc2942 | 109 | this.workerImplementation = WorkerFactory.getWorkerImplementation<ChargingStationWorkerData>(this.workerScript, Configuration.getWorkerProcess(), |
8df3f0a9 JB |
110 | { |
111 | startDelay: Configuration.getWorkerStartDelay(), | |
112 | poolMaxSize: Configuration.getWorkerPoolMaxSize(), | |
113 | poolMinSize: Configuration.getWorkerPoolMinSize(), | |
114 | elementsPerWorker: Configuration.getChargingStationsPerWorker(), | |
115 | poolOptions: { | |
116 | workerChoiceStrategy: Configuration.getWorkerPoolStrategy() | |
ffd71f2c | 117 | }, |
98dc07fa | 118 | messageHandler: async (msg: ChargingStationWorkerMessage) => { |
ee0f106b | 119 | if (msg.id === ChargingStationWorkerMessageEvents.STARTED) { |
de9136ae | 120 | this.uiWebSocketServer.chargingStations.add(msg.data.id); |
ee0f106b | 121 | } else if (msg.id === ChargingStationWorkerMessageEvents.STOPPED) { |
de9136ae | 122 | this.uiWebSocketServer.chargingStations.delete(msg.data.id); |
ee0f106b | 123 | } else if (msg.id === ChargingStationWorkerMessageEvents.PERFORMANCE_STATISTICS) { |
a4bc2942 | 124 | await this.storage.storePerformanceStatistics(msg.data); |
ffd71f2c | 125 | } |
81797102 | 126 | } |
535aaa27 | 127 | }); |
ded13d97 | 128 | } |
81797102 JB |
129 | |
130 | private logPrefix(): string { | |
689dca78 | 131 | return Utils.logPrefix(' Bootstrap |'); |
81797102 | 132 | } |
ded13d97 | 133 | } |