From: Jérôme Benoit Date: Sat, 23 Mar 2024 11:27:19 +0000 (+0100) Subject: fix: only process worker message events when necessary X-Git-Tag: v1.3.1~5 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=ce0abd8248cbc1c976bb298e45daeb0749387619;p=e-mobility-charging-stations-simulator.git fix: only process worker message events when necessary Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index 5b2ace54..bb801e74 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -370,7 +370,7 @@ export class Bootstrap extends EventEmitter { msg: ChargingStationWorkerMessage ): 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 ) } diff --git a/src/charging-station/ChargingStationWorker.ts b/src/charging-station/ChargingStationWorker.ts index 779c638a..ab32a464 100644 --- a/src/charging-station/ChargingStationWorker.ts +++ b/src/charging-station/ChargingStationWorker.ts @@ -26,41 +26,44 @@ if (Configuration.workerPoolInUse()) { constructor () { parentPort?.on('message', (message: WorkerMessage) => { 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) + } 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) + } + 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) - } 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) - } - break - default: - throw new BaseError( - `Unknown worker event: '${event}' received with data: '${JSON.stringify( - data, - undefined, - 2 - )}'` - ) + } } }) } diff --git a/src/worker/WorkerSet.ts b/src/worker/WorkerSet.ts index 4e360db8..b09ea711 100644 --- a/src/worker/WorkerSet.ts +++ b/src/worker/WorkerSet.ts @@ -157,13 +157,26 @@ export class WorkerSet 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) }