fix: ensure simulator will stop after a timeout (120s)
authorJérôme Benoit <jerome.benoit@sap.com>
Mon, 19 Jun 2023 20:02:34 +0000 (22:02 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Mon, 19 Jun 2023 20:02:34 +0000 (22:02 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/Bootstrap.ts
src/utils/Constants.ts

index d56beefcdc4df36076911751ce32f54dd26edf3e..3612f2818692671235bcd59384837c06e8ec8b62 100644 (file)
@@ -162,11 +162,22 @@ export class Bootstrap extends EventEmitter {
             Constants.EMPTY_FREEZED_OBJECT
           )
         );
-        await ChargingStationUtils.waitForChargingStationEvents(
-          this,
-          ChargingStationWorkerMessageEvents.stopped,
-          this.numberOfChargingStations
-        );
+        await Promise.race([
+          ChargingStationUtils.waitForChargingStationEvents(
+            this,
+            ChargingStationWorkerMessageEvents.stopped,
+            this.numberOfChargingStations
+          ),
+          new Promise<string>((resolve) => {
+            setTimeout(() => {
+              const message = `Timeout reached ${Utils.formatDurationMilliSeconds(
+                Constants.STOP_SIMULATOR_TIMEOUT
+              )} at stopping charging stations simulator`;
+              console.warn(chalk.yellow(message));
+              resolve(message);
+            }, Constants.STOP_SIMULATOR_TIMEOUT);
+          }),
+        ]);
         await this.workerImplementation?.stop();
         this.workerImplementation = null;
         this.uiServer?.stop();
index bb765c75dcf8f00d6ed766c1c1f8ccdcd1592357..30327b6a578bdd0efac7448f0d4645af7d257963 100644 (file)
@@ -47,6 +47,8 @@ export class Constants {
 
   static readonly MAX_RANDOM_INTEGER = 281474976710654;
 
+  static readonly STOP_SIMULATOR_TIMEOUT = 120000; // Ms
+
   static readonly EMPTY_FREEZED_OBJECT = Object.freeze({});
   static readonly EMPTY_FUNCTION = Object.freeze(() => {
     /* This is intentional */