X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fcharging-station%2FBootstrap.ts;h=f09864d4ed66d935960084061359fe6ca7af3799;hb=ba9a56a613727d96757690a8b52af6731f3fd8a8;hp=731cc477d7a406d8165c8764ea851bc06115bafe;hpb=66a7748ddeda8c94d7562a1ce58d440319654a4c;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index 731cc477..f09864d4 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -1,12 +1,13 @@ -// Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved. +// Partial Copyright Jerome Benoit. 2021-2024. All Rights Reserved. import { EventEmitter } from 'node:events' import { dirname, extname, join } from 'node:path' import process, { exit } from 'node:process' import { fileURLToPath } from 'node:url' +import type { Worker } from 'worker_threads' import chalk from 'chalk' -import { availableParallelism } from 'poolifier' +import { type MessageHandler, availableParallelism } from 'poolifier' import { waitChargingStationEvents } from './Helpers.js' import type { AbstractUIServer } from './ui-server/AbstractUIServer.js' @@ -35,8 +36,8 @@ import { generateUUID, handleUncaughtException, handleUnhandledRejection, + isAsyncFunction, isNotEmptyArray, - isNullOrUndefined, logPrefix, logger } from '../utils/index.js' @@ -130,7 +131,7 @@ export class Bootstrap extends EventEmitter { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion for (const stationTemplateUrl of Configuration.getStationTemplateUrls()!) { try { - const nbStations = stationTemplateUrl.numberOfStations ?? 0 + const nbStations = stationTemplateUrl.numberOfStations for (let index = 1; index <= nbStations; index++) { await this.startChargingStation(index, stationTemplateUrl) } @@ -156,8 +157,8 @@ export class Bootstrap extends EventEmitter { ? `/${workerConfiguration.poolMaxSize?.toString()}` : '' } worker(s) concurrently running in '${workerConfiguration.processType}' mode${ - !isNullOrUndefined(this.workerImplementation?.maxElementsPerWorker) - ? ` (${this.workerImplementation?.maxElementsPerWorker} charging station(s) per worker)` + this.workerImplementation?.maxElementsPerWorker != null + ? ` (${this.workerImplementation.maxElementsPerWorker} charging station(s) per worker)` : '' }` ) @@ -224,16 +225,16 @@ export class Bootstrap extends EventEmitter { private async waitChargingStationsStopped (): Promise { return await new Promise((resolve, reject) => { const waitTimeout = setTimeout(() => { - const message = `Timeout ${formatDurationMilliSeconds( + const timeoutMessage = `Timeout ${formatDurationMilliSeconds( Constants.STOP_CHARGING_STATIONS_TIMEOUT )} reached at stopping charging stations` - console.warn(chalk.yellow(message)) - reject(new Error(message)) + console.warn(chalk.yellow(timeoutMessage)) + reject(new Error(timeoutMessage)) }, Constants.STOP_CHARGING_STATIONS_TIMEOUT) waitChargingStationEvents( this, ChargingStationWorkerMessageEvents.stopped, - this.numberOfChargingStations + this.numberOfStartedChargingStations ) .then(() => { resolve('Charging stations stopped') @@ -247,7 +248,7 @@ export class Bootstrap extends EventEmitter { private initializeWorkerImplementation (workerConfiguration: WorkerConfiguration): void { let elementsPerWorker: number | undefined - switch (workerConfiguration?.elementsPerWorker) { + switch (workerConfiguration.elementsPerWorker) { case 'auto': elementsPerWorker = this.numberOfChargingStations > availableParallelism() @@ -274,7 +275,7 @@ export class Bootstrap extends EventEmitter { poolMinSize: workerConfiguration.poolMinSize!, elementsPerWorker: elementsPerWorker ?? (workerConfiguration.elementsPerWorker as number), poolOptions: { - messageHandler: this.messageHandler.bind(this) as (message: unknown) => void, + messageHandler: this.messageHandler.bind(this) as MessageHandler, workerOptions: { resourceLimits: workerConfiguration.resourceLimits } } } @@ -363,7 +364,18 @@ export class Bootstrap extends EventEmitter { } private readonly workerEventPerformanceStatistics = (data: Statistics): void => { - this.storage?.storePerformanceStatistics(data) as undefined + // eslint-disable-next-line @typescript-eslint/unbound-method + if (isAsyncFunction(this.storage?.storePerformanceStatistics)) { + ( + this.storage.storePerformanceStatistics as ( + performanceStatistics: Statistics + ) => Promise + )(data).catch(Constants.EMPTY_FUNCTION) + } else { + (this.storage?.storePerformanceStatistics as (performanceStatistics: Statistics) => void)( + data + ) + } } private initializeCounters (): void { @@ -374,7 +386,7 @@ export class Bootstrap extends EventEmitter { if (isNotEmptyArray(stationTemplateUrls)) { this.numberOfChargingStationTemplates = stationTemplateUrls.length for (const stationTemplateUrl of stationTemplateUrls) { - this.numberOfChargingStations += stationTemplateUrl.numberOfStations ?? 0 + this.numberOfChargingStations += stationTemplateUrl.numberOfStations } } else { console.warn( @@ -414,7 +426,7 @@ export class Bootstrap extends EventEmitter { private gracefulShutdown (): void { this.stop() .then(() => { - console.info(`${chalk.green('Graceful shutdown')}`) + console.info(chalk.green('Graceful shutdown')) this.uiServer?.stop() // stop() asks for charging stations to stop by default this.waitChargingStationsStopped() @@ -425,7 +437,7 @@ export class Bootstrap extends EventEmitter { exit(exitCodes.gracefulShutdownError) }) }) - .catch((error) => { + .catch(error => { console.error(chalk.red('Error while shutdowning charging stations simulator: '), error) exit(exitCodes.gracefulShutdownError) })