Merge branch 'master' of github.com:jerome-benoit/poolifier
[poolifier.git] / src / pools / worker.ts
1 import type { MessageChannel } from 'node:worker_threads'
2 import type { CircularArray } from '../circular-array'
3 import type { Task } from '../utility-types'
4
5 /**
6 * Callback invoked when the worker has started successfully.
7 */
8 export type OnlineHandler<Worker extends IWorker> = (this: Worker) => void
9
10 /**
11 * Callback invoked if the worker has received a message.
12 */
13 export type MessageHandler<Worker extends IWorker> = (
14 this: Worker,
15 message: unknown
16 ) => void
17
18 /**
19 * Callback invoked if the worker raised an error.
20 */
21 export type ErrorHandler<Worker extends IWorker> = (
22 this: Worker,
23 error: Error
24 ) => void
25
26 /**
27 * Callback invoked when the worker exits successfully.
28 */
29 export type ExitHandler<Worker extends IWorker> = (
30 this: Worker,
31 exitCode: number
32 ) => void
33
34 /**
35 * Measurement statistics.
36 *
37 * @internal
38 */
39 export interface MeasurementStatistics {
40 /**
41 * Measurement aggregate.
42 */
43 aggregate?: number
44 /**
45 * Measurement minimum.
46 */
47 minimum?: number
48 /**
49 * Measurement maximum.
50 */
51 maximum?: number
52 /**
53 * Measurement average.
54 */
55 average?: number
56 /**
57 * Measurement median.
58 */
59 median?: number
60 /**
61 * Measurement history.
62 */
63 readonly history: CircularArray<number>
64 }
65
66 /**
67 * Event loop utilization measurement statistics.
68 *
69 * @internal
70 */
71 export interface EventLoopUtilizationMeasurementStatistics {
72 readonly idle: MeasurementStatistics
73 readonly active: MeasurementStatistics
74 utilization?: number
75 }
76
77 /**
78 * Task statistics.
79 *
80 * @internal
81 */
82 export interface TaskStatistics {
83 /**
84 * Number of executed tasks.
85 */
86 executed: number
87 /**
88 * Number of executing tasks.
89 */
90 executing: number
91 /**
92 * Number of queued tasks.
93 */
94 readonly queued: number
95 /**
96 * Maximum number of queued tasks.
97 */
98 readonly maxQueued?: number
99 /**
100 * Number of stolen tasks.
101 */
102 stolen: number
103 /**
104 * Number of failed tasks.
105 */
106 failed: number
107 }
108
109 /**
110 * Enumeration of worker types.
111 */
112 export const WorkerTypes = Object.freeze({
113 thread: 'thread',
114 cluster: 'cluster'
115 } as const)
116
117 /**
118 * Worker type.
119 */
120 export type WorkerType = keyof typeof WorkerTypes
121
122 /**
123 * Worker information.
124 *
125 * @internal
126 */
127 export interface WorkerInfo {
128 /**
129 * Worker id.
130 */
131 readonly id: number | undefined
132 /**
133 * Worker type.
134 */
135 readonly type: WorkerType
136 /**
137 * Dynamic flag.
138 */
139 dynamic: boolean
140 /**
141 * Ready flag.
142 */
143 ready: boolean
144 /**
145 * Task function names.
146 */
147 taskFunctions?: string[]
148 }
149
150 /**
151 * Worker usage statistics.
152 *
153 * @internal
154 */
155 export interface WorkerUsage {
156 /**
157 * Tasks statistics.
158 */
159 readonly tasks: TaskStatistics
160 /**
161 * Tasks runtime statistics.
162 */
163 readonly runTime: MeasurementStatistics
164 /**
165 * Tasks wait time statistics.
166 */
167 readonly waitTime: MeasurementStatistics
168 /**
169 * Tasks event loop utilization statistics.
170 */
171 readonly elu: EventLoopUtilizationMeasurementStatistics
172 }
173
174 /**
175 * Worker interface.
176 */
177 export interface IWorker {
178 /**
179 * Worker id.
180 */
181 readonly id?: number
182 readonly threadId?: number
183 /**
184 * Registers an event listener.
185 *
186 * @param event - The event.
187 * @param handler - The event handler.
188 */
189 readonly on: ((event: 'online', handler: OnlineHandler<this>) => void) &
190 ((event: 'message', handler: MessageHandler<this>) => void) &
191 ((event: 'error', handler: ErrorHandler<this>) => void) &
192 ((event: 'exit', handler: ExitHandler<this>) => void)
193 /**
194 * Registers a listener to the exit event that will only be performed once.
195 *
196 * @param event - The `'exit'` event.
197 * @param handler - The exit handler.
198 */
199 readonly once: (event: 'exit', handler: ExitHandler<this>) => void
200 }
201
202 /**
203 * Worker node event callback.
204 *
205 * @param workerId - The worker id.
206 * @internal
207 */
208 export type WorkerNodeEventCallback = (workerId: number) => void
209
210 /**
211 * Worker node interface.
212 *
213 * @typeParam Worker - Type of worker.
214 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
215 * @internal
216 */
217 export interface IWorkerNode<Worker extends IWorker, Data = unknown> {
218 /**
219 * Worker.
220 */
221 readonly worker: Worker
222 /**
223 * Worker info.
224 */
225 readonly info: WorkerInfo
226 /**
227 * Worker usage statistics.
228 */
229 readonly usage: WorkerUsage
230 /**
231 * Message channel (worker_threads only).
232 */
233 readonly messageChannel?: MessageChannel
234 /**
235 * Tasks queue back pressure size.
236 * This is the number of tasks that can be enqueued before the worker node has back pressure.
237 */
238 tasksQueueBackPressureSize: number
239 /**
240 * Callback invoked when worker node tasks queue is back pressured.
241 */
242 onBackPressure?: WorkerNodeEventCallback
243 /**
244 * Callback invoked when worker node tasks queue is empty.
245 */
246 onEmptyQueue?: WorkerNodeEventCallback
247 /**
248 * Tasks queue size.
249 *
250 * @returns The tasks queue size.
251 */
252 readonly tasksQueueSize: () => number
253 /**
254 * Enqueue task.
255 *
256 * @param task - The task to queue.
257 * @returns The tasks queue size.
258 */
259 readonly enqueueTask: (task: Task<Data>) => number
260 /**
261 * Prepends a task to the tasks queue.
262 *
263 * @param task - The task to prepend.
264 * @returns The tasks queue size.
265 */
266 readonly unshiftTask: (task: Task<Data>) => number
267 /**
268 * Dequeue task.
269 *
270 * @returns The dequeued task.
271 */
272 readonly dequeueTask: () => Task<Data> | undefined
273 /**
274 * Pops a task from the tasks queue.
275 *
276 * @returns The popped task.
277 */
278 readonly popTask: () => Task<Data> | undefined
279 /**
280 * Clears tasks queue.
281 */
282 readonly clearTasksQueue: () => void
283 /**
284 * Whether the worker node has back pressure (i.e. its tasks queue is full).
285 *
286 * @returns `true` if the worker node has back pressure, `false` otherwise.
287 */
288 readonly hasBackPressure: () => boolean
289 /**
290 * Resets usage statistics.
291 */
292 readonly resetUsage: () => void
293 /**
294 * Closes communication channel.
295 */
296 readonly closeChannel: () => void
297 /**
298 * Gets task function worker usage statistics.
299 *
300 * @param name - The task function name.
301 * @returns The task function worker usage statistics if the task function worker usage statistics are initialized, `undefined` otherwise.
302 */
303 readonly getTaskFunctionWorkerUsage: (name: string) => WorkerUsage | undefined
304 }