refactor: abstract out measurement statistics
[poolifier.git] / src / pools / cluster / fixed.ts
index 09c5e369a5459d0858559a29eef8e9fc3a563c66..592b3fe7d4234fe3df267074963b10301ee23707 100644 (file)
@@ -1,9 +1,13 @@
-import type { Worker } from 'cluster'
-import cluster from 'cluster'
+import cluster, { type ClusterSettings, type Worker } from 'node:cluster'
 import type { MessageValue } from '../../utility-types'
 import { AbstractPool } from '../abstract-pool'
-import type { PoolOptions } from '../pool'
-import { PoolType } from '../pool-internal'
+import {
+  type PoolOptions,
+  type PoolType,
+  PoolTypes,
+  type WorkerType,
+  WorkerTypes
+} from '../pool'
 
 /**
  * Options for a poolifier cluster pool.
@@ -14,8 +18,13 @@ export interface ClusterPoolOptions extends PoolOptions<Worker> {
    *
    * @see https://nodejs.org/api/cluster.html#cluster_cluster_fork_env
    */
-  // eslint-disable-next-line @typescript-eslint/no-explicit-any
-  env?: any
+  env?: Record<string, unknown>
+  /**
+   * Cluster settings.
+   *
+   * @see https://nodejs.org/api/cluster.html#cluster_cluster_settings
+   */
+  settings?: ClusterSettings
 }
 
 /**
@@ -25,8 +34,8 @@ export interface ClusterPoolOptions extends PoolOptions<Worker> {
  *
  * This pool selects the workers in a round robin fashion.
  *
- * @template DataType of data sent to the worker. This can only be serializable data.
- * @template ResponseType of response of execution. This can only be serializable data.
+ * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
+ * @typeParam Response - Type of execution response. This can only be serializable data.
  * @author [Christopher Quadflieg](https://github.com/Shinigami92)
  * @since 2.0.0
  */
@@ -37,23 +46,21 @@ export class FixedClusterPool<
   /**
    * Constructs a new poolifier fixed cluster pool.
    *
-   * @param numberOfWorkers Number of workers for this pool.
-   * @param filePath Path to an implementation of a `ClusterWorker` file, which can be relative or absolute.
-   * @param [opts={}] Options for this fixed cluster pool.
+   * @param numberOfWorkers Number of workers for this pool.
+   * @param filePath Path to an implementation of a `ClusterWorker` file, which can be relative or absolute.
+   * @param opts - Options for this fixed cluster pool.
    */
   public constructor (
     numberOfWorkers: number,
     filePath: string,
-    public readonly opts: ClusterPoolOptions = {}
+    protected readonly opts: ClusterPoolOptions = {}
   ) {
     super(numberOfWorkers, filePath, opts)
   }
 
   /** @inheritDoc */
   protected setupHook (): void {
-    cluster.setupPrimary({
-      exec: this.filePath
-    })
+    cluster.setupPrimary({ ...this.opts.settings, exec: this.filePath })
   }
 
   /** @inheritDoc */
@@ -62,7 +69,7 @@ export class FixedClusterPool<
   }
 
   /** @inheritDoc */
-  public destroyWorker (worker: Worker): void {
+  protected destroyWorker (worker: Worker): void {
     this.sendToWorker(worker, { kill: 1 })
     worker.kill()
   }
@@ -73,7 +80,7 @@ export class FixedClusterPool<
   }
 
   /** @inheritDoc */
-  public registerWorkerMessageListener<Message extends Data | Response> (
+  protected registerWorkerMessageListener<Message extends Data | Response>(
     worker: Worker,
     listener: (message: MessageValue<Message>) => void
   ): void {
@@ -87,17 +94,32 @@ export class FixedClusterPool<
 
   /** @inheritDoc */
   protected afterWorkerSetup (worker: Worker): void {
-    // Listen worker messages.
+    // Listen to worker messages.
     this.registerWorkerMessageListener(worker, super.workerListener())
   }
 
   /** @inheritDoc */
-  public get type (): PoolType {
-    return PoolType.FIXED
+  protected get type (): PoolType {
+    return PoolTypes.fixed
+  }
+
+  /** @inheritDoc */
+  protected get worker (): WorkerType {
+    return WorkerTypes.cluster
+  }
+
+  /** @inheritDoc */
+  protected get minSize (): number {
+    return this.numberOfWorkers
+  }
+
+  /** @inheritDoc */
+  protected get maxSize (): number {
+    return this.numberOfWorkers
   }
 
   /** @inheritDoc */
-  public get busy (): boolean {
-    return this.internalGetBusyStatus()
+  protected get busy (): boolean {
+    return this.internalBusy()
   }
 }