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