X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fcharging-station%2FBootstrap.ts;h=00926e4fd8860dc35a513645253c6cdfcbc646fe;hb=2665ed1ef62a9fc9b6eec417f3ec7c33305789cf;hp=22b3207b57e5a03fbe09e3d250d8405486ba22ec;hpb=a01134ed215b62a0c82475b9fbb35306fa792bbc;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index 22b3207b..00926e4f 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,6 +36,7 @@ import { generateUUID, handleUncaughtException, handleUnhandledRejection, + isAsyncFunction, isNotEmptyArray, logPrefix, logger @@ -47,7 +49,7 @@ enum exitCodes { succeeded = 0, missingChargingStationsConfiguration = 1, noChargingStationTemplates = 2, - gracefulShutdownError = 3, + gracefulShutdownError = 3 } export class Bootstrap extends EventEmitter { @@ -273,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 } } } @@ -309,7 +311,7 @@ export class Bootstrap extends EventEmitter { break case ChargingStationWorkerMessageEvents.startWorkerElementError: logger.error( - `${this.logPrefix()} ${moduleName}.messageHandler: Error occured while starting worker element:`, + `${this.logPrefix()} ${moduleName}.messageHandler: Error occurred while starting worker element:`, msg.data ) this.emit(ChargingStationWorkerMessageEvents.startWorkerElementError, msg.data) @@ -362,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 { @@ -424,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) })