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. |
d29bce7c | 86 | * |
95ec6006 | 87 | * @defaultValue WorkerChoiceStrategies.ROUND_ROBIN |
bdaf31cd JB |
88 | */ |
89 | workerChoiceStrategy?: WorkerChoiceStrategy | |
da309861 JB |
90 | /** |
91 | * The worker choice strategy options. | |
92 | */ | |
93 | workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions | |
bdaf31cd JB |
94 | /** |
95 | * Pool events emission. | |
96 | * | |
38e795c1 | 97 | * @defaultValue true |
bdaf31cd JB |
98 | */ |
99 | enableEvents?: boolean | |
ff733df7 JB |
100 | /** |
101 | * Pool worker tasks queue. | |
102 | * | |
ff733df7 JB |
103 | * @defaultValue false |
104 | */ | |
105 | enableTasksQueue?: boolean | |
7171d33f JB |
106 | /** |
107 | * Pool worker tasks queue options. | |
7171d33f JB |
108 | */ |
109 | tasksQueueOptions?: TasksQueueOptions | |
bdaf31cd | 110 | } |
a35560ba | 111 | |
729c563d S |
112 | /** |
113 | * Contract definition for a poolifier pool. | |
114 | * | |
c4855468 | 115 | * @typeParam Worker - Type of worker which manages this pool. |
38e795c1 | 116 | * @typeParam Data - Type of data sent to the worker. This can only be serializable data. |
02706357 | 117 | * @typeParam Response - Type of execution response. This can only be serializable data. |
729c563d | 118 | */ |
c4855468 JB |
119 | export interface IPool< |
120 | Worker extends IWorker, | |
121 | Data = unknown, | |
122 | Response = unknown | |
123 | > { | |
124 | /** | |
125 | * Pool type. | |
126 | * | |
127 | * If it is `'dynamic'`, it provides the `max` property. | |
128 | */ | |
129 | readonly type: PoolType | |
08f3f44c JB |
130 | /** |
131 | * Pool maximum size. | |
132 | */ | |
133 | readonly size: number | |
c4855468 JB |
134 | /** |
135 | * Pool worker nodes. | |
136 | */ | |
137 | readonly workerNodes: Array<WorkerNode<Worker, Data>> | |
b4904890 JB |
138 | /** |
139 | * Emitter on which events can be listened to. | |
140 | * | |
141 | * Events that can currently be listened to: | |
142 | * | |
164d950a JB |
143 | * - `'full'`: Emitted when the pool is dynamic and full. |
144 | * - `'busy'`: Emitted when the pool is busy. | |
b4904890 JB |
145 | */ |
146 | readonly emitter?: PoolEmitter | |
729c563d | 147 | /** |
61aa11a6 | 148 | * Executes the specified function in the worker constructor with the task data input parameter. |
729c563d | 149 | * |
ef41a6e6 | 150 | * @param data - The task input data for the specified worker function. This can only be serializable data. |
a86b6df1 | 151 | * @param name - The name of the worker function to execute. If not specified, the default worker function will be executed. |
ef41a6e6 | 152 | * @returns Promise that will be fulfilled when the task is completed. |
729c563d | 153 | */ |
a86b6df1 | 154 | execute: (data?: Data, name?: string) => Promise<Response> |
280c2a77 | 155 | /** |
675bb809 | 156 | * Shutdowns every current worker in this pool. |
280c2a77 | 157 | */ |
78cea37e | 158 | destroy: () => Promise<void> |
a35560ba | 159 | /** |
bdede008 | 160 | * Sets the worker choice strategy in this pool. |
a35560ba | 161 | * |
38e795c1 | 162 | * @param workerChoiceStrategy - The worker choice strategy. |
59219cbb | 163 | * @param workerChoiceStrategyOptions - The worker choice strategy options. |
a35560ba | 164 | */ |
59219cbb JB |
165 | setWorkerChoiceStrategy: ( |
166 | workerChoiceStrategy: WorkerChoiceStrategy, | |
167 | workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions | |
168 | ) => void | |
a20f0ba5 JB |
169 | /** |
170 | * Sets the worker choice strategy options in this pool. | |
171 | * | |
172 | * @param workerChoiceStrategyOptions - The worker choice strategy options. | |
173 | */ | |
174 | setWorkerChoiceStrategyOptions: ( | |
175 | workerChoiceStrategyOptions: WorkerChoiceStrategyOptions | |
176 | ) => void | |
177 | /** | |
178 | * Enables/disables the worker tasks queue in this pool. | |
179 | * | |
180 | * @param enable - Whether to enable or disable the worker tasks queue. | |
181 | * @param tasksQueueOptions - The worker tasks queue options. | |
182 | */ | |
8f52842f JB |
183 | enableTasksQueue: ( |
184 | enable: boolean, | |
185 | tasksQueueOptions?: TasksQueueOptions | |
186 | ) => void | |
a20f0ba5 JB |
187 | /** |
188 | * Sets the worker tasks queue options in this pool. | |
189 | * | |
190 | * @param tasksQueueOptions - The worker tasks queue options. | |
191 | */ | |
192 | setTasksQueueOptions: (tasksQueueOptions: TasksQueueOptions) => void | |
c97c7edb | 193 | } |