refactor: use object destructuration at handling task execution response
[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 if the worker has received a message.
7 */
8 export type MessageHandler<Worker extends IWorker> = (
9 this: Worker,
10 message: unknown
11 ) => void
12
13 /**
14 * Callback invoked if the worker raised an error.
15 */
16 export type ErrorHandler<Worker extends IWorker> = (
17 this: Worker,
18 error: Error
19 ) => void
20
21 /**
22 * Callback invoked when the worker has started successfully.
23 */
24 export type OnlineHandler<Worker extends IWorker> = (this: Worker) => 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 failed tasks.
101 */
102 failed: number
103 }
104
105 /**
106 * Enumeration of worker types.
107 */
108 export const WorkerTypes = Object.freeze({
109 thread: 'thread',
110 cluster: 'cluster'
111 } as const)
112
113 /**
114 * Worker type.
115 */
116 export type WorkerType = keyof typeof WorkerTypes
117
118 /**
119 * Worker information.
120 *
121 * @internal
122 */
123 export interface WorkerInfo {
124 /**
125 * Worker id.
126 */
127 readonly id: number | undefined
128 /**
129 * Worker type.
130 */
131 type: WorkerType
132 /**
133 * Dynamic flag.
134 */
135 dynamic: boolean
136 /**
137 * Ready flag.
138 */
139 ready: boolean
140 /**
141 * Task function names.
142 */
143 taskFunctions?: string[]
144 /**
145 * Message channel.
146 */
147 messageChannel?: MessageChannel
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 - `'exit'`.
197 * @param handler - The exit handler.
198 */
199 readonly once: (event: 'exit', handler: ExitHandler<this>) => void
200 }
201
202 /**
203 * Worker node interface.
204 *
205 * @typeParam Worker - Type of worker.
206 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
207 * @internal
208 */
209 export interface IWorkerNode<Worker extends IWorker, Data = unknown> {
210 /**
211 * Worker.
212 */
213 readonly worker: Worker
214 /**
215 * Worker info.
216 */
217 readonly info: WorkerInfo
218 /**
219 * Worker usage statistics.
220 */
221 usage: WorkerUsage
222 /**
223 * Tasks queue size.
224 *
225 * @returns The tasks queue size.
226 */
227 readonly tasksQueueSize: () => number
228 /**
229 * Enqueue task.
230 *
231 * @param task - The task to queue.
232 * @returns The task queue size.
233 */
234 readonly enqueueTask: (task: Task<Data>) => number
235 /**
236 * Dequeue task.
237 *
238 * @returns The dequeued task.
239 */
240 readonly dequeueTask: () => Task<Data> | undefined
241 /**
242 * Clears tasks queue.
243 */
244 readonly clearTasksQueue: () => void
245 /**
246 * Resets usage statistics .
247 */
248 readonly resetUsage: () => void
249 /**
250 * Close communication channel.
251 */
252 readonly closeChannel: () => void
253 /**
254 * Gets task worker usage statistics.
255 */
256 readonly getTaskWorkerUsage: (name: string) => WorkerUsage | undefined
257 }