From a596d200439848662293885dd9730f91be083114 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 2 Feb 2023 22:04:53 +0100 Subject: [PATCH] fix(simulator): fix delayed initialization in promise at startup 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 | 49 ++++++++++++++++++------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index 7a823e56..80e0f49a 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -45,6 +45,7 @@ export class Bootstrap { private readonly storage!: Storage; private numberOfStartedChargingStations!: number; private readonly version: string = version; + private initializedCounters: boolean; private started: boolean; private readonly workerScript: string; @@ -52,7 +53,9 @@ export class Bootstrap { // Enable unconditionally for now this.logUnhandledRejection(); this.logUncaughtException(); + this.initializedCounters = false; this.started = false; + this.initializeCounters(); this.workerImplementation = null; this.workerScript = path.join( path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../'), @@ -82,11 +85,11 @@ export class Bootstrap { try { this.initializeCounters(); this.initializeWorkerImplementation(); - await this.storage?.open(); await this.workerImplementation?.start(); + await this.storage?.open(); this.uiServer?.start(); const stationTemplateUrls = Configuration.getStationTemplateUrls(); - // Start ChargingStation object in worker thread + // Start ChargingStation object instance in worker thread for (const stationTemplateUrl of stationTemplateUrls) { try { const nbStations = stationTemplateUrl.numberOfStations ?? 0; @@ -132,6 +135,7 @@ export class Bootstrap { public async stop(): Promise { if (isMainThread && this.started === true) { + this.initializedCounters = false; await this.workerImplementation?.stop(); this.workerImplementation = null; this.uiServer?.stop(); @@ -238,25 +242,30 @@ export class Bootstrap { }; private initializeCounters() { - this.numberOfChargingStationTemplates = 0; - this.numberOfChargingStations = 0; - const stationTemplateUrls = Configuration.getStationTemplateUrls(); - if (!Utils.isEmptyArray(stationTemplateUrls)) { - this.numberOfChargingStationTemplates = stationTemplateUrls?.length; - stationTemplateUrls.forEach((stationTemplateUrl) => { - this.numberOfChargingStations += stationTemplateUrl.numberOfStations ?? 0; - }); - } else { - console.warn( - chalk.yellow("'stationTemplateUrls' not defined or empty in configuration, exiting") - ); - process.exit(exitCodes.missingChargingStationsConfiguration); - } - if (this.numberOfChargingStations === 0) { - console.warn(chalk.yellow('No charging station template enabled in configuration, exiting')); - process.exit(exitCodes.noChargingStationTemplates); + if (this.initializedCounters === false) { + this.numberOfChargingStationTemplates = 0; + this.numberOfChargingStations = 0; + const stationTemplateUrls = Configuration.getStationTemplateUrls(); + if (!Utils.isEmptyArray(stationTemplateUrls)) { + this.numberOfChargingStationTemplates = stationTemplateUrls?.length; + stationTemplateUrls.forEach((stationTemplateUrl) => { + this.numberOfChargingStations += stationTemplateUrl.numberOfStations ?? 0; + }); + } else { + console.warn( + chalk.yellow("'stationTemplateUrls' not defined or empty in configuration, exiting") + ); + process.exit(exitCodes.missingChargingStationsConfiguration); + } + if (this.numberOfChargingStations === 0) { + console.warn( + chalk.yellow('No charging station template enabled in configuration, exiting') + ); + process.exit(exitCodes.noChargingStationTemplates); + } + this.numberOfStartedChargingStations = 0; + this.initializedCounters = true; } - this.numberOfStartedChargingStations = 0; } private logUncaughtException(): void { -- 2.34.1