Log errors in worker
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 16 Sep 2022 17:38:06 +0000 (19:38 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Fri, 16 Sep 2022 17:38:06 +0000 (19:38 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStationWorker.ts
src/worker/WorkerDynamicPool.ts
src/worker/WorkerSet.ts
src/worker/WorkerStaticPool.ts
src/worker/WorkerUtils.ts

index 73c2cb7f71dc30678cc802a22d7d1da8b56c23d4..4f109bf5023206c826e55f880c278d576ccaafa3 100644 (file)
@@ -49,11 +49,3 @@ function startChargingStation(data: ChargingStationWorkerData): void {
   const station = new ChargingStation(data.index, data.templateFile);
   station.start();
 }
-
-process.on('uncaughtException', (err) => {
-  throw err;
-});
-
-process.on('unhandledRejection', (reason) => {
-  throw reason;
-});
index b2cd20dd10d181fe047db0621571b76d99786eee..68ba56e9a2330c3f5ae550c8abcb845ccead75f8 100644 (file)
@@ -16,6 +16,8 @@ export default class WorkerDynamicPool extends WorkerAbstract<WorkerData> {
    */
   constructor(workerScript: string, workerOptions?: WorkerOptions) {
     super(workerScript, workerOptions);
+    this.workerOptions.poolOptions.errorHandler =
+      this.workerOptions?.poolOptions?.errorHandler ?? WorkerUtils.defaultErrorHandler;
     this.workerOptions.poolOptions.exitHandler =
       this.workerOptions?.poolOptions?.exitHandler ?? WorkerUtils.defaultExitHandler;
     this.pool = new DynamicThreadPool<WorkerData>(
index 956d97de0f07b54f0ce627f4620642205963de2a..2fed62f5669828e0e18d3abf6bcf4e245d74fbf0 100644 (file)
@@ -96,8 +96,8 @@ export default class WorkerSet extends WorkerAbstract<WorkerData> {
         /* This is intentional */
       });
     });
-    worker.on('error', () => {
-      /* This is intentional */
+    worker.on('error', (error) => {
+      WorkerUtils.defaultErrorHandler(error);
     });
     worker.on('exit', (code) => {
       WorkerUtils.defaultExitHandler(code);
index 6fc91524173e614e1fb4ff1a7bae586c5e216b22..23112a6c566ff0b95f6f07cf48b606332ab1b7fd 100644 (file)
@@ -16,6 +16,8 @@ export default class WorkerStaticPool extends WorkerAbstract<WorkerData> {
    */
   constructor(workerScript: string, workerOptions?: WorkerOptions) {
     super(workerScript, workerOptions);
+    this.workerOptions.poolOptions.errorHandler =
+      this.workerOptions?.poolOptions?.errorHandler ?? WorkerUtils.defaultErrorHandler;
     this.workerOptions.poolOptions.exitHandler =
       this.workerOptions?.poolOptions?.exitHandler ?? WorkerUtils.defaultExitHandler;
     this.pool = new FixedThreadPool(
index 00feba445ff939cbc933eac0daf1a12fc0cfb4c4..4d6868f76f35cf77166cfcff54c7e23ccae0036d 100644 (file)
@@ -10,4 +10,8 @@ export class WorkerUtils {
       console.error(chalk.red(`Worker stopped with exit code ${code}`));
     }
   };
+
+  public static defaultErrorHandler = (error: Error): void => {
+    console.error(chalk.red('Worker errored: ', error));
+  };
 }