fix: only process worker message events when necessary
authorJérôme Benoit <jerome.benoit@sap.com>
Sat, 23 Mar 2024 11:27:19 +0000 (12:27 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Sat, 23 Mar 2024 11:27:19 +0000 (12:27 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/Bootstrap.ts
src/charging-station/ChargingStationWorker.ts
src/worker/WorkerSet.ts

index 5b2ace54b934d1ad6eee03da8d4a6b94fe100cb5..bb801e74eb1ffd566c7d30af07fa5d65af166d19 100644 (file)
@@ -370,7 +370,7 @@ export class Bootstrap extends EventEmitter {
     msg: ChargingStationWorkerMessage<ChargingStationWorkerMessageData>
   ): void {
     // logger.debug(
-    //   `${this.logPrefix()} ${moduleName}.messageHandler: Worker channel message received: ${JSON.stringify(
+    //   `${this.logPrefix()} ${moduleName}.messageHandler: Charging station worker message received: ${JSON.stringify(
     //     msg,
     //     undefined,
     //     2
@@ -404,12 +404,12 @@ export class Bootstrap extends EventEmitter {
           break
         default:
           throw new BaseError(
-            `Unknown charging station worker event: '${event}' received with data: ${JSON.stringify(data, undefined, 2)}`
+            `Unknown charging station worker message event: '${event}' received with data: ${JSON.stringify(data, undefined, 2)}`
           )
       }
     } catch (error) {
       logger.error(
-        `${this.logPrefix()} ${moduleName}.messageHandler: Error occurred while handling '${event}' event:`,
+        `${this.logPrefix()} ${moduleName}.messageHandler: Error occurred while handling charging station worker message event '${event}':`,
         error
       )
     }
index 779c638a49fab5ec30119e184682335afa01b20d..ab32a464fabbcf3ca3ee2e20a7c48d685945f327 100644 (file)
@@ -26,41 +26,44 @@ if (Configuration.workerPoolInUse()) {
     constructor () {
       parentPort?.on('message', (message: WorkerMessage<Data>) => {
         const { uuid, event, data } = message
-        switch (event) {
-          case WorkerMessageEvents.addWorkerElement:
-            try {
-              const chargingStation = new ChargingStation(
-                data.index,
-                data.templateFile,
-                data.options
+        // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
+        if (uuid != null) {
+          switch (event) {
+            case WorkerMessageEvents.addWorkerElement:
+              try {
+                const chargingStation = new ChargingStation(
+                  data.index,
+                  data.templateFile,
+                  data.options
+                )
+                parentPort?.postMessage({
+                  uuid,
+                  event: WorkerMessageEvents.addedWorkerElement,
+                  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+                  data: chargingStation.stationInfo!
+                } satisfies WorkerMessage<ChargingStationInfo>)
+              } catch (error) {
+                parentPort?.postMessage({
+                  uuid,
+                  event: WorkerMessageEvents.workerElementError,
+                  data: {
+                    event,
+                    name: (error as Error).name,
+                    message: (error as Error).message,
+                    stack: (error as Error).stack
+                  }
+                } satisfies WorkerMessage<WorkerDataError>)
+              }
+              break
+            default:
+              throw new BaseError(
+                `Unknown worker message event: '${event}' received with data: '${JSON.stringify(
+                  data,
+                  undefined,
+                  2
+                )}'`
               )
-              parentPort?.postMessage({
-                uuid,
-                event: WorkerMessageEvents.addedWorkerElement,
-                // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-                data: chargingStation.stationInfo!
-              } satisfies WorkerMessage<ChargingStationInfo>)
-            } catch (error) {
-              parentPort?.postMessage({
-                uuid,
-                event: WorkerMessageEvents.workerElementError,
-                data: {
-                  event,
-                  name: (error as Error).name,
-                  message: (error as Error).message,
-                  stack: (error as Error).stack
-                }
-              } satisfies WorkerMessage<WorkerDataError>)
-            }
-            break
-          default:
-            throw new BaseError(
-              `Unknown worker event: '${event}' received with data: '${JSON.stringify(
-                data,
-                undefined,
-                2
-              )}'`
-            )
+          }
         }
       })
     }
index 4e360db8fa89b8a929cd60fd90a376058b0e2517..b09ea711df718bbcd508dfba52dd99112e90b285 100644 (file)
@@ -157,13 +157,26 @@ export class WorkerSet<D extends WorkerData, R extends WorkerData> extends Worke
       if (this.promiseResponseMap.has(uuid)) {
         // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
         const { resolve, reject, workerSetElement } = this.promiseResponseMap.get(uuid)!
-        if (event === WorkerMessageEvents.addedWorkerElement) {
-          this.emitter?.emit(WorkerSetEvents.elementAdded, this.info)
-          ++workerSetElement.numberOfWorkerElements
-          resolve(data)
-        } else if (event === WorkerMessageEvents.workerElementError) {
-          this.emitter?.emit(WorkerSetEvents.elementError, data)
-          reject(data)
+        switch (event) {
+          case WorkerMessageEvents.addedWorkerElement:
+            this.emitter?.emit(WorkerSetEvents.elementAdded, this.info)
+            ++workerSetElement.numberOfWorkerElements
+            resolve(data)
+            break
+          case WorkerMessageEvents.workerElementError:
+            this.emitter?.emit(WorkerSetEvents.elementError, data)
+            reject(data)
+            break
+          default:
+            reject(
+              new Error(
+                `Unknown worker message event: '${event}' received with data: '${JSON.stringify(
+                  data,
+                  undefined,
+                  2
+                )}'`
+              )
+            )
         }
         this.promiseResponseMap.delete(uuid)
       }