From 0f187001df8a7a1ecf7f54b67e9a24bd95c6b444 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 16 Sep 2022 19:38:06 +0200 Subject: [PATCH] Log errors in 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/ChargingStationWorker.ts | 8 -------- src/worker/WorkerDynamicPool.ts | 2 ++ src/worker/WorkerSet.ts | 4 ++-- src/worker/WorkerStaticPool.ts | 2 ++ src/worker/WorkerUtils.ts | 4 ++++ 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/charging-station/ChargingStationWorker.ts b/src/charging-station/ChargingStationWorker.ts index 73c2cb7f..4f109bf5 100644 --- a/src/charging-station/ChargingStationWorker.ts +++ b/src/charging-station/ChargingStationWorker.ts @@ -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; -}); diff --git a/src/worker/WorkerDynamicPool.ts b/src/worker/WorkerDynamicPool.ts index b2cd20dd..68ba56e9 100644 --- a/src/worker/WorkerDynamicPool.ts +++ b/src/worker/WorkerDynamicPool.ts @@ -16,6 +16,8 @@ export default class WorkerDynamicPool extends WorkerAbstract { */ 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( diff --git a/src/worker/WorkerSet.ts b/src/worker/WorkerSet.ts index 956d97de..2fed62f5 100644 --- a/src/worker/WorkerSet.ts +++ b/src/worker/WorkerSet.ts @@ -96,8 +96,8 @@ export default class WorkerSet extends WorkerAbstract { /* This is intentional */ }); }); - worker.on('error', () => { - /* This is intentional */ + worker.on('error', (error) => { + WorkerUtils.defaultErrorHandler(error); }); worker.on('exit', (code) => { WorkerUtils.defaultExitHandler(code); diff --git a/src/worker/WorkerStaticPool.ts b/src/worker/WorkerStaticPool.ts index 6fc91524..23112a6c 100644 --- a/src/worker/WorkerStaticPool.ts +++ b/src/worker/WorkerStaticPool.ts @@ -16,6 +16,8 @@ export default class WorkerStaticPool extends WorkerAbstract { */ 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( diff --git a/src/worker/WorkerUtils.ts b/src/worker/WorkerUtils.ts index 00feba44..4d6868f7 100644 --- a/src/worker/WorkerUtils.ts +++ b/src/worker/WorkerUtils.ts @@ -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)); + }; } -- 2.34.1