// Partial Copyright Jerome Benoit. 2021-2024. All Rights Reserved.
import { EventEmitter } from 'node:events'
-import { dirname, extname, join, parse } from 'node:path'
+import { dirname, extname, join } from 'node:path'
import process, { exit } from 'node:process'
import { fileURLToPath } from 'node:url'
import { isMainThread } from 'node:worker_threads'
-import type { Worker } from 'worker_threads'
import chalk from 'chalk'
-import { type MessageHandler, availableParallelism } from 'poolifier'
+import { availableParallelism, type MessageHandler } from 'poolifier'
+import type { Worker } from 'worker_threads'
-import { waitChargingStationEvents } from './Helpers.js'
-import type { AbstractUIServer } from './ui-server/AbstractUIServer.js'
-import { UIServerFactory } from './ui-server/UIServerFactory.js'
import { version } from '../../package.json'
import { BaseError } from '../exception/index.js'
import { type Storage, StorageFactory } from '../performance/index.js'
type ChargingStationWorkerMessageData,
ChargingStationWorkerMessageEvents,
ConfigurationSection,
- type InternalTemplateStatistics,
ProcedureName,
type SimulatorState,
type Statistics,
type StorageConfiguration,
+ type TemplateStatistics,
type UIServerConfiguration,
type WorkerConfiguration
} from '../types/index.js'
import {
Configuration,
Constants,
- buildTemplateStatisticsPayload,
formatDurationMilliSeconds,
generateUUID,
handleUncaughtException,
handleUnhandledRejection,
isAsyncFunction,
isNotEmptyArray,
- logPrefix,
- logger
+ logger,
+ logPrefix
} from '../utils/index.js'
import { type WorkerAbstract, WorkerFactory } from '../worker/index.js'
+import { buildTemplateName, waitChargingStationEvents } from './Helpers.js'
+import type { AbstractUIServer } from './ui-server/AbstractUIServer.js'
+import { UIServerFactory } from './ui-server/UIServerFactory.js'
const moduleName = 'Bootstrap'
private workerImplementation?: WorkerAbstract<ChargingStationWorkerData>
private readonly uiServer: AbstractUIServer
private storage?: Storage
- private readonly templateStatistics: Map<string, InternalTemplateStatistics>
+ private readonly templateStatistics: Map<string, TemplateStatistics>
private readonly version: string = version
private initializedCounters: boolean
private started: boolean
this.started = false
this.starting = false
this.stopping = false
+ this.initializedCounters = false
this.uiServerStarted = false
+ this.templateStatistics = new Map<string, TemplateStatistics>()
+ this.initializeWorkerImplementation(
+ Configuration.getConfigurationSection<WorkerConfiguration>(ConfigurationSection.worker)
+ )
this.uiServer = UIServerFactory.getUIServerImplementation(
Configuration.getConfigurationSection<UIServerConfiguration>(ConfigurationSection.uiServer)
)
- this.templateStatistics = new Map<string, InternalTemplateStatistics>()
- this.initializedCounters = false
this.initializeCounters()
Configuration.configurationChangeCallback = async () => {
if (isMainThread) {
return {
version: this.version,
started: this.started,
- templateStatistics: buildTemplateStatisticsPayload(this.templateStatistics)
+ templateStatistics: this.templateStatistics
}
}
}
)
this.initializeCounters()
- const workerConfiguration = Configuration.getConfigurationSection<WorkerConfiguration>(
- ConfigurationSection.worker
- )
- this.initializeWorkerImplementation(workerConfiguration)
- await this.workerImplementation?.start()
+ // eslint-disable-next-line @typescript-eslint/unbound-method
+ if (isAsyncFunction(this.workerImplementation?.start)) {
+ await this.workerImplementation.start()
+ } else {
+ (this.workerImplementation?.start as () => void)()
+ }
const performanceStorageConfiguration =
Configuration.getConfigurationSection<StorageConfiguration>(
ConfigurationSection.performanceStorage
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
for (const stationTemplateUrl of Configuration.getStationTemplateUrls()!) {
try {
- const nbStations =
- this.templateStatistics.get(parse(stationTemplateUrl.file).name)?.configured ??
- stationTemplateUrl.numberOfStations
+ const nbStations = stationTemplateUrl.numberOfStations
for (let index = 1; index <= nbStations; index++) {
await this.addChargingStation(index, stationTemplateUrl.file)
}
)
}
}
+ const workerConfiguration = Configuration.getConfigurationSection<WorkerConfiguration>(
+ ConfigurationSection.worker
+ )
console.info(
chalk.green(
`Charging stations simulator ${
console.error(chalk.red('Error while waiting for charging stations to stop: '), error)
}
await this.workerImplementation?.stop()
- delete this.workerImplementation
this.removeAllListeners()
this.uiServer.clearCaches()
this.initializedCounters = false
private async restart (): Promise<void> {
await this.stop()
+ // FIXME: initialize worker implementation only if the worker section has changed
+ this.initializeWorkerImplementation(
+ Configuration.getConfigurationSection<WorkerConfiguration>(ConfigurationSection.worker)
+ )
if (
this.uiServerStarted &&
Configuration.getConfigurationSection<UIServerConfiguration>(ConfigurationSection.uiServer)
}
private async waitChargingStationsStopped (): Promise<string> {
- return await new Promise<string>((resolve, reject) => {
+ return await new Promise<string>((resolve, reject: (reason?: unknown) => void) => {
const waitTimeout = setTimeout(() => {
const timeoutMessage = `Timeout ${formatDurationMilliSeconds(
Constants.STOP_CHARGING_STATIONS_TIMEOUT
workerConfiguration.processType!,
{
workerStartDelay: workerConfiguration.startDelay,
- elementStartDelay: workerConfiguration.elementStartDelay,
+ elementAddDelay: workerConfiguration.elementAddDelay,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
poolMaxSize: workerConfiguration.poolMaxSize!,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
elementsPerWorker,
poolOptions: {
messageHandler: this.messageHandler.bind(this) as MessageHandler<Worker>,
- workerOptions: { resourceLimits: workerConfiguration.resourceLimits }
+ ...(workerConfiguration.resourceLimits != null && {
+ workerOptions: { resourceLimits: workerConfiguration.resourceLimits }
+ })
}
}
)
const stationTemplateUrls = Configuration.getStationTemplateUrls()!
if (isNotEmptyArray(stationTemplateUrls)) {
for (const stationTemplateUrl of stationTemplateUrls) {
- const templateName = join(
- parse(stationTemplateUrl.file).dir,
- parse(stationTemplateUrl.file).name
- )
+ const templateName = buildTemplateName(stationTemplateUrl.file)
this.templateStatistics.set(templateName, {
configured: stationTemplateUrl.numberOfStations,
added: 0,
public async addChargingStation (
index: number,
- stationTemplateFile: string,
+ templateFile: string,
options?: ChargingStationOptions
): Promise<void> {
+ if (!this.started && !this.starting) {
+ throw new BaseError(
+ 'Cannot add charging station while the charging stations simulator is not started'
+ )
+ }
await this.workerImplementation?.addElement({
index,
templateFile: join(
dirname(fileURLToPath(import.meta.url)),
'assets',
'station-templates',
- stationTemplateFile
+ templateFile
),
options
})
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- const templateStatistics = this.templateStatistics.get(parse(stationTemplateFile).name)!
+ const templateStatistics = this.templateStatistics.get(buildTemplateName(templateFile))!
++templateStatistics.added
templateStatistics.indexes.add(index)
}
exit(exitCodes.gracefulShutdownError)
})
})
- .catch(error => {
+ .catch((error: unknown) => {
console.error(chalk.red('Error while shutdowning charging stations simulator: '), error)
exit(exitCodes.gracefulShutdownError)
})