Merge pull request #161 from pioardi/issue-70
[poolifier.git] / src / worker / abstract-worker.ts
index c50cfbb081599ed955561edea56a634c93d885ef..6aa34e8e899fda868bf0c1935b656a21d8b67629 100644 (file)
@@ -2,11 +2,11 @@ import { AsyncResource } from 'async_hooks'
 import type { Worker } from 'cluster'
 import type { MessagePort } from 'worker_threads'
 import type { MessageValue } from '../utility-types'
-import type { WorkerOptions } from './worker-options'
-import { killBehaviorEnumeration } from './worker-options'
+import type { KillBehavior, WorkerOptions } from './worker-options'
+import { KillBehaviors } from './worker-options'
 
-const defaultMaxInactiveTime = 1000 * 60
-const defaultKillBehavior = killBehaviorEnumeration.SOFT
+const DEFAULT_MAX_INACTIVE_TIME = 1000 * 60
+const DEFAULT_KILL_BEHAVIOR: KillBehavior = KillBehaviors.SOFT
 
 /**
  * Base class containing some shared logic for all poolifier workers.
@@ -27,7 +27,7 @@ export abstract class AbstractWorker<
   /**
    * The kill behavior set as option on the Worker constructor or a default value.
    */
-  protected readonly killBehavior: string
+  protected readonly killBehavior: KillBehavior
   /**
    * Whether the worker is working asynchronously or not.
    */
@@ -56,13 +56,14 @@ export abstract class AbstractWorker<
     fn: (data: Data) => Response,
     protected mainWorker?: MainWorker | null,
     public readonly opts: WorkerOptions = {
-      killBehavior: defaultKillBehavior,
-      maxInactiveTime: defaultMaxInactiveTime
+      killBehavior: DEFAULT_KILL_BEHAVIOR,
+      maxInactiveTime: DEFAULT_MAX_INACTIVE_TIME
     }
   ) {
     super(type)
-    this.killBehavior = this.opts.killBehavior ?? defaultKillBehavior
-    this.maxInactiveTime = this.opts.maxInactiveTime ?? defaultMaxInactiveTime
+    this.killBehavior = this.opts.killBehavior ?? DEFAULT_KILL_BEHAVIOR
+    this.maxInactiveTime =
+      this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME
     this.async = !!this.opts.async
     this.lastTask = Date.now()
     if (!fn) throw new Error('fn parameter is mandatory')