Apply dependencies update (#359)
[poolifier.git] / src / worker / abstract-worker.ts
index 0a6530e3889c61bb925ce4d787736ba72c104001..26468aeb96ae0d5f838518e2e9308b2e31c16c0e 100644 (file)
@@ -2,6 +2,7 @@ import { AsyncResource } from 'async_hooks'
 import type { Worker } from 'cluster'
 import type { MessagePort } from 'worker_threads'
 import type { MessageValue } from '../utility-types'
+import { EMPTY_FUNCTION } from '../utils'
 import type { KillBehavior, WorkerOptions } from './worker-options'
 import { KillBehaviors } from './worker-options'
 
@@ -12,8 +13,8 @@ const DEFAULT_KILL_BEHAVIOR: KillBehavior = KillBehaviors.SOFT
  * Base class containing some shared logic for all poolifier workers.
  *
  * @template MainWorker Type of main worker.
- * @template Data Type of data this worker receives from pool's execution.
- * @template Response Type of response the worker sends back to the main worker.
+ * @template Data Type of data this worker receives from pool's execution. This can only be serializable data.
+ * @template Response Type of response the worker sends back to the main worker. This can only be serializable data.
  */
 export abstract class AbstractWorker<
   MainWorker extends Worker | MessagePort,
@@ -41,11 +42,6 @@ export abstract class AbstractWorker<
    */
   protected readonly interval?: NodeJS.Timeout
 
-  /**
-   * This value is immediately set to true when the kill from the main worker is received.
-   */
-  private isKilled: boolean = false
-
   /**
    * Constructs a new poolifier worker.
    *
@@ -95,7 +91,6 @@ export abstract class AbstractWorker<
         this.mainWorker = value.parent
       } else if (value.kill) {
         // Here is time to kill this worker, just clearing the interval
-        this.isKilled = true
         if (this.interval) clearInterval(this.interval)
         this.emitDestroy()
       }
@@ -107,7 +102,7 @@ export abstract class AbstractWorker<
    *
    * @param fn The function that should be defined.
    */
-  private checkFunctionInput (fn: (data: Data) => Response) {
+  private checkFunctionInput (fn: (data: Data) => Response): void {
     if (!fn) throw new Error('fn parameter is mandatory')
   }
 
@@ -134,7 +129,7 @@ export abstract class AbstractWorker<
    * Check to see if the worker should be terminated, because its living too long.
    */
   protected checkAlive (): void {
-    if (Date.now() - this.lastTask > this.maxInactiveTime && !this.isKilled) {
+    if (Date.now() - this.lastTask > this.maxInactiveTime) {
       this.sendToMainWorker({ kill: this.killBehavior })
     }
   }
@@ -162,10 +157,10 @@ export abstract class AbstractWorker<
     try {
       const res = fn(value.data)
       this.sendToMainWorker({ data: res, id: value.id })
-      this.lastTask = Date.now()
     } catch (e) {
       const err = this.handleError(e)
       this.sendToMainWorker({ error: err, id: value.id })
+    } finally {
       this.lastTask = Date.now()
     }
   }
@@ -183,13 +178,15 @@ export abstract class AbstractWorker<
     fn(value.data)
       .then(res => {
         this.sendToMainWorker({ data: res, id: value.id })
-        this.lastTask = Date.now()
         return null
       })
       .catch(e => {
         const err = this.handleError(e)
         this.sendToMainWorker({ error: err, id: value.id })
+      })
+      .finally(() => {
         this.lastTask = Date.now()
       })
+      .catch(EMPTY_FUNCTION)
   }
 }