refactor: add sanity checks to worker code
authorJérôme Benoit <jerome.benoit@sap.com>
Wed, 19 Jul 2023 10:26:54 +0000 (12:26 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Wed, 19 Jul 2023 10:26:54 +0000 (12:26 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/worker/abstract-worker.ts

index 2f4212f9cc884e5b63745949ae3bf521b2ab1b57..a431f5a7fb0e9e72771dc848c0245fb775078854 100644 (file)
@@ -299,13 +299,15 @@ export abstract class AbstractWorker<
         this.statistics = message.statistics
       } else if (message.checkActive != null) {
         // Check active message received
-        message.checkActive ? this.startCheckActive() : this.stopCheckActive()
+        !this.isMain && message.checkActive
+          ? this.startCheckActive()
+          : this.stopCheckActive()
       } else if (message.id != null && message.data != null) {
         // Task message received
         this.run(message)
       } else if (message.kill === true) {
         // Kill message received
-        this.stopCheckActive()
+        !this.isMain && this.stopCheckActive()
         this.emitDestroy()
       }
     }
@@ -389,6 +391,9 @@ export abstract class AbstractWorker<
    * @throws {@link https://nodejs.org/api/errors.html#class-error} If the task function is not found.
    */
   protected run (task: Task<Data>): void {
+    if (this.isMain) {
+      throw new Error('Cannot run a task in the main worker')
+    }
     const fn = this.getTaskFunction(task.name)
     if (isAsyncFunction(fn)) {
       this.runInAsyncScope(this.runAsync.bind(this), this, fn, task)