Commit | Line | Data |
---|---|---|
9e45c2c4 | 1 | import EventEmitterAsyncResource 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 | */ | |
9e45c2c4 | 35 | export class PoolEmitter extends EventEmitterAsyncResource {} |
b4904890 | 36 | |
aee46736 JB |
37 | /** |
38 | * Enumeration of pool events. | |
39 | */ | |
40 | export const PoolEvents = Object.freeze({ | |
41 | full: 'full', | |
1f68cede | 42 | busy: 'busy', |
91ee39ed JB |
43 | error: 'error', |
44 | taskError: 'taskError' | |
aee46736 JB |
45 | } as const) |
46 | ||
47 | /** | |
48 | * Pool event. | |
49 | */ | |
50 | export type PoolEvent = keyof typeof PoolEvents | |
51 | ||
7171d33f JB |
52 | /** |
53 | * Worker tasks queue options. | |
54 | */ | |
55 | export interface TasksQueueOptions { | |
56 | /** | |
57 | * Maximum number of tasks that can be executed concurrently on a worker. | |
58 | * | |
59 | * @defaultValue 1 | |
60 | */ | |
61 | concurrency?: number | |
62 | } | |
63 | ||
bdaf31cd JB |
64 | /** |
65 | * Options for a poolifier pool. | |
c319c66b | 66 | * |
d480d708 | 67 | * @typeParam Worker - Type of worker. |
bdaf31cd | 68 | */ |
50e66724 | 69 | export interface PoolOptions<Worker extends IWorker> { |
bdaf31cd JB |
70 | /** |
71 | * A function that will listen for message event on each worker. | |
72 | */ | |
73 | messageHandler?: MessageHandler<Worker> | |
74 | /** | |
75 | * A function that will listen for error event on each worker. | |
76 | */ | |
77 | errorHandler?: ErrorHandler<Worker> | |
78 | /** | |
79 | * A function that will listen for online event on each worker. | |
80 | */ | |
81 | onlineHandler?: OnlineHandler<Worker> | |
82 | /** | |
83 | * A function that will listen for exit event on each worker. | |
84 | */ | |
85 | exitHandler?: ExitHandler<Worker> | |
86 | /** | |
46e857ca | 87 | * The worker choice strategy to use in this pool. |
d29bce7c | 88 | * |
95ec6006 | 89 | * @defaultValue WorkerChoiceStrategies.ROUND_ROBIN |
bdaf31cd JB |
90 | */ |
91 | workerChoiceStrategy?: WorkerChoiceStrategy | |
da309861 JB |
92 | /** |
93 | * The worker choice strategy options. | |
94 | */ | |
95 | workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions | |
1f68cede JB |
96 | /** |
97 | * Restart worker on error. | |
98 | */ | |
99 | restartWorkerOnError?: boolean | |
bdaf31cd JB |
100 | /** |
101 | * Pool events emission. | |
102 | * | |
38e795c1 | 103 | * @defaultValue true |
bdaf31cd JB |
104 | */ |
105 | enableEvents?: boolean | |
ff733df7 JB |
106 | /** |
107 | * Pool worker tasks queue. | |
108 | * | |
ff733df7 JB |
109 | * @defaultValue false |
110 | */ | |
111 | enableTasksQueue?: boolean | |
7171d33f JB |
112 | /** |
113 | * Pool worker tasks queue options. | |
7171d33f JB |
114 | */ |
115 | tasksQueueOptions?: TasksQueueOptions | |
bdaf31cd | 116 | } |
a35560ba | 117 | |
729c563d S |
118 | /** |
119 | * Contract definition for a poolifier pool. | |
120 | * | |
c4855468 | 121 | * @typeParam Worker - Type of worker which manages this pool. |
38e795c1 | 122 | * @typeParam Data - Type of data sent to the worker. This can only be serializable data. |
02706357 | 123 | * @typeParam Response - Type of execution response. This can only be serializable data. |
729c563d | 124 | */ |
c4855468 JB |
125 | export interface IPool< |
126 | Worker extends IWorker, | |
127 | Data = unknown, | |
128 | Response = unknown | |
129 | > { | |
130 | /** | |
131 | * Pool type. | |
132 | * | |
133 | * If it is `'dynamic'`, it provides the `max` property. | |
134 | */ | |
135 | readonly type: PoolType | |
08f3f44c JB |
136 | /** |
137 | * Pool maximum size. | |
138 | */ | |
139 | readonly size: number | |
c4855468 JB |
140 | /** |
141 | * Pool worker nodes. | |
142 | */ | |
143 | readonly workerNodes: Array<WorkerNode<Worker, Data>> | |
b4904890 JB |
144 | /** |
145 | * Emitter on which events can be listened to. | |
146 | * | |
147 | * Events that can currently be listened to: | |
148 | * | |
164d950a JB |
149 | * - `'full'`: Emitted when the pool is dynamic and full. |
150 | * - `'busy'`: Emitted when the pool is busy. | |
91ee39ed JB |
151 | * - `'error'`: Emitted when an uncaught error occurs. |
152 | * - `'taskError'`: Emitted when an error occurs while executing a task. | |
b4904890 JB |
153 | */ |
154 | readonly emitter?: PoolEmitter | |
729c563d | 155 | /** |
61aa11a6 | 156 | * Executes the specified function in the worker constructor with the task data input parameter. |
729c563d | 157 | * |
ef41a6e6 | 158 | * @param data - The task input data for the specified worker function. This can only be serializable data. |
a86b6df1 | 159 | * @param name - The name of the worker function to execute. If not specified, the default worker function will be executed. |
ef41a6e6 | 160 | * @returns Promise that will be fulfilled when the task is completed. |
729c563d | 161 | */ |
a86b6df1 | 162 | execute: (data?: Data, name?: string) => Promise<Response> |
280c2a77 | 163 | /** |
675bb809 | 164 | * Shutdowns every current worker in this pool. |
280c2a77 | 165 | */ |
78cea37e | 166 | destroy: () => Promise<void> |
a35560ba | 167 | /** |
bdede008 | 168 | * Sets the worker choice strategy in this pool. |
a35560ba | 169 | * |
38e795c1 | 170 | * @param workerChoiceStrategy - The worker choice strategy. |
59219cbb | 171 | * @param workerChoiceStrategyOptions - The worker choice strategy options. |
a35560ba | 172 | */ |
59219cbb JB |
173 | setWorkerChoiceStrategy: ( |
174 | workerChoiceStrategy: WorkerChoiceStrategy, | |
175 | workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions | |
176 | ) => void | |
a20f0ba5 JB |
177 | /** |
178 | * Sets the worker choice strategy options in this pool. | |
179 | * | |
180 | * @param workerChoiceStrategyOptions - The worker choice strategy options. | |
181 | */ | |
182 | setWorkerChoiceStrategyOptions: ( | |
183 | workerChoiceStrategyOptions: WorkerChoiceStrategyOptions | |
184 | ) => void | |
185 | /** | |
186 | * Enables/disables the worker tasks queue in this pool. | |
187 | * | |
188 | * @param enable - Whether to enable or disable the worker tasks queue. | |
189 | * @param tasksQueueOptions - The worker tasks queue options. | |
190 | */ | |
8f52842f JB |
191 | enableTasksQueue: ( |
192 | enable: boolean, | |
193 | tasksQueueOptions?: TasksQueueOptions | |
194 | ) => void | |
a20f0ba5 JB |
195 | /** |
196 | * Sets the worker tasks queue options in this pool. | |
197 | * | |
198 | * @param tasksQueueOptions - The worker tasks queue options. | |
199 | */ | |
200 | setTasksQueueOptions: (tasksQueueOptions: TasksQueueOptions) => void | |
c97c7edb | 201 | } |