From 2bb3c92f49572f8d81f40620df42de19217a6b4c Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 19 Mar 2024 20:03:03 +0100 Subject: [PATCH] fix: fix simulator initialization ordering MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/Bootstrap.ts | 86 ++++++++++++++----------------- src/worker/WorkerSet.ts | 5 +- 2 files changed, 41 insertions(+), 50 deletions(-) diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index 64953a96..34ac90f3 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -64,7 +64,6 @@ export class Bootstrap extends EventEmitter { private storage?: Storage private readonly templateStatistics: Map private readonly version: string = version - private initializedCounters: boolean private started: boolean private starting: boolean private stopping: boolean @@ -81,16 +80,15 @@ export class Bootstrap extends EventEmitter { this.started = false this.starting = false this.stopping = false - this.initializedCounters = false this.uiServerStarted = false this.templateStatistics = new Map() - this.initializeWorkerImplementation( - Configuration.getConfigurationSection(ConfigurationSection.worker) - ) this.uiServer = UIServerFactory.getUIServerImplementation( Configuration.getConfigurationSection(ConfigurationSection.uiServer) ) this.initializeCounters() + this.initializeWorkerImplementation( + Configuration.getConfigurationSection(ConfigurationSection.worker) + ) Configuration.configurationChangeCallback = async () => { if (isMainThread) { await Bootstrap.getInstance().restart() @@ -177,7 +175,6 @@ export class Bootstrap extends EventEmitter { ) } ) - this.initializeCounters() // eslint-disable-next-line @typescript-eslint/unbound-method if (isAsyncFunction(this.workerImplementation?.start)) { await this.workerImplementation.start() @@ -278,7 +275,6 @@ export class Bootstrap extends EventEmitter { await this.workerImplementation?.stop() this.removeAllListeners() this.uiServer.clearCaches() - this.initializedCounters = false await this.storage?.close() delete this.storage this.started = false @@ -293,10 +289,6 @@ export class Bootstrap extends EventEmitter { private async restart (): Promise { await this.stop() - // FIXME: initialize worker implementation only if the worker section has changed - this.initializeWorkerImplementation( - Configuration.getConfigurationSection(ConfigurationSection.worker) - ) if ( this.uiServerStarted && Configuration.getConfigurationSection(ConfigurationSection.uiServer) @@ -305,6 +297,11 @@ export class Bootstrap extends EventEmitter { this.uiServer.stop() this.uiServerStarted = false } + this.initializeCounters() + // FIXME: initialize worker implementation only if the worker section has changed + this.initializeWorkerImplementation( + Configuration.getConfigurationSection(ConfigurationSection.worker) + ) await this.start() } @@ -499,47 +496,44 @@ export class Bootstrap extends EventEmitter { } private initializeCounters (): void { - if (!this.initializedCounters) { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const stationTemplateUrls = Configuration.getStationTemplateUrls()! - if (isNotEmptyArray(stationTemplateUrls)) { - for (const stationTemplateUrl of stationTemplateUrls) { - const templateName = buildTemplateName(stationTemplateUrl.file) - this.templateStatistics.set(templateName, { - configured: stationTemplateUrl.numberOfStations, - added: 0, - started: 0, - indexes: new Set() - }) - this.uiServer.chargingStationTemplates.add(templateName) - } - if (this.templateStatistics.size !== stationTemplateUrls.length) { - console.error( - chalk.red( - "'stationTemplateUrls' contains duplicate entries, please check your configuration" - ) - ) - exit(exitCodes.duplicateChargingStationTemplateUrls) - } - } else { - console.error( - chalk.red("'stationTemplateUrls' not defined or empty, please check your configuration") - ) - exit(exitCodes.missingChargingStationsConfiguration) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const stationTemplateUrls = Configuration.getStationTemplateUrls()! + if (isNotEmptyArray(stationTemplateUrls)) { + for (const stationTemplateUrl of stationTemplateUrls) { + const templateName = buildTemplateName(stationTemplateUrl.file) + this.templateStatistics.set(templateName, { + configured: stationTemplateUrl.numberOfStations, + added: 0, + started: 0, + indexes: new Set() + }) + this.uiServer.chargingStationTemplates.add(templateName) } - if ( - this.numberOfConfiguredChargingStations === 0 && - Configuration.getConfigurationSection(ConfigurationSection.uiServer) - .enabled !== true - ) { + if (this.templateStatistics.size !== stationTemplateUrls.length) { console.error( chalk.red( - "'stationTemplateUrls' has no charging station enabled and UI server is disabled, please check your configuration" + "'stationTemplateUrls' contains duplicate entries, please check your configuration" ) ) - exit(exitCodes.noChargingStationTemplates) + exit(exitCodes.duplicateChargingStationTemplateUrls) } - this.initializedCounters = true + } else { + console.error( + chalk.red("'stationTemplateUrls' not defined or empty, please check your configuration") + ) + exit(exitCodes.missingChargingStationsConfiguration) + } + if ( + this.numberOfConfiguredChargingStations === 0 && + Configuration.getConfigurationSection(ConfigurationSection.uiServer) + .enabled !== true + ) { + console.error( + chalk.red( + "'stationTemplateUrls' has no charging station enabled and UI server is disabled, please check your configuration" + ) + ) + exit(exitCodes.noChargingStationTemplates) } } diff --git a/src/worker/WorkerSet.ts b/src/worker/WorkerSet.ts index 10606d2e..d0f80b6e 100644 --- a/src/worker/WorkerSet.ts +++ b/src/worker/WorkerSet.ts @@ -36,10 +36,7 @@ export class WorkerSet extends WorkerAbstract { if (!Number.isSafeInteger(this.workerOptions.elementsPerWorker)) { throw new TypeError('Elements per worker must be an integer') } - if ( - typeof this.workerOptions.elementsPerWorker === 'number' && - this.workerOptions.elementsPerWorker <= 0 - ) { + if (this.workerOptions.elementsPerWorker <= 0) { throw new RangeError('Elements per worker must be greater than zero') } this.workerSet = new Set() -- 2.34.1