Add support for [cluster settings](https://nodejs.org/api/cluster.html#cluster_cluste...
[poolifier.git] / src / pools / pool-internal.ts
index dd71ae71045b555d99b64a24bba516461c77b561..3f35ec13e25b79e67c3cb2c9084843adcbf63f99 100644 (file)
@@ -1,6 +1,6 @@
 import EventEmitter from 'events'
-import type { IWorker } from './abstract-pool'
 import type { IPool } from './pool'
+import type { IPoolWorker } from './pool-worker'
 
 /**
  * Pool types.
@@ -10,6 +10,16 @@ export enum PoolType {
   DYNAMIC = 'dynamic'
 }
 
+/**
+ * Tasks usage statistics.
+ */
+export interface TasksUsage {
+  run: number
+  running: number
+  runTime: number
+  avgRunTime: number
+}
+
 /**
  * Internal poolifier pool emitter.
  */
@@ -23,7 +33,7 @@ export class PoolEmitter extends EventEmitter {}
  * @template Response Type of response of execution.
  */
 export interface IPoolInternal<
-  Worker extends IWorker,
+  Worker extends IPoolWorker,
   Data = unknown,
   Response = unknown
 > extends IPool<Data, Response> {
@@ -33,12 +43,12 @@ export interface IPoolInternal<
   readonly workers: Worker[]
 
   /**
-   * The tasks map.
+   * The workers tasks usage map.
    *
-   * - `key`: The `Worker`
-   * - `value`: Number of tasks currently in progress on the worker.
+   *  `key`: The `Worker`
+   *  `value`: Worker tasks usage statistics.
    */
-  readonly tasks: Map<Worker, number>
+  readonly workersTasksUsage: Map<Worker, TasksUsage>
 
   /**
    * Emitter on which events can be listened to.
@@ -74,13 +84,37 @@ export interface IPoolInternal<
   readonly numberOfRunningTasks: number
 
   /**
-   * Find a tasks map entry with a free worker based on the number of tasks the worker has applied.
+   * Finds a free worker based on the number of tasks the worker has applied.
+   *
+   * If a worker is found with `0` running tasks, it is detected as free and returned.
+   *
+   * If no free worker is found, `false` is returned.
+   *
+   * @returns A free worker if there is one, otherwise `false`.
+   */
+  findFreeWorker(): Worker | false
+
+  /**
+   * Gets worker index.
    *
-   * If an entry is found with a worker that has `0` tasks, it is detected as free.
+   * @param worker The worker.
+   * @returns The worker index.
+   */
+  getWorkerIndex(worker: Worker): number
+
+  /**
+   * Gets worker running tasks.
    *
-   * If no tasks map entry with a free worker was found, `false` will be returned.
+   * @param worker The worker.
+   * @returns The number of tasks currently running on the worker.
+   */
+  getWorkerRunningTasks(worker: Worker): number | undefined
+
+  /**
+   * Gets worker average tasks runtime.
    *
-   * @returns A tasks map entry with a free worker if there was one, otherwise `false`.
+   * @param worker The worker.
+   * @returns The average tasks runtime on the worker.
    */
-  findFreeTasksMapEntry(): [Worker, number] | false
+  getWorkerAverageTasksRunTime(worker: Worker): number | undefined
 }