From 5b2721dbc4fa0b5ee6e48241a5077091a7b1c342 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 19 Nov 2023 12:32:27 +0100 Subject: [PATCH] fix: remove incorrect promise race usage at stopping charging stations 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 | 42 ++++++++++++------- .../ocpp/OCPPRequestService.ts | 2 +- ui/web/src/composables/UIClient.ts | 2 +- 3 files changed, 28 insertions(+), 18 deletions(-) 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]); -- 2.34.1