Merge branch 'master' of github.com:poolifier/poolifier
[poolifier.git] / src / worker / abstract-worker.ts
index ba74409e2b5c53aa7ddb3c9a3b34ad8ec2515b11..92d68bcb6e9d4f38fdb3cc87170a038c4f226722 100644 (file)
@@ -148,9 +148,9 @@ export abstract class AbstractWorker<
     if (message.statistics != null) {
       // Statistics message received
       this.statistics = message.statistics
-    } else if (message.dynamic === true) {
-      // Worker dynamic message received
-      this.startCheckAlive()
+    } else if (message.checkAlive != null) {
+      // Check alive message received
+      message.checkAlive ? this.startCheckAlive() : this.stopCheckAlive()
     } else if (message.id != null && message.data != null) {
       // Task message received
       const fn = this.getTaskFunction(message.name)
@@ -159,13 +159,16 @@ export abstract class AbstractWorker<
       } else {
         this.runInAsyncScope(this.runSync.bind(this), this, fn, message)
       }
-    } else if (message.kill != null) {
+    } else if (message.kill === true) {
       // Kill message received
-      this.aliveInterval != null && clearInterval(this.aliveInterval)
+      this.stopCheckAlive()
       this.emitDestroy()
     }
   }
 
+  /**
+   * Starts the worker alive check interval.
+   */
   private startCheckAlive (): void {
     this.lastTaskTimestamp = performance.now()
     this.aliveInterval = setInterval(
@@ -175,6 +178,25 @@ export abstract class AbstractWorker<
     this.checkAlive.bind(this)()
   }
 
+  /**
+   * Stops the worker alive check interval.
+   */
+  private stopCheckAlive (): void {
+    this.aliveInterval != null && clearInterval(this.aliveInterval)
+  }
+
+  /**
+   * Checks if the worker should be terminated, because its living too long.
+   */
+  private checkAlive (): void {
+    if (
+      performance.now() - this.lastTaskTimestamp >
+      (this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME)
+    ) {
+      this.sendToMainWorker({ kill: this.opts.killBehavior })
+    }
+  }
+
   /**
    * Returns the main worker.
    *
@@ -196,18 +218,6 @@ export abstract class AbstractWorker<
     message: MessageValue<Response, Data>
   ): void
 
-  /**
-   * Checks if the worker should be terminated, because its living too long.
-   */
-  protected checkAlive (): void {
-    if (
-      performance.now() - this.lastTaskTimestamp >
-      (this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME)
-    ) {
-      this.sendToMainWorker({ kill: this.opts.killBehavior })
-    }
-  }
-
   /**
    * Handles an error and convert it to a string so it can be sent back to the main worker.
    *