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 { buildTemplateName, 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
)
}
}
+ 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 }
+ })
}
}
)
): Promise<void> {
if (!this.started && !this.starting) {
throw new BaseError(
- 'Cannot add charging station while charging stations simulator is not started'
+ 'Cannot add charging station while the charging stations simulator is not started'
)
}
await this.workerImplementation?.addElement({
exit(exitCodes.gracefulShutdownError)
})
})
- .catch(error => {
+ .catch((error: unknown) => {
console.error(chalk.red('Error while shutdowning charging stations simulator: '), error)
exit(exitCodes.gracefulShutdownError)
})