-// 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'
generateUUID,
handleUncaughtException,
handleUnhandledRejection,
+ isAsyncFunction,
isNotEmptyArray,
logPrefix,
logger
succeeded = 0,
missingChargingStationsConfiguration = 1,
noChargingStationTemplates = 2,
- gracefulShutdownError = 3,
+ gracefulShutdownError = 3
}
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<Worker>,
workerOptions: { resourceLimits: workerConfiguration.resourceLimits }
}
}
}
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<void>
+ )(data).catch(Constants.EMPTY_FUNCTION)
+ } else {
+ (this.storage?.storePerformanceStatistics as (performanceStatistics: Statistics) => void)(
+ data
+ )
+ }
}
private initializeCounters (): void {
exit(exitCodes.gracefulShutdownError)
})
})
- .catch((error) => {
+ .catch(error => {
console.error(chalk.red('Error while shutdowning charging stations simulator: '), error)
exit(exitCodes.gracefulShutdownError)
})