refactor: cleanup workerId usage in messaging
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 13 Sep 2023 20:29:25 +0000 (22:29 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 13 Sep 2023 20:29:25 +0000 (22:29 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/worker/abstract-worker.ts
src/worker/cluster-worker.ts
src/worker/thread-worker.ts

index 107d72384166053e5783f121ef14d0a7abbe69ca..11a2263a5b5238ac4f728daa8ed97f6ca013c86f 100644 (file)
@@ -371,8 +371,7 @@ export abstract class AbstractWorker<
       workerError: {
         name: taskFunctionName as string,
         message: this.handleError(response.error as Error | string)
-      },
-      workerId: this.id
+      }
     })
   }
 
@@ -386,11 +385,11 @@ export abstract class AbstractWorker<
     if (isAsyncFunction(this.opts.killHandler)) {
       (this.opts.killHandler?.() as Promise<void>)
         .then(() => {
-          this.sendToMainWorker({ kill: 'success', workerId: this.id })
+          this.sendToMainWorker({ kill: 'success' })
           return null
         })
         .catch(() => {
-          this.sendToMainWorker({ kill: 'failure', workerId: this.id })
+          this.sendToMainWorker({ kill: 'failure' })
         })
         .finally(() => {
           this.emitDestroy()
@@ -400,9 +399,9 @@ export abstract class AbstractWorker<
       try {
         // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
         this.opts.killHandler?.() as void
-        this.sendToMainWorker({ kill: 'success', workerId: this.id })
+        this.sendToMainWorker({ kill: 'success' })
       } catch {
-        this.sendToMainWorker({ kill: 'failure', workerId: this.id })
+        this.sendToMainWorker({ kill: 'failure' })
       } finally {
         this.emitDestroy()
       }
@@ -454,7 +453,7 @@ export abstract class AbstractWorker<
       performance.now() - this.lastTaskTimestamp >
       (this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME)
     ) {
-      this.sendToMainWorker({ kill: this.opts.killBehavior, workerId: this.id })
+      this.sendToMainWorker({ kill: this.opts.killBehavior })
     }
   }
 
@@ -485,8 +484,7 @@ export abstract class AbstractWorker<
    */
   protected sendTaskFunctionNamesToMainWorker (): void {
     this.sendToMainWorker({
-      taskFunctionNames: this.listTaskFunctionNames(),
-      workerId: this.id
+      taskFunctionNames: this.listTaskFunctionNames()
     })
   }
 
@@ -516,7 +514,6 @@ export abstract class AbstractWorker<
           message: `Task function '${name as string}' not found`,
           data
         },
-        workerId: this.id,
         taskId
       })
       return
@@ -546,7 +543,6 @@ export abstract class AbstractWorker<
       this.sendToMainWorker({
         data: res,
         taskPerformance,
-        workerId: this.id,
         taskId
       })
     } catch (error) {
@@ -556,7 +552,6 @@ export abstract class AbstractWorker<
           message: this.handleError(error as Error | string),
           data
         },
-        workerId: this.id,
         taskId
       })
     } finally {
@@ -582,7 +577,6 @@ export abstract class AbstractWorker<
         this.sendToMainWorker({
           data: res,
           taskPerformance,
-          workerId: this.id,
           taskId
         })
         return null
@@ -594,7 +588,6 @@ export abstract class AbstractWorker<
             message: this.handleError(error as Error | string),
             data
           },
-          workerId: this.id,
           taskId
         })
       })
index 1f9895d6e37b25a73911cd84f1c1f6f7404a98b8..201a516c56e1d8ab2a3d2d569c4d54d7f0fd484d 100644 (file)
@@ -48,14 +48,12 @@ export class ClusterWorker<
         this.getMainWorker().on('message', this.messageListener.bind(this))
         this.sendToMainWorker({
           ready: true,
-          taskFunctionNames: this.listTaskFunctionNames(),
-          workerId: this.id
+          taskFunctionNames: this.listTaskFunctionNames()
         })
       } catch {
         this.sendToMainWorker({
           ready: false,
-          taskFunctionNames: this.listTaskFunctionNames(),
-          workerId: this.id
+          taskFunctionNames: this.listTaskFunctionNames()
         })
       }
     }
@@ -68,6 +66,6 @@ export class ClusterWorker<
 
   /** @inheritDoc */
   protected sendToMainWorker (message: MessageValue<Response>): void {
-    this.getMainWorker().send(message)
+    this.getMainWorker().send({ ...message, workerId: this.id })
   }
 }
index ede0b3b5602f6335e2fc0c3409a719fd5edbf5b6..ec6b6a0aa65ac60adb1f089a4923894798953525 100644 (file)
@@ -62,14 +62,12 @@ export class ThreadWorker<
         this.port.on('message', this.messageListener.bind(this))
         this.sendToMainWorker({
           ready: true,
-          taskFunctionNames: this.listTaskFunctionNames(),
-          workerId: this.id
+          taskFunctionNames: this.listTaskFunctionNames()
         })
       } catch {
         this.sendToMainWorker({
           ready: false,
-          taskFunctionNames: this.listTaskFunctionNames(),
-          workerId: this.id
+          taskFunctionNames: this.listTaskFunctionNames()
         })
       }
     }
@@ -89,7 +87,7 @@ export class ThreadWorker<
 
   /** @inheritDoc */
   protected sendToMainWorker (message: MessageValue<Response>): void {
-    this.port.postMessage(message)
+    this.port.postMessage({ ...message, workerId: this.id })
   }
 
   /** @inheritDoc */