Commit | Line | Data |
---|---|---|
fc3e6586 | 1 | import EventEmitter from 'node:events' |
bdaf31cd JB |
2 | import type { |
3 | ErrorHandler, | |
4 | ExitHandler, | |
50e66724 | 5 | IWorker, |
bdaf31cd | 6 | MessageHandler, |
c4855468 JB |
7 | OnlineHandler, |
8 | WorkerNode | |
f06e48d8 | 9 | } from './worker' |
da309861 JB |
10 | import type { |
11 | WorkerChoiceStrategy, | |
12 | WorkerChoiceStrategyOptions | |
13 | } from './selection-strategies/selection-strategies-types' | |
bdaf31cd | 14 | |
c4855468 JB |
15 | /** |
16 | * Pool types. | |
17 | * | |
18 | * @enum | |
71ebe93b | 19 | * @internal |
c4855468 JB |
20 | */ |
21 | export enum PoolType { | |
22 | /** | |
23 | * Fixed pool type. | |
24 | */ | |
25 | FIXED = 'fixed', | |
26 | /** | |
27 | * Dynamic pool type. | |
28 | */ | |
29 | DYNAMIC = 'dynamic' | |
30 | } | |
31 | ||
b4904890 JB |
32 | /** |
33 | * Pool events emitter. | |
34 | */ | |
35 | export class PoolEmitter extends EventEmitter {} | |
36 | ||
aee46736 JB |
37 | /** |
38 | * Enumeration of pool events. | |
39 | */ | |
40 | export const PoolEvents = Object.freeze({ | |
41 | full: 'full', | |
42 | busy: 'busy' | |
43 | } as const) | |
44 | ||
45 | /** | |
46 | * Pool event. | |
47 | */ | |
48 | export type PoolEvent = keyof typeof PoolEvents | |
49 | ||
7171d33f JB |
50 | /** |
51 | * Worker tasks queue options. | |
52 | */ | |
53 | export interface TasksQueueOptions { | |
54 | /** | |
55 | * Maximum number of tasks that can be executed concurrently on a worker. | |
56 | * | |
57 | * @defaultValue 1 | |
58 | */ | |
59 | concurrency?: number | |
60 | } | |
61 | ||
bdaf31cd JB |
62 | /** |
63 | * Options for a poolifier pool. | |
c319c66b | 64 | * |
d480d708 | 65 | * @typeParam Worker - Type of worker. |
bdaf31cd | 66 | */ |
50e66724 | 67 | export interface PoolOptions<Worker extends IWorker> { |
bdaf31cd JB |
68 | /** |
69 | * A function that will listen for message event on each worker. | |
70 | */ | |
71 | messageHandler?: MessageHandler<Worker> | |
72 | /** | |
73 | * A function that will listen for error event on each worker. | |
74 | */ | |
75 | errorHandler?: ErrorHandler<Worker> | |
76 | /** | |
77 | * A function that will listen for online event on each worker. | |
78 | */ | |
79 | onlineHandler?: OnlineHandler<Worker> | |
80 | /** | |
81 | * A function that will listen for exit event on each worker. | |
82 | */ | |
83 | exitHandler?: ExitHandler<Worker> | |
84 | /** | |
46e857ca | 85 | * The worker choice strategy to use in this pool. |
bdaf31cd JB |
86 | */ |
87 | workerChoiceStrategy?: WorkerChoiceStrategy | |
da309861 JB |
88 | /** |
89 | * The worker choice strategy options. | |
90 | */ | |
91 | workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions | |
bdaf31cd JB |
92 | /** |
93 | * Pool events emission. | |
94 | * | |
38e795c1 | 95 | * @defaultValue true |
bdaf31cd JB |
96 | */ |
97 | enableEvents?: boolean | |
ff733df7 JB |
98 | /** |
99 | * Pool worker tasks queue. | |
100 | * | |
101 | * @experimental | |
102 | * @defaultValue false | |
103 | */ | |
104 | enableTasksQueue?: boolean | |
7171d33f JB |
105 | /** |
106 | * Pool worker tasks queue options. | |
107 | * | |
108 | * @experimental | |
7171d33f JB |
109 | */ |
110 | tasksQueueOptions?: TasksQueueOptions | |
bdaf31cd | 111 | } |
a35560ba | 112 | |
729c563d S |
113 | /** |
114 | * Contract definition for a poolifier pool. | |
115 | * | |
c4855468 | 116 | * @typeParam Worker - Type of worker which manages this pool. |
38e795c1 | 117 | * @typeParam Data - Type of data sent to the worker. This can only be serializable data. |
02706357 | 118 | * @typeParam Response - Type of execution response. This can only be serializable data. |
729c563d | 119 | */ |
c4855468 JB |
120 | export interface IPool< |
121 | Worker extends IWorker, | |
122 | Data = unknown, | |
123 | Response = unknown | |
124 | > { | |
125 | /** | |
126 | * Pool type. | |
127 | * | |
128 | * If it is `'dynamic'`, it provides the `max` property. | |
129 | */ | |
130 | readonly type: PoolType | |
131 | /** | |
132 | * Pool worker nodes. | |
133 | */ | |
134 | readonly workerNodes: Array<WorkerNode<Worker, Data>> | |
b4904890 JB |
135 | /** |
136 | * Emitter on which events can be listened to. | |
137 | * | |
138 | * Events that can currently be listened to: | |
139 | * | |
164d950a JB |
140 | * - `'full'`: Emitted when the pool is dynamic and full. |
141 | * - `'busy'`: Emitted when the pool is busy. | |
b4904890 JB |
142 | */ |
143 | readonly emitter?: PoolEmitter | |
c4855468 JB |
144 | /** |
145 | * Finds a free worker node key based on the number of tasks the worker has applied. | |
146 | * | |
147 | * If a worker is found with `0` running tasks, it is detected as free and its worker node key is returned. | |
148 | * | |
149 | * If no free worker is found, `-1` is returned. | |
150 | * | |
151 | * @returns A worker node key if there is one, `-1` otherwise. | |
152 | */ | |
153 | findFreeWorkerNodeKey: () => number | |
729c563d | 154 | /** |
b46d5ebc | 155 | * Executes the function specified in the constructor with the task data parameter. |
729c563d | 156 | * |
38e795c1 | 157 | * @param data - The input for the specified task. This can only be serializable data. |
729c563d S |
158 | * @returns Promise that will be resolved when the task is successfully completed. |
159 | */ | |
78cea37e | 160 | execute: (data: Data) => Promise<Response> |
280c2a77 | 161 | /** |
675bb809 | 162 | * Shutdowns every current worker in this pool. |
280c2a77 | 163 | */ |
78cea37e | 164 | destroy: () => Promise<void> |
a35560ba | 165 | /** |
bdede008 | 166 | * Sets the worker choice strategy in this pool. |
a35560ba | 167 | * |
38e795c1 | 168 | * @param workerChoiceStrategy - The worker choice strategy. |
a35560ba | 169 | */ |
78cea37e | 170 | setWorkerChoiceStrategy: (workerChoiceStrategy: WorkerChoiceStrategy) => void |
c97c7edb | 171 | } |