X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=inline;f=src%2Fpools%2Fpool-internal.ts;h=3f35ec13e25b79e67c3cb2c9084843adcbf63f99;hb=1a76932b50afaed3047a58cb0365ad4c446460d6;hp=dd71ae71045b555d99b64a24bba516461c77b561;hpb=7c0ba92006a5c188738ffc5ff642c51f172df3d6;p=poolifier.git diff --git a/src/pools/pool-internal.ts b/src/pools/pool-internal.ts index dd71ae71..3f35ec13 100644 --- a/src/pools/pool-internal.ts +++ b/src/pools/pool-internal.ts @@ -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 { @@ -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 + readonly workersTasksUsage: Map /** * 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 }