refactor: untangle worker choosing code from worker creation code
[poolifier.git] / src / pools / abstract-pool.ts
index 68e833710f802d7bd30f52bab9955dc373072c35..2e033fc1614c0a778c85569d98cb8e014f010bcc 100644 (file)
@@ -93,23 +93,7 @@ export abstract class AbstractPool<
     Worker,
     Data,
     Response
-    >(
-      this,
-      () => {
-        const createdWorker = this.createAndSetupWorker()
-        this.registerWorkerMessageListener(createdWorker, message => {
-          if (
-            isKillBehavior(KillBehaviors.HARD, message.kill) ||
-            this.getWorkerTasksUsage(createdWorker)?.running === 0
-          ) {
-            // Kill received from the worker, means that no new tasks are submitted to that worker for a while ( > maxInactiveTime)
-            void this.destroyWorker(createdWorker)
-          }
-        })
-        return this.getWorkerKey(createdWorker)
-      },
-      this.opts.workerChoiceStrategy
-    )
+    >(this, this.opts.workerChoiceStrategy)
   }
 
   private checkFilePath (filePath: string): void {
@@ -248,11 +232,12 @@ export abstract class AbstractPool<
   /**
    * Setup hook that can be overridden by a Poolifier pool implementation
    * to run code before workers are created in the abstract constructor.
+   * Can be overridden
    *
    * @virtual
    */
   protected setupHook (): void {
-    // Can be overridden
+    // Intentionally empty
   }
 
   /**
@@ -307,7 +292,26 @@ export abstract class AbstractPool<
    * @returns [worker key, worker].
    */
   protected chooseWorker (): [number, Worker] {
-    const workerKey = this.workerChoiceStrategyContext.execute()
+    let workerKey: number
+    if (
+      this.type === PoolType.DYNAMIC &&
+      !this.full &&
+      this.findFreeWorkerKey() === -1
+    ) {
+      const createdWorker = this.createAndSetupWorker()
+      this.registerWorkerMessageListener(createdWorker, message => {
+        if (
+          isKillBehavior(KillBehaviors.HARD, message.kill) ||
+          this.getWorkerTasksUsage(createdWorker)?.running === 0
+        ) {
+          // Kill received from the worker, means that no new tasks are submitted to that worker for a while ( > maxInactiveTime)
+          void this.destroyWorker(createdWorker)
+        }
+      })
+      workerKey = this.getWorkerKey(createdWorker)
+    } else {
+      workerKey = this.workerChoiceStrategyContext.execute()
+    }
     return [workerKey, this.workers[workerKey].worker]
   }