feat: add error handling to worker set worker
authorJérôme Benoit <jerome.benoit@sap.com>
Thu, 20 Jul 2023 18:44:03 +0000 (20:44 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Thu, 20 Jul 2023 18:44:03 +0000 (20:44 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/Bootstrap.ts
src/charging-station/ChargingStationWorker.ts
src/worker/WorkerConstants.ts
src/worker/WorkerSet.ts
src/worker/WorkerTypes.ts

index ac1dd88c64b22871644a9cca2597c32678d8075d..4bfea3213d822244acc028d75049f8a711cfe647 100644 (file)
@@ -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)}`,
index 3a723eca25dd5dd86902c510354c9069332643d6..b20bf3e8ba2851e8bd5be467b03cb044ef73a132 100644 (file)
@@ -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<ChargingStationWorkerData>) => {
       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,
+            },
+          });
+        }
       }
     });
   }
index e7572db3d5cc2eb92ff4b1a4238dc1447e7a1a25..36b23fb15a76a009141bbfe64cde7f224771a503 100644 (file)
@@ -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
index ce25e7e3a6e4ec3428072a05fc6e13d90e14c0fd..19ed8393f171a64d37e4e3d3e871cacf81f17539 100644 (file)
@@ -94,7 +94,7 @@ export class WorkerSet extends WorkerAbstract<WorkerData> {
     }
     const workerSetElement = await this.getWorkerSetElement();
     workerSetElement.worker.postMessage({
-      id: WorkerMessageEvents.startWorkerElement,
+      event: WorkerMessageEvents.startWorkerElement,
       data: elementData,
     });
     ++workerSetElement.numberOfWorkerElements;
index 90a4175fdbb97170db6c1c4aa27d75f2433ac3ca..653cfedf66d58c4855387da0393f03b97e9b8af8 100644 (file)
@@ -51,4 +51,6 @@ export interface WorkerMessage<T extends WorkerData> {
 
 export enum WorkerMessageEvents {
   startWorkerElement = 'startWorkerElement',
+  startWorkerElementError = 'startWorkerElementError',
+  startedWorkerElement = 'startedWorkerElement',
 }