X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fpool-internal.ts;h=6e2850a892d8d63f024e09d6db40e23b43129c06;hb=refs%2Ftags%2Fv2.3.1;hp=d641fcf9d9fec246ee26a92a67a78d348b3dacf3;hpb=ff5e76e152be8540cba8118bb4e2b9da314dfff5;p=poolifier.git diff --git a/src/pools/pool-internal.ts b/src/pools/pool-internal.ts index d641fcf9..6e2850a8 100644 --- a/src/pools/pool-internal.ts +++ b/src/pools/pool-internal.ts @@ -1,7 +1,24 @@ import EventEmitter from 'events' -import type { MessageValue } from '../utility-types' -import type { IWorker } from './abstract-pool' import type { IPool } from './pool' +import type { IPoolWorker } from './pool-worker' + +/** + * Pool types. + */ +export enum PoolType { + FIXED = 'fixed', + DYNAMIC = 'dynamic' +} + +/** + * Tasks usage statistics. + */ +export interface TasksUsage { + run: number + running: number + runTime: number + avgRunTime: number +} /** * Internal poolifier pool emitter. @@ -16,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 { @@ -26,28 +43,28 @@ 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 + readonly workersTasksUsage: Map /** * Emitter on which events can be listened to. * * Events that can currently be listened to: * - * - `'FullPool'` + * - `'busy'` */ - readonly emitter: PoolEmitter + readonly emitter?: PoolEmitter /** - * Whether the pool is dynamic or not. + * Pool type. * - * If it is dynamic, it provides the `max` property. + * If it is `'dynamic'`, it provides the `max` property. */ - readonly dynamic: boolean + readonly type: PoolType /** * Maximum number of workers that can be created by this pool. @@ -55,27 +72,49 @@ export interface IPoolInternal< readonly max?: number /** - * Creates a new worker for this pool and sets it up completely. + * Whether the pool is busy or not. + * + * The pool busyness boolean status. + */ + readonly busy: boolean + + /** + * Number of tasks currently concurrently running. + */ + readonly numberOfRunningTasks: number + + /** + * Find 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 + + /** + * Get worker index. * - * @returns New, completely set up worker. + * @param worker The worker. + * @returns The worker index. */ - createAndSetupWorker(): Worker + getWorkerIndex(worker: Worker): number /** - * Shut down given worker. + * Get worker running tasks. * - * @param worker A worker within `workers`. + * @param worker The worker. + * @returns The number of tasks currently running on the worker. */ - destroyWorker(worker: Worker): void | Promise + getWorkerRunningTasks(worker: Worker): number | undefined /** - * Register a listener callback on a given worker. + * Get worker average tasks runtime. * - * @param worker A worker. - * @param listener A message listener callback. + * @param worker The worker. + * @returns The average tasks runtime on the worker. */ - registerWorkerMessageListener( - worker: Worker, - listener: (message: MessageValue) => void - ): void + getWorkerAverageTasksRunTime(worker: Worker): number | undefined }