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