From 7629bdf1633551ff56f7e2108fb27bcd6ae4c68b Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 4 Aug 2023 22:13:44 +0200 Subject: [PATCH] refactor: cleanup internal message data structure 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 | 12 +++++++----- src/utility-types.ts | 4 ++-- src/worker/abstract-worker.ts | 10 +++++----- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 57c50861..aec731a5 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -639,9 +639,9 @@ export abstract class AbstractPool< data: data ?? ({} as Data), timestamp, workerId: this.getWorkerInfo(workerNodeKey).id as number, - id: randomUUID() + taskId: randomUUID() } - this.promiseResponseMap.set(task.id as string, { + this.promiseResponseMap.set(task.taskId as string, { resolve, reject, workerNodeKey @@ -1037,7 +1037,7 @@ export abstract class AbstractPool< if (message.ready != null) { // Worker ready response received from worker this.handleWorkerReadyResponse(message) - } else if (message.id != null) { + } else if (message.taskId != null) { // Task execution response received from worker this.handleTaskExecutionResponse(message) } @@ -1054,7 +1054,9 @@ export abstract class AbstractPool< } private handleTaskExecutionResponse (message: MessageValue): void { - const promiseResponse = this.promiseResponseMap.get(message.id as string) + const promiseResponse = this.promiseResponseMap.get( + message.taskId as string + ) if (promiseResponse != null) { if (message.taskError != null) { this.emitter?.emit(PoolEvents.taskError, message.taskError) @@ -1064,7 +1066,7 @@ export abstract class AbstractPool< } const workerNodeKey = promiseResponse.workerNodeKey this.afterTaskExecutionHook(workerNodeKey, message) - this.promiseResponseMap.delete(message.id as string) + this.promiseResponseMap.delete(message.taskId as string) if ( this.opts.enableTasksQueue === true && this.tasksQueueSize(workerNodeKey) > 0 && diff --git a/src/utility-types.ts b/src/utility-types.ts index be0ed287..123b5d6e 100644 --- a/src/utility-types.ts +++ b/src/utility-types.ts @@ -80,9 +80,9 @@ export interface Task { */ readonly timestamp?: number /** - * Message UUID. + * Task UUID. */ - readonly id?: string + readonly taskId?: string } /** diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 381e4d9a..b9e3deb0 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -302,7 +302,7 @@ export abstract class AbstractWorker< !this.isMain && message.checkActive ? this.startCheckActive() : this.stopCheckActive() - } else if (message.id != null && message.data != null) { + } else if (message.taskId != null && message.data != null) { // Task message received this.run(message) } else if (message.kill === true) { @@ -422,7 +422,7 @@ export abstract class AbstractWorker< data: res, taskPerformance, workerId: this.id, - id: task.id + taskId: task.taskId }) } catch (e) { const errorMessage = this.handleError(e as Error | string) @@ -433,7 +433,7 @@ export abstract class AbstractWorker< data: task.data }, workerId: this.id, - id: task.id + taskId: task.taskId }) } finally { this.updateLastTaskTimestamp() @@ -458,7 +458,7 @@ export abstract class AbstractWorker< data: res, taskPerformance, workerId: this.id, - id: task.id + taskId: task.taskId }) return null }) @@ -471,7 +471,7 @@ export abstract class AbstractWorker< data: task.data }, workerId: this.id, - id: task.id + taskId: task.taskId }) }) .finally(() => { -- 2.34.1