UI Server: Always delete reponse handler in error case
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 9 Sep 2022 20:02:05 +0000 (22:02 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Fri, 9 Sep 2022 20:02:05 +0000 (22:02 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/Bootstrap.ts
src/charging-station/ui-server/UIHttpServer.ts
src/charging-station/ui-server/UIWebSocketServer.ts

index 6dc6620d71dd4fed3f834dca67b53ebba6e68e18..37cd1e64d2693ae99436137aebe32df5a7cfe46e 100644 (file)
@@ -159,7 +159,7 @@ export class Bootstrap {
   }
 
   private initializeWorkerImplementation(): void {
-    !this.workerImplementation &&
+    this.workerImplementation === null &&
       (this.workerImplementation = WorkerFactory.getWorkerImplementation<ChargingStationWorkerData>(
         this.workerScript,
         Configuration.getWorker().processType,
@@ -218,19 +218,19 @@ export class Bootstrap {
     }
   }
 
-  private workerEventStarted(data: ChargingStationData) {
+  private workerEventStarted = (data: ChargingStationData) => {
     this.uiServer?.chargingStations.set(data.stationInfo.hashId, data);
     ++this.numberOfStartedChargingStations;
-  }
+  };
 
-  private workerEventStopped(data: ChargingStationData) {
+  private workerEventStopped = (data: ChargingStationData) => {
     this.uiServer?.chargingStations.set(data.stationInfo.hashId, data);
     --this.numberOfStartedChargingStations;
-  }
+  };
 
-  private workerEventUpdated(data: ChargingStationData) {
+  private workerEventUpdated = (data: ChargingStationData) => {
     this.uiServer?.chargingStations.set(data.stationInfo.hashId, data);
-  }
+  };
 
   private workerEventPerformanceStatistics = (data: Statistics) => {
     this.storage.storePerformanceStatistics(data) as void;
index 822244f96081305ef939ae7f107d67f9c840c1a4..37c579dc56a552dc26dbd57ae515378820e9e2d9 100644 (file)
@@ -47,7 +47,6 @@ export default class UIHttpServer extends AbstractUIServer {
             'Content-Type': 'application/json',
           })
           .end(JSON.stringify(payload));
-        this.responseHandlers.delete(uuid);
       } else {
         logger.error(
           `${this.logPrefix(moduleName, 'sendResponse')} Response for unknown request id: ${uuid}`
@@ -58,6 +57,8 @@ export default class UIHttpServer extends AbstractUIServer {
         `${this.logPrefix(moduleName, 'sendResponse')} Error at sending response id '${uuid}':`,
         error
       );
+    } finally {
+      this.responseHandlers.delete(uuid);
     }
   }
 
index 03cd184ddc81a106fb5a72c17ec35aabc7290014..f8f836b1073b98b0f861145cefa441ba71192f89 100644 (file)
@@ -99,8 +99,16 @@ export default class UIWebSocketServer extends AbstractUIServer {
         const ws = this.responseHandlers.get(responseId) as WebSocket;
         if (ws?.readyState === WebSocket.OPEN) {
           ws.send(JSON.stringify(response));
+        } else {
+          logger.error(
+            `${this.logPrefix(
+              moduleName,
+              'sendResponse'
+            )} Error at sending response id '${responseId}', WebSocket is not open: ${
+              ws?.readyState
+            }`
+          );
         }
-        this.responseHandlers.delete(responseId);
       } else {
         logger.error(
           `${this.logPrefix(
@@ -117,6 +125,8 @@ export default class UIWebSocketServer extends AbstractUIServer {
         )} Error at sending response id '${responseId}':`,
         error
       );
+    } finally {
+      this.responseHandlers.delete(responseId);
     }
   }