Commit | Line | Data |
---|---|---|
a35560ba | 1 | import EventEmitter from 'events' |
a35560ba | 2 | import type { IPool } from './pool' |
ea7a90d3 | 3 | import type { IPoolWorker } from './pool-worker' |
a35560ba | 4 | |
7c0ba920 JB |
5 | /** |
6 | * Pool types. | |
7 | */ | |
8 | export enum PoolType { | |
9 | FIXED = 'fixed', | |
10 | DYNAMIC = 'dynamic' | |
11 | } | |
12 | ||
bf9549ae JB |
13 | /** |
14 | * Tasks usage statistics. | |
15 | */ | |
16 | export interface TasksUsage { | |
17 | run: number | |
18 | running: number | |
19 | runTime: number | |
20 | avgRunTime: number | |
21 | } | |
22 | ||
a35560ba S |
23 | /** |
24 | * Internal poolifier pool emitter. | |
25 | */ | |
26 | export class PoolEmitter extends EventEmitter {} | |
27 | ||
28 | /** | |
29 | * Internal contract definition for a poolifier pool. | |
30 | * | |
31 | * @template Worker Type of worker which manages this pool. | |
32 | * @template Data Type of data sent to the worker. | |
33 | * @template Response Type of response of execution. | |
34 | */ | |
35 | export interface IPoolInternal< | |
ea7a90d3 | 36 | Worker extends IPoolWorker, |
a35560ba S |
37 | Data = unknown, |
38 | Response = unknown | |
39 | > extends IPool<Data, Response> { | |
40 | /** | |
41 | * List of currently available workers. | |
42 | */ | |
43 | readonly workers: Worker[] | |
44 | ||
ea7a90d3 JB |
45 | /** |
46 | * The workers tasks usage map. | |
47 | * | |
48 | * `key`: The `Worker` | |
49 | * `value`: Worker tasks usage statistics. | |
50 | */ | |
51 | readonly workersTasksUsage: Map<Worker, TasksUsage> | |
52 | ||
a35560ba S |
53 | /** |
54 | * Emitter on which events can be listened to. | |
55 | * | |
56 | * Events that can currently be listened to: | |
57 | * | |
330c983e | 58 | * - `'busy'` |
a35560ba | 59 | */ |
7c0ba920 | 60 | readonly emitter?: PoolEmitter |
a35560ba | 61 | |
a35560ba | 62 | /** |
7c0ba920 | 63 | * Pool type. |
a35560ba | 64 | * |
7c0ba920 | 65 | * If it is `'dynamic'`, it provides the `max` property. |
a35560ba | 66 | */ |
7c0ba920 | 67 | readonly type: PoolType |
ff5e76e1 JB |
68 | |
69 | /** | |
70 | * Maximum number of workers that can be created by this pool. | |
71 | */ | |
72 | readonly max?: number | |
7c0ba920 JB |
73 | |
74 | /** | |
75 | * Whether the pool is busy or not. | |
76 | * | |
77 | * The pool busyness boolean status. | |
78 | */ | |
79 | readonly busy: boolean | |
80 | ||
81 | /** | |
82 | * Number of tasks currently concurrently running. | |
83 | */ | |
84 | readonly numberOfRunningTasks: number | |
85 | ||
86 | /** | |
8accb8d5 | 87 | * Finds a free worker based on the number of tasks the worker has applied. |
7c0ba920 | 88 | * |
bdaf31cd | 89 | * If a worker is found with `0` running tasks, it is detected as free and returned. |
7c0ba920 | 90 | * |
bdaf31cd | 91 | * If no free worker is found, `false` is returned. |
7c0ba920 | 92 | * |
bdaf31cd | 93 | * @returns A free worker if there is one, otherwise `false`. |
7c0ba920 | 94 | */ |
bdaf31cd JB |
95 | findFreeWorker(): Worker | false |
96 | ||
97 | /** | |
8accb8d5 | 98 | * Gets worker index. |
bdaf31cd JB |
99 | * |
100 | * @param worker The worker. | |
101 | * @returns The worker index. | |
102 | */ | |
103 | getWorkerIndex(worker: Worker): number | |
104 | ||
105 | /** | |
8accb8d5 | 106 | * Gets worker running tasks. |
bdaf31cd JB |
107 | * |
108 | * @param worker The worker. | |
109 | * @returns The number of tasks currently running on the worker. | |
110 | */ | |
111 | getWorkerRunningTasks(worker: Worker): number | undefined | |
bf9549ae JB |
112 | |
113 | /** | |
8accb8d5 | 114 | * Gets worker average tasks runtime. |
bf9549ae JB |
115 | * |
116 | * @param worker The worker. | |
23135a89 | 117 | * @returns The average tasks runtime on the worker. |
bf9549ae JB |
118 | */ |
119 | getWorkerAverageTasksRunTime(worker: Worker): number | undefined | |
a35560ba | 120 | } |