Commit | Line | Data |
---|---|---|
b4d34251 JB |
1 | // Partial Copyright Jerome Benoit. 2021. All Rights Reserved. |
2 | ||
e7aeea18 JB |
3 | import { |
4 | ChargingStationWorkerData, | |
5 | ChargingStationWorkerMessage, | |
6 | ChargingStationWorkerMessageEvents, | |
7 | } from '../types/ChargingStationWorker'; | |
81797102 | 8 | |
ded13d97 | 9 | import Configuration from '../utils/Configuration'; |
717c1e56 | 10 | import { StationTemplateUrl } from '../types/ConfigurationData'; |
c3ee95af | 11 | import Statistics from '../types/Statistics'; |
a6b3c6c3 JB |
12 | import { Storage } from '../performance/storage/Storage'; |
13 | import { StorageFactory } from '../performance/storage/StorageFactory'; | |
675fa8e3 JB |
14 | import { UIServiceUtils } from './ui-server/ui-services/UIServiceUtils'; |
15 | import UIWebSocketServer from './ui-server/UIWebSocketServer'; | |
ded13d97 | 16 | import Utils from '../utils/Utils'; |
fd1fdf1b | 17 | import WorkerAbstract from '../worker/WorkerAbstract'; |
ded13d97 | 18 | import WorkerFactory from '../worker/WorkerFactory'; |
8eac9a09 | 19 | import chalk from 'chalk'; |
ded13d97 | 20 | import { isMainThread } from 'worker_threads'; |
bf1866b2 | 21 | import path from 'path'; |
84e52c2c | 22 | import { version } from '../../package.json'; |
ded13d97 JB |
23 | |
24 | export default class Bootstrap { | |
535aaa27 | 25 | private static instance: Bootstrap | null = null; |
c3ee95af | 26 | private workerImplementation: WorkerAbstract<ChargingStationWorkerData> | null = null; |
675fa8e3 | 27 | private readonly uiServer!: UIWebSocketServer; |
6a49ad23 | 28 | private readonly storage!: Storage; |
a4bc2942 | 29 | private numberOfChargingStations: number; |
9e23580d | 30 | private readonly version: string = version; |
eb87fe87 | 31 | private started: boolean; |
9e23580d | 32 | private readonly workerScript: string; |
ded13d97 JB |
33 | |
34 | private constructor() { | |
eb87fe87 | 35 | this.started = false; |
e7aeea18 JB |
36 | this.workerScript = path.join( |
37 | path.resolve(__dirname, '../'), | |
38 | 'charging-station', | |
39 | 'ChargingStationWorker.js' | |
40 | ); | |
8df3f0a9 | 41 | this.initWorkerImplementation(); |
675fa8e3 JB |
42 | Configuration.getUIServer().enabled && |
43 | (this.uiServer = new UIWebSocketServer({ | |
44 | ...Configuration.getUIServer().options, | |
e7aeea18 JB |
45 | handleProtocols: UIServiceUtils.handleProtocols, |
46 | })); | |
47 | Configuration.getPerformanceStorage().enabled && | |
48 | (this.storage = StorageFactory.getStorage( | |
49 | Configuration.getPerformanceStorage().type, | |
50 | Configuration.getPerformanceStorage().uri, | |
51 | this.logPrefix() | |
52 | )); | |
7874b0b1 | 53 | Configuration.setConfigurationChangeCallback(async () => Bootstrap.getInstance().restart()); |
ded13d97 JB |
54 | } |
55 | ||
56 | public static getInstance(): Bootstrap { | |
57 | if (!Bootstrap.instance) { | |
58 | Bootstrap.instance = new Bootstrap(); | |
59 | } | |
60 | return Bootstrap.instance; | |
61 | } | |
62 | ||
63 | public async start(): Promise<void> { | |
eb87fe87 | 64 | if (isMainThread && !this.started) { |
ded13d97 | 65 | try { |
a4bc2942 | 66 | this.numberOfChargingStations = 0; |
6a49ad23 | 67 | await this.storage?.open(); |
a4bc2942 | 68 | await this.workerImplementation.start(); |
675fa8e3 | 69 | this.uiServer?.start(); |
1f5df42a | 70 | const stationTemplateUrls = Configuration.getStationTemplateUrls(); |
ded13d97 | 71 | // Start ChargingStation object in worker thread |
1f5df42a JB |
72 | if (stationTemplateUrls) { |
73 | for (const stationTemplateUrl of stationTemplateUrls) { | |
ded13d97 | 74 | try { |
1f5df42a | 75 | const nbStations = stationTemplateUrl.numberOfStations ?? 0; |
ded13d97 | 76 | for (let index = 1; index <= nbStations; index++) { |
717c1e56 | 77 | await this.startChargingStation(index, stationTemplateUrl); |
ded13d97 JB |
78 | } |
79 | } catch (error) { | |
e7aeea18 JB |
80 | console.error( |
81 | chalk.red( | |
3d25cc86 JB |
82 | 'Error at starting charging station with template file ' + |
83 | stationTemplateUrl.file + | |
84 | ': ' | |
e7aeea18 JB |
85 | ), |
86 | error | |
87 | ); | |
ded13d97 JB |
88 | } |
89 | } | |
90 | } else { | |
3d25cc86 | 91 | console.warn(chalk.yellow("No 'stationTemplateUrls' defined in configuration, exiting")); |
ded13d97 | 92 | } |
a4bc2942 | 93 | if (this.numberOfChargingStations === 0) { |
e7aeea18 JB |
94 | console.warn( |
95 | chalk.yellow('No charging station template enabled in configuration, exiting') | |
96 | ); | |
ded13d97 | 97 | } else { |
e7aeea18 JB |
98 | console.log( |
99 | chalk.green( | |
100 | `Charging stations simulator ${ | |
101 | this.version | |
102 | } started with ${this.numberOfChargingStations.toString()} charging station(s) and ${ | |
103 | Utils.workerDynamicPoolInUse() | |
104 | ? `${Configuration.getWorkerPoolMinSize().toString()}/` | |
105 | : '' | |
106 | }${this.workerImplementation.size}${ | |
107 | Utils.workerPoolInUse() ? `/${Configuration.getWorkerPoolMaxSize().toString()}` : '' | |
108 | } worker(s) concurrently running in '${Configuration.getWorkerProcess()}' mode${ | |
109 | this.workerImplementation.maxElementsPerWorker | |
110 | ? ` (${this.workerImplementation.maxElementsPerWorker} charging station(s) per worker)` | |
111 | : '' | |
112 | }` | |
113 | ) | |
114 | ); | |
ded13d97 | 115 | } |
eb87fe87 | 116 | this.started = true; |
ded13d97 | 117 | } catch (error) { |
8eac9a09 | 118 | console.error(chalk.red('Bootstrap start error '), error); |
ded13d97 | 119 | } |
b322b8b4 JB |
120 | } else { |
121 | console.error(chalk.red('Cannot start an already started charging stations simulator')); | |
ded13d97 JB |
122 | } |
123 | } | |
124 | ||
125 | public async stop(): Promise<void> { | |
eb87fe87 | 126 | if (isMainThread && this.started) { |
a4bc2942 | 127 | await this.workerImplementation.stop(); |
675fa8e3 | 128 | this.uiServer?.stop(); |
6a49ad23 | 129 | await this.storage?.close(); |
b322b8b4 JB |
130 | } else { |
131 | console.error(chalk.red('Trying to stop the charging stations simulator while not started')); | |
ded13d97 | 132 | } |
eb87fe87 | 133 | this.started = false; |
ded13d97 JB |
134 | } |
135 | ||
136 | public async restart(): Promise<void> { | |
137 | await this.stop(); | |
535aaa27 | 138 | this.initWorkerImplementation(); |
ded13d97 JB |
139 | await this.start(); |
140 | } | |
141 | ||
2a370053 | 142 | private initWorkerImplementation(): void { |
e7aeea18 JB |
143 | this.workerImplementation = WorkerFactory.getWorkerImplementation<ChargingStationWorkerData>( |
144 | this.workerScript, | |
145 | Configuration.getWorkerProcess(), | |
8df3f0a9 | 146 | { |
4bfd80fa JB |
147 | workerStartDelay: Configuration.getWorkerStartDelay(), |
148 | elementStartDelay: Configuration.getElementStartDelay(), | |
8df3f0a9 JB |
149 | poolMaxSize: Configuration.getWorkerPoolMaxSize(), |
150 | poolMinSize: Configuration.getWorkerPoolMinSize(), | |
151 | elementsPerWorker: Configuration.getChargingStationsPerWorker(), | |
152 | poolOptions: { | |
e7aeea18 | 153 | workerChoiceStrategy: Configuration.getWorkerPoolStrategy(), |
ffd71f2c | 154 | }, |
98dc07fa | 155 | messageHandler: async (msg: ChargingStationWorkerMessage) => { |
ee0f106b | 156 | if (msg.id === ChargingStationWorkerMessageEvents.STARTED) { |
675fa8e3 | 157 | this.uiServer.chargingStations.add(msg.data.id as string); |
ee0f106b | 158 | } else if (msg.id === ChargingStationWorkerMessageEvents.STOPPED) { |
675fa8e3 | 159 | this.uiServer.chargingStations.delete(msg.data.id as string); |
ee0f106b | 160 | } else if (msg.id === ChargingStationWorkerMessageEvents.PERFORMANCE_STATISTICS) { |
c3ee95af | 161 | await this.storage.storePerformanceStatistics(msg.data as unknown as Statistics); |
ffd71f2c | 162 | } |
e7aeea18 JB |
163 | }, |
164 | } | |
165 | ); | |
ded13d97 | 166 | } |
81797102 | 167 | |
e7aeea18 JB |
168 | private async startChargingStation( |
169 | index: number, | |
170 | stationTemplateUrl: StationTemplateUrl | |
171 | ): Promise<void> { | |
717c1e56 JB |
172 | const workerData: ChargingStationWorkerData = { |
173 | index, | |
e7aeea18 JB |
174 | templateFile: path.join( |
175 | path.resolve(__dirname, '../'), | |
176 | 'assets', | |
177 | 'station-templates', | |
178 | path.basename(stationTemplateUrl.file) | |
179 | ), | |
717c1e56 JB |
180 | }; |
181 | await this.workerImplementation.addElement(workerData); | |
182 | this.numberOfChargingStations++; | |
183 | } | |
184 | ||
81797102 | 185 | private logPrefix(): string { |
689dca78 | 186 | return Utils.logPrefix(' Bootstrap |'); |
81797102 | 187 | } |
ded13d97 | 188 | } |