e329a65b4e32ffa1018aa6b01a35b32ffacd0649
[e-mobility-charging-stations-simulator.git] / src / charging-station / Bootstrap.ts
1 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
2
3 import { EventEmitter } from 'node:events';
4 import { dirname, extname, join } from 'node:path';
5 import { fileURLToPath } from 'node:url';
6 import { isMainThread } from 'node:worker_threads';
7
8 import chalk from 'chalk';
9
10 import { waitForChargingStationEvents } from './ChargingStationUtils';
11 import type { AbstractUIServer } from './ui-server/AbstractUIServer';
12 import { UIServerFactory } from './ui-server/UIServerFactory';
13 import { version } from '../../package.json' assert { type: 'json' };
14 import { BaseError } from '../exception';
15 import { type Storage, StorageFactory } from '../performance';
16 import {
17 type ChargingStationData,
18 type ChargingStationWorkerData,
19 type ChargingStationWorkerMessage,
20 type ChargingStationWorkerMessageData,
21 ChargingStationWorkerMessageEvents,
22 ProcedureName,
23 type StationTemplateUrl,
24 type Statistics,
25 } from '../types';
26 import {
27 Configuration,
28 Constants,
29 formatDurationMilliSeconds,
30 generateUUID,
31 handleUncaughtException,
32 handleUnhandledRejection,
33 isNotEmptyArray,
34 isNullOrUndefined,
35 logPrefix,
36 logger,
37 } from '../utils';
38 import { type WorkerAbstract, WorkerFactory } from '../worker';
39
40 const moduleName = 'Bootstrap';
41
42 enum exitCodes {
43 missingChargingStationsConfiguration = 1,
44 noChargingStationTemplates = 2,
45 }
46
47 export class Bootstrap extends EventEmitter {
48 private static instance: Bootstrap | null = null;
49 public numberOfChargingStations!: number;
50 public numberOfChargingStationTemplates!: number;
51 private workerImplementation: WorkerAbstract<ChargingStationWorkerData> | null;
52 private readonly uiServer!: AbstractUIServer | null;
53 private readonly storage!: Storage;
54 private numberOfStartedChargingStations!: number;
55 private readonly version: string = version;
56 private initializedCounters: boolean;
57 private started: boolean;
58 private starting: boolean;
59 private stopping: boolean;
60 private readonly workerScript: string;
61
62 private constructor() {
63 super();
64 for (const signal of ['SIGINT', 'SIGQUIT', 'SIGTERM']) {
65 process.on(signal, this.gracefulShutdown);
66 }
67 // Enable unconditionally for now
68 handleUnhandledRejection();
69 handleUncaughtException();
70 this.started = false;
71 this.starting = false;
72 this.stopping = false;
73 this.initializedCounters = false;
74 this.initializeCounters();
75 this.workerImplementation = null;
76 this.workerScript = join(
77 dirname(fileURLToPath(import.meta.url)),
78 `ChargingStationWorker${extname(fileURLToPath(import.meta.url))}`
79 );
80 Configuration.getUIServer().enabled === true &&
81 (this.uiServer = UIServerFactory.getUIServerImplementation(Configuration.getUIServer()));
82 Configuration.getPerformanceStorage().enabled === true &&
83 (this.storage = StorageFactory.getStorage(
84 Configuration.getPerformanceStorage().type,
85 Configuration.getPerformanceStorage().uri,
86 this.logPrefix()
87 ));
88 Configuration.setConfigurationChangeCallback(async () => Bootstrap.getInstance().restart());
89 }
90
91 public static getInstance(): Bootstrap {
92 if (Bootstrap.instance === null) {
93 Bootstrap.instance = new Bootstrap();
94 }
95 return Bootstrap.instance;
96 }
97
98 public async start(): Promise<void> {
99 if (!isMainThread) {
100 throw new Error('Cannot start charging stations simulator from worker thread');
101 }
102 if (this.started === false) {
103 if (this.starting === false) {
104 this.starting = true;
105 this.initializeCounters();
106 this.initializeWorkerImplementation();
107 await this.workerImplementation?.start();
108 await this.storage?.open();
109 this.uiServer?.start();
110 // Start ChargingStation object instance in worker thread
111 for (const stationTemplateUrl of Configuration.getStationTemplateUrls()) {
112 try {
113 const nbStations = stationTemplateUrl.numberOfStations ?? 0;
114 for (let index = 1; index <= nbStations; index++) {
115 await this.startChargingStation(index, stationTemplateUrl);
116 }
117 } catch (error) {
118 console.error(
119 chalk.red(
120 `Error at starting charging station with template file ${stationTemplateUrl.file}: `
121 ),
122 error
123 );
124 }
125 }
126 console.info(
127 chalk.green(
128 `Charging stations simulator ${
129 this.version
130 } started with ${this.numberOfChargingStations.toString()} charging station(s) from ${this.numberOfChargingStationTemplates.toString()} configured charging station template(s) and ${
131 Configuration.workerDynamicPoolInUse()
132 ? `${Configuration.getWorker().poolMinSize?.toString()}/`
133 : ''
134 }${this.workerImplementation?.size}${
135 Configuration.workerPoolInUse()
136 ? `/${Configuration.getWorker().poolMaxSize?.toString()}`
137 : ''
138 } worker(s) concurrently running in '${Configuration.getWorker().processType}' mode${
139 !isNullOrUndefined(this.workerImplementation?.maxElementsPerWorker)
140 ? ` (${this.workerImplementation?.maxElementsPerWorker} charging station(s) per worker)`
141 : ''
142 }`
143 )
144 );
145 console.info(chalk.green('Worker set/pool information:'), this.workerImplementation?.info);
146 this.started = true;
147 this.starting = false;
148 } else {
149 console.error(chalk.red('Cannot start an already starting charging stations simulator'));
150 }
151 } else {
152 console.error(chalk.red('Cannot start an already started charging stations simulator'));
153 }
154 }
155
156 public async stop(): Promise<void> {
157 if (!isMainThread) {
158 throw new Error('Cannot stop charging stations simulator from worker thread');
159 }
160 if (this.started === true) {
161 if (this.stopping === false) {
162 this.stopping = true;
163 await this.uiServer?.sendInternalRequest(
164 this.uiServer.buildProtocolRequest(
165 generateUUID(),
166 ProcedureName.STOP_CHARGING_STATION,
167 Constants.EMPTY_FREEZED_OBJECT
168 )
169 );
170 await Promise.race([
171 waitForChargingStationEvents(
172 this,
173 ChargingStationWorkerMessageEvents.stopped,
174 this.numberOfChargingStations
175 ),
176 new Promise<string>((resolve) => {
177 setTimeout(() => {
178 const message = `Timeout reached ${formatDurationMilliSeconds(
179 Constants.STOP_SIMULATOR_TIMEOUT
180 )} at stopping charging stations simulator`;
181 console.warn(chalk.yellow(message));
182 resolve(message);
183 }, Constants.STOP_SIMULATOR_TIMEOUT);
184 }),
185 ]);
186 await this.workerImplementation?.stop();
187 this.workerImplementation = null;
188 this.uiServer?.stop();
189 await this.storage?.close();
190 this.resetCounters();
191 this.initializedCounters = false;
192 this.started = false;
193 this.stopping = false;
194 } else {
195 console.error(chalk.red('Cannot stop an already stopping charging stations simulator'));
196 }
197 } else {
198 console.error(chalk.red('Cannot stop an already stopped charging stations simulator'));
199 }
200 }
201
202 public async restart(): Promise<void> {
203 await this.stop();
204 await this.start();
205 }
206
207 private initializeWorkerImplementation(): void {
208 this.workerImplementation === null &&
209 (this.workerImplementation = WorkerFactory.getWorkerImplementation<ChargingStationWorkerData>(
210 this.workerScript,
211 Configuration.getWorker().processType,
212 {
213 workerStartDelay: Configuration.getWorker().startDelay,
214 elementStartDelay: Configuration.getWorker().elementStartDelay,
215 poolMaxSize: Configuration.getWorker().poolMaxSize,
216 poolMinSize: Configuration.getWorker().poolMinSize,
217 elementsPerWorker: Configuration.getWorker().elementsPerWorker,
218 poolOptions: {
219 workerChoiceStrategy: Configuration.getWorker().poolStrategy,
220 messageHandler: this.messageHandler.bind(this) as (message: unknown) => void,
221 },
222 }
223 ));
224 }
225
226 private messageHandler(
227 msg: ChargingStationWorkerMessage<ChargingStationWorkerMessageData>
228 ): void {
229 // logger.debug(
230 // `${this.logPrefix()} ${moduleName}.messageHandler: Worker channel message received: ${JSON.stringify(
231 // msg,
232 // null,
233 // 2
234 // )}`
235 // );
236 try {
237 switch (msg.id) {
238 case ChargingStationWorkerMessageEvents.started:
239 this.workerEventStarted(msg.data as ChargingStationData);
240 this.emit(ChargingStationWorkerMessageEvents.started, msg.data as ChargingStationData);
241 break;
242 case ChargingStationWorkerMessageEvents.stopped:
243 this.workerEventStopped(msg.data as ChargingStationData);
244 this.emit(ChargingStationWorkerMessageEvents.stopped, msg.data as ChargingStationData);
245 break;
246 case ChargingStationWorkerMessageEvents.updated:
247 this.workerEventUpdated(msg.data as ChargingStationData);
248 this.emit(ChargingStationWorkerMessageEvents.updated, msg.data as ChargingStationData);
249 break;
250 case ChargingStationWorkerMessageEvents.performanceStatistics:
251 this.workerEventPerformanceStatistics(msg.data as Statistics);
252 this.emit(
253 ChargingStationWorkerMessageEvents.performanceStatistics,
254 msg.data as Statistics
255 );
256 break;
257 default:
258 throw new BaseError(
259 `Unknown event type: '${msg.id}' for data: ${JSON.stringify(msg.data, null, 2)}`
260 );
261 }
262 } catch (error) {
263 logger.error(
264 `${this.logPrefix()} ${moduleName}.messageHandler: Error occurred while handling '${
265 msg.id
266 }' event:`,
267 error
268 );
269 }
270 }
271
272 private workerEventStarted = (data: ChargingStationData) => {
273 this.uiServer?.chargingStations.set(data.stationInfo.hashId, data);
274 ++this.numberOfStartedChargingStations;
275 logger.info(
276 `${this.logPrefix()} ${moduleName}.workerEventStarted: Charging station ${
277 data.stationInfo.chargingStationId
278 } (hashId: ${data.stationInfo.hashId}) started (${
279 this.numberOfStartedChargingStations
280 } started from ${this.numberOfChargingStations})`
281 );
282 };
283
284 private workerEventStopped = (data: ChargingStationData) => {
285 this.uiServer?.chargingStations.set(data.stationInfo.hashId, data);
286 --this.numberOfStartedChargingStations;
287 logger.info(
288 `${this.logPrefix()} ${moduleName}.workerEventStopped: Charging station ${
289 data.stationInfo.chargingStationId
290 } (hashId: ${data.stationInfo.hashId}) stopped (${
291 this.numberOfStartedChargingStations
292 } started from ${this.numberOfChargingStations})`
293 );
294 };
295
296 private workerEventUpdated = (data: ChargingStationData) => {
297 this.uiServer?.chargingStations.set(data.stationInfo.hashId, data);
298 };
299
300 private workerEventPerformanceStatistics = (data: Statistics) => {
301 this.storage.storePerformanceStatistics(data) as void;
302 };
303
304 private initializeCounters() {
305 if (this.initializedCounters === false) {
306 this.resetCounters();
307 const stationTemplateUrls = Configuration.getStationTemplateUrls();
308 if (isNotEmptyArray(stationTemplateUrls)) {
309 this.numberOfChargingStationTemplates = stationTemplateUrls.length;
310 for (const stationTemplateUrl of stationTemplateUrls) {
311 this.numberOfChargingStations += stationTemplateUrl.numberOfStations ?? 0;
312 }
313 } else {
314 console.warn(
315 chalk.yellow("'stationTemplateUrls' not defined or empty in configuration, exiting")
316 );
317 process.exit(exitCodes.missingChargingStationsConfiguration);
318 }
319 if (this.numberOfChargingStations === 0) {
320 console.warn(
321 chalk.yellow('No charging station template enabled in configuration, exiting')
322 );
323 process.exit(exitCodes.noChargingStationTemplates);
324 }
325 this.initializedCounters = true;
326 }
327 }
328
329 private resetCounters(): void {
330 this.numberOfChargingStationTemplates = 0;
331 this.numberOfChargingStations = 0;
332 this.numberOfStartedChargingStations = 0;
333 }
334
335 private async startChargingStation(
336 index: number,
337 stationTemplateUrl: StationTemplateUrl
338 ): Promise<void> {
339 await this.workerImplementation?.addElement({
340 index,
341 templateFile: join(
342 dirname(fileURLToPath(import.meta.url)),
343 'assets',
344 'station-templates',
345 stationTemplateUrl.file
346 ),
347 });
348 }
349
350 private gracefulShutdown = (): void => {
351 console.info(`${chalk.green('Graceful shutdown')}`);
352 this.stop()
353 .then(() => {
354 process.exit(0);
355 })
356 .catch((error) => {
357 console.error(chalk.red('Error while shutdowning charging stations simulator: '), error);
358 process.exit(1);
359 });
360 };
361
362 private logPrefix = (): string => {
363 return logPrefix(' Bootstrap |');
364 };
365 }