From 82f36766232abd4e4ffde72ecd68462adf59cca7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 7 Jun 2023 22:43:12 +0200 Subject: [PATCH] refactor: cleanup and type task error messages MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/pools/abstract-pool.ts | 11 ++++------- src/pools/cluster/fixed.ts | 3 +-- src/utility-types.ts | 18 +++++++++++++----- src/worker/abstract-worker.ts | 20 ++++++++++++++------ tests/pools/cluster/fixed.test.js | 8 ++++---- tests/pools/thread/fixed.test.js | 8 ++++---- 6 files changed, 40 insertions(+), 28 deletions(-) diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 85a47e38..ac7da967 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -472,7 +472,7 @@ export abstract class AbstractPool< this.workerNodes[this.getWorkerNodeKey(worker)].tasksUsage --workerTasksUsage.running ++workerTasksUsage.ran - if (message.error != null) { + if (message.taskError != null) { ++workerTasksUsage.error } this.updateRunTimeTasksUsage(workerTasksUsage, message) @@ -662,13 +662,10 @@ export abstract class AbstractPool< // Task execution response received const promiseResponse = this.promiseResponseMap.get(message.id) if (promiseResponse != null) { - if (message.error != null) { - promiseResponse.reject(message.error) + if (message.taskError != null) { + promiseResponse.reject(message.taskError.message) if (this.emitter != null) { - this.emitter.emit(PoolEvents.taskError, { - error: message.error, - errorData: message.errorData - }) + this.emitter.emit(PoolEvents.taskError, message.taskError) } } else { promiseResponse.resolve(message.data as Response) diff --git a/src/pools/cluster/fixed.ts b/src/pools/cluster/fixed.ts index f22906df..592b3fe7 100644 --- a/src/pools/cluster/fixed.ts +++ b/src/pools/cluster/fixed.ts @@ -18,8 +18,7 @@ export interface ClusterPoolOptions extends PoolOptions { * * @see https://nodejs.org/api/cluster.html#cluster_cluster_fork_env */ - // eslint-disable-next-line @typescript-eslint/no-explicit-any - env?: any + env?: Record /** * Cluster settings. * diff --git a/src/utility-types.ts b/src/utility-types.ts index faeda0a5..3917ad20 100644 --- a/src/utility-types.ts +++ b/src/utility-types.ts @@ -11,6 +11,17 @@ import type { IWorker, Task } from './pools/worker' */ export type Draft = { -readonly [P in keyof T]?: T[P] } +export interface TaskError { + /** + * Error message. + */ + message: string + /** + * Data passed to the worker triggering the error. + */ + data?: Data +} + /** * Task performance. */ @@ -51,6 +62,7 @@ export interface WorkerStatistics { */ export interface MessageValue< Data = unknown, + ErrorData = unknown, MainWorker extends ClusterWorker | MessagePort = ClusterWorker | MessagePort > extends Task { /** @@ -60,11 +72,7 @@ export interface MessageValue< /** * Task error. */ - readonly error?: string - /** - * Task data triggering task error. - */ - readonly errorData?: unknown + readonly taskError?: TaskError /** * Task performance. */ diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 7805514a..76a7ee70 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -146,7 +146,9 @@ export abstract class AbstractWorker< * * @param message - Message received. */ - protected messageListener (message: MessageValue): void { + protected messageListener ( + message: MessageValue + ): void { if (message.id != null && message.data != null) { // Task message received const fn = this.getTaskFunction(message.name) @@ -185,7 +187,9 @@ export abstract class AbstractWorker< * * @param message - The response message. */ - protected abstract sendToMainWorker (message: MessageValue): void + protected abstract sendToMainWorker ( + message: MessageValue + ): void /** * Checks if the worker should be terminated, because its living too long. @@ -231,8 +235,10 @@ export abstract class AbstractWorker< } catch (e) { const err = this.handleError(e as Error) this.sendToMainWorker({ - error: err, - errorData: message.data, + taskError: { + message: err, + data: message.data + }, id: message.id }) } finally { @@ -264,8 +270,10 @@ export abstract class AbstractWorker< .catch(e => { const err = this.handleError(e as Error) this.sendToMainWorker({ - error: err, - errorData: message.data, + taskError: { + message: err, + data: message.data + }, id: message.id }) }) diff --git a/tests/pools/cluster/fixed.test.js b/tests/pools/cluster/fixed.test.js index c6cdce54..c879fc68 100644 --- a/tests/pools/cluster/fixed.test.js +++ b/tests/pools/cluster/fixed.test.js @@ -145,8 +145,8 @@ describe('Fixed cluster pool test suite', () => { expect(typeof inError === 'string').toBe(true) expect(inError).toBe('Error Message from ClusterWorker') expect(taskError).toStrictEqual({ - error: 'Error Message from ClusterWorker', - errorData: data + message: 'Error Message from ClusterWorker', + data }) expect( errorPool.workerNodes.some( @@ -171,8 +171,8 @@ describe('Fixed cluster pool test suite', () => { expect(typeof inError === 'string').toBe(true) expect(inError).toBe('Error Message from ClusterWorker:async') expect(taskError).toStrictEqual({ - error: 'Error Message from ClusterWorker:async', - errorData: data + message: 'Error Message from ClusterWorker:async', + data }) expect( asyncErrorPool.workerNodes.some( diff --git a/tests/pools/thread/fixed.test.js b/tests/pools/thread/fixed.test.js index 4c90207c..90dd110c 100644 --- a/tests/pools/thread/fixed.test.js +++ b/tests/pools/thread/fixed.test.js @@ -147,8 +147,8 @@ describe('Fixed thread pool test suite', () => { expect(typeof inError.message === 'string').toBe(true) expect(inError.message).toBe('Error Message from ThreadWorker') expect(taskError).toStrictEqual({ - error: new Error('Error Message from ThreadWorker'), - errorData: data + message: new Error('Error Message from ThreadWorker'), + data }) expect( errorPool.workerNodes.some( @@ -175,8 +175,8 @@ describe('Fixed thread pool test suite', () => { expect(typeof inError.message === 'string').toBe(true) expect(inError.message).toBe('Error Message from ThreadWorker:async') expect(taskError).toStrictEqual({ - error: new Error('Error Message from ThreadWorker:async'), - errorData: data + message: new Error('Error Message from ThreadWorker:async'), + data }) expect( asyncErrorPool.workerNodes.some( -- 2.34.1