fix: remove incorrect promise race usage at stopping charging stations
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 19 Nov 2023 11:32:27 +0000 (12:32 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 19 Nov 2023 11:32:27 +0000 (12:32 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/Bootstrap.ts
src/charging-station/ocpp/OCPPRequestService.ts
ui/web/src/composables/UIClient.ts

index 2c7f56fe4de94c1d7524e99957e68c1b8503c83c..88c516bddeb527be0c11cf03ac8902c3e16fda87 100644 (file)
@@ -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<void> {
-    await Promise.race([
+  private async waitChargingStationsStopped(): Promise<string> {
+    return new Promise<string>((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<string>((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);
           });
       })
index 6e6d0f70db4a6a2554c5707287209c3525972191..880cf078df19b370457fe5c51c3204961bea61eb 100644 (file)
@@ -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<ResponseType>((resolve, reject) => {
+      return new Promise<ResponseType>((resolve, reject) => {
         /**
          * Function that will receive the request's response
          *
index 841ec3dca8c42510c3ce504c05dbe42c45773adc..89db2302c81e1dec901965efeb4c6444196a5d30 100644 (file)
@@ -145,7 +145,7 @@ export class UIClient {
     data: RequestPayload,
   ): Promise<ResponsePayload> {
     let uuid: string;
-    return await new Promise<ResponsePayload>((resolve, reject) => {
+    return new Promise<ResponsePayload>((resolve, reject) => {
       uuid = crypto.randomUUID();
       const msg = JSON.stringify([uuid, command, data]);