From 2bb7a73e94db7533c1ba47db677d709e6f07dfdb Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 20 Jul 2023 20:44:03 +0200 Subject: [PATCH] feat: add error handling to worker set worker MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/Bootstrap.ts | 9 ++++++++ src/charging-station/ChargingStationWorker.ts | 23 +++++++++++++++---- src/worker/WorkerConstants.ts | 2 +- src/worker/WorkerSet.ts | 2 +- src/worker/WorkerTypes.ts | 2 ++ 5 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index ac1dd88c..4bfea321 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -281,6 +281,15 @@ export class Bootstrap extends EventEmitter { msg.data as Statistics, ); break; + case ChargingStationWorkerMessageEvents.startWorkerElementError: + logger.error( + `${this.logPrefix()} ${moduleName}.messageHandler: Error occured while starting worker element:`, + msg.data, + ); + this.emit(ChargingStationWorkerMessageEvents.startWorkerElementError, msg.data); + break; + case ChargingStationWorkerMessageEvents.startedWorkerElement: + break; default: throw new BaseError( `Unknown event type: '${msg.event}' for data: ${JSON.stringify(msg.data, null, 2)}`, diff --git a/src/charging-station/ChargingStationWorker.ts b/src/charging-station/ChargingStationWorker.ts index 3a723eca..b20bf3e8 100644 --- a/src/charging-station/ChargingStationWorker.ts +++ b/src/charging-station/ChargingStationWorker.ts @@ -27,11 +27,24 @@ class ChargingStationWorker extends AsyncResource { // Add message listener to create and start charging station from the main thread parentPort?.on('message', (message: WorkerMessage) => { if (message.event === WorkerMessageEvents.startWorkerElement) { - this.runInAsyncScope( - startChargingStation.bind(this) as (data?: ChargingStationWorkerData) => void, - this, - message.data, - ); + try { + this.runInAsyncScope( + startChargingStation.bind(this) as (data?: ChargingStationWorkerData) => void, + this, + message.data, + ); + parentPort?.postMessage({ + event: WorkerMessageEvents.startedWorkerElement, + }); + } catch (error) { + parentPort?.postMessage({ + event: WorkerMessageEvents.startWorkerElementError, + data: { + message: (error as Error).message, + stack: (error as Error).stack, + }, + }); + } } }); } diff --git a/src/worker/WorkerConstants.ts b/src/worker/WorkerConstants.ts index e7572db3..36b23fb1 100644 --- a/src/worker/WorkerConstants.ts +++ b/src/worker/WorkerConstants.ts @@ -12,7 +12,7 @@ export class WorkerConstants { public static readonly DEFAULT_POOL_MAX_SIZE = availableParallelism(); public static readonly DEFAULT_ELEMENTS_PER_WORKER = 1; - public static readonly version = '1.0.0'; + public static readonly version = '1.0.1'; private constructor() { // This is intentional diff --git a/src/worker/WorkerSet.ts b/src/worker/WorkerSet.ts index ce25e7e3..19ed8393 100644 --- a/src/worker/WorkerSet.ts +++ b/src/worker/WorkerSet.ts @@ -94,7 +94,7 @@ export class WorkerSet extends WorkerAbstract { } const workerSetElement = await this.getWorkerSetElement(); workerSetElement.worker.postMessage({ - id: WorkerMessageEvents.startWorkerElement, + event: WorkerMessageEvents.startWorkerElement, data: elementData, }); ++workerSetElement.numberOfWorkerElements; diff --git a/src/worker/WorkerTypes.ts b/src/worker/WorkerTypes.ts index 90a4175f..653cfedf 100644 --- a/src/worker/WorkerTypes.ts +++ b/src/worker/WorkerTypes.ts @@ -51,4 +51,6 @@ export interface WorkerMessage { export enum WorkerMessageEvents { startWorkerElement = 'startWorkerElement', + startWorkerElementError = 'startWorkerElementError', + startedWorkerElement = 'startedWorkerElement', } -- 2.34.1