From: Jérôme Benoit Date: Sun, 19 Nov 2023 11:32:27 +0000 (+0100) Subject: fix: remove incorrect promise race usage at stopping charging stations X-Git-Tag: v1.2.25~7 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=5b2721dbc4fa0b5ee6e48241a5077091a7b1c342;p=e-mobility-charging-stations-simulator.git fix: remove incorrect promise race usage at stopping charging stations Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index 2c7f56fe..88c516bd 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -184,7 +184,11 @@ export class Bootstrap extends EventEmitter { Constants.EMPTY_FROZEN_OBJECT, ), ); - await this.waitChargingStationsStopped(); + try { + await this.waitChargingStationsStopped(); + } catch (error) { + console.error(chalk.red('Error while waiting for charging stations to stop: '), error); + } } await this.workerImplementation?.stop(); this.workerImplementation = null; @@ -207,23 +211,30 @@ export class Bootstrap extends EventEmitter { await this.start(); } - private async waitChargingStationsStopped(): Promise { - await Promise.race([ + private async waitChargingStationsStopped(): Promise { + return new Promise((resolve, reject) => { + const waitTimeout = setTimeout(() => { + const message = `Timeout ${formatDurationMilliSeconds( + Constants.STOP_SIMULATOR_TIMEOUT, + )} reached at stopping charging stations`; + console.warn(chalk.yellow(message)); + reject(new Error(message)); + }, Constants.STOP_SIMULATOR_TIMEOUT); waitChargingStationEvents( this, ChargingStationWorkerMessageEvents.stopped, this.numberOfChargingStations, - ), - new Promise((resolve) => { - setTimeout(() => { - const message = `Timeout ${formatDurationMilliSeconds( - Constants.STOP_SIMULATOR_TIMEOUT, - )} reached at stopping charging stations simulator`; - console.warn(chalk.yellow(message)); - resolve(message); - }, Constants.STOP_SIMULATOR_TIMEOUT); - }), - ]); + ) + .then(() => { + resolve('Charging stations stopped'); + }) + .catch((error) => { + reject(error); + }) + .finally(() => { + clearTimeout(waitTimeout); + }); + }); } private initializeWorkerImplementation(workerConfiguration: WorkerConfiguration): void { @@ -395,8 +406,7 @@ export class Bootstrap extends EventEmitter { .then(() => { exit(exitCodes.succeeded); }) - .catch((error) => { - console.error(chalk.red('Error while waiting for charging stations to stop: '), error); + .catch(() => { exit(exitCodes.gracefulShutdownError); }); }) diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index 6e6d0f70..880cf078 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -313,7 +313,7 @@ export abstract class OCPPRequestService { // eslint-disable-next-line @typescript-eslint/no-this-alias const self = this; // Send a message through wsConnection - return await new Promise((resolve, reject) => { + return new Promise((resolve, reject) => { /** * Function that will receive the request's response * diff --git a/ui/web/src/composables/UIClient.ts b/ui/web/src/composables/UIClient.ts index 841ec3dc..89db2302 100644 --- a/ui/web/src/composables/UIClient.ts +++ b/ui/web/src/composables/UIClient.ts @@ -145,7 +145,7 @@ export class UIClient { data: RequestPayload, ): Promise { let uuid: string; - return await new Promise((resolve, reject) => { + return new Promise((resolve, reject) => { uuid = crypto.randomUUID(); const msg = JSON.stringify([uuid, command, data]);