refactor: cleanup internal message data structure
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 4 Aug 2023 20:13:44 +0000 (22:13 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Fri, 4 Aug 2023 20:13:44 +0000 (22:13 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/pools/abstract-pool.ts
src/utility-types.ts
src/worker/abstract-worker.ts

index 57c50861879c9a4225c502898958b9d33a4bbaa6..aec731a5c189903b46230ceceeb831768fa62425 100644 (file)
@@ -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<Response>): 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 &&
index be0ed287633b24e211e46024f736aefc033701e3..123b5d6ea4fa397982c6195de0d9ccd95ba0c1df 100644 (file)
@@ -80,9 +80,9 @@ export interface Task<Data = unknown> {
    */
   readonly timestamp?: number
   /**
-   * Message UUID.
+   * Task UUID.
    */
-  readonly id?: string
+  readonly taskId?: string
 }
 
 /**
index 381e4d9a7357ec70ed425fa4ab9d3ae990a775a7..b9e3deb08676f27aa7f2ab637e060f449811c11c 100644 (file)
@@ -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(() => {