fix: ensure worker node cannot be instantiaed without proper arguments
[poolifier.git] / src / pools / abstract-pool.ts
index dceef6decb534f083510d90a947a05861c2ec2df..183344f84b42a2a55411788d1d414fb82383f9dc 100644 (file)
@@ -84,6 +84,11 @@ export abstract class AbstractPool<
   Response
   >
 
+  /**
+   * Dynamic pool maximum size property placeholder.
+   */
+  protected readonly max?: number
+
   /**
    * Whether the pool is starting or not.
    */
@@ -117,11 +122,6 @@ export abstract class AbstractPool<
     this.chooseWorkerNode = this.chooseWorkerNode.bind(this)
     this.executeTask = this.executeTask.bind(this)
     this.enqueueTask = this.enqueueTask.bind(this)
-    this.dequeueTask = this.dequeueTask.bind(this)
-    this.checkAndEmitTaskExecutionEvents =
-      this.checkAndEmitTaskExecutionEvents.bind(this)
-    this.checkAndEmitTaskQueuingEvents =
-      this.checkAndEmitTaskQueuingEvents.bind(this)
 
     if (this.opts.enableEvents === true) {
       this.emitter = new PoolEmitter()
@@ -303,7 +303,7 @@ export abstract class AbstractPool<
       tasksQueueOptions.concurrency <= 0
     ) {
       throw new Error(
-        `Invalid worker tasks concurrency '${tasksQueueOptions.concurrency}' is a negative integer or zero`
+        `Invalid worker tasks concurrency: ${tasksQueueOptions.concurrency} is a negative integer or zero`
       )
     }
   }
@@ -515,12 +515,16 @@ export abstract class AbstractPool<
   /**
    * The pool minimum size.
    */
-  protected abstract get minSize (): number
+  protected get minSize (): number {
+    return this.numberOfWorkers
+  }
 
   /**
    * The pool maximum size.
    */
-  protected abstract get maxSize (): number
+  protected get maxSize (): number {
+    return this.max ?? this.numberOfWorkers
+  }
 
   /**
    * Checks if the worker id sent in the received message from a worker is valid.
@@ -749,7 +753,7 @@ export abstract class AbstractPool<
         await this.destroyWorkerNode(workerNodeKey)
       })
     )
-    this.emitter?.emit(PoolEvents.destroy)
+    this.emitter?.emit(PoolEvents.destroy, this.info)
   }
 
   protected async sendKillMessageToWorker (
@@ -1061,6 +1065,7 @@ export abstract class AbstractPool<
     if (this.workerChoiceStrategyContext.getStrategyPolicy().useDynamicWorker) {
       workerInfo.ready = true
     }
+    this.checkAndEmitDynamicWorkerCreationEvents()
     return workerNodeKey
   }
 
@@ -1227,13 +1232,8 @@ export abstract class AbstractPool<
   }
 
   private checkAndEmitTaskExecutionEvents (): void {
-    if (this.emitter != null) {
-      if (this.busy) {
-        this.emitter.emit(PoolEvents.busy, this.info)
-      }
-      if (this.type === PoolTypes.dynamic && this.full) {
-        this.emitter.emit(PoolEvents.full, this.info)
-      }
+    if (this.busy) {
+      this.emitter?.emit(PoolEvents.busy, this.info)
     }
   }
 
@@ -1243,6 +1243,14 @@ export abstract class AbstractPool<
     }
   }
 
+  private checkAndEmitDynamicWorkerCreationEvents (): void {
+    if (this.type === PoolTypes.dynamic) {
+      if (this.full) {
+        this.emitter?.emit(PoolEvents.full, this.info)
+      }
+    }
+  }
+
   /**
    * Gets the worker information given its worker node key.
    *