fix: fix dynamic import syntax
[poolifier.git] / src / pools / worker.ts
1 import type { CircularArray } from '../circular-array'
2 import type { Queue } from '../queue'
3
4 /**
5 * Callback invoked if the worker has received a message.
6 */
7 export type MessageHandler<Worker extends IWorker> = (
8 this: Worker,
9 m: unknown
10 ) => void
11
12 /**
13 * Callback invoked if the worker raised an error.
14 */
15 export type ErrorHandler<Worker extends IWorker> = (
16 this: Worker,
17 e: Error
18 ) => void
19
20 /**
21 * Callback invoked when the worker has started successfully.
22 */
23 export type OnlineHandler<Worker extends IWorker> = (this: Worker) => void
24
25 /**
26 * Callback invoked when the worker exits successfully.
27 */
28 export type ExitHandler<Worker extends IWorker> = (
29 this: Worker,
30 code: number
31 ) => void
32
33 /**
34 * Message object that is passed as a task between main worker and worker.
35 *
36 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
37 * @internal
38 */
39 export interface Task<Data = unknown> {
40 /**
41 * Task name.
42 */
43 readonly name?: string
44 /**
45 * Task input data that will be passed to the worker.
46 */
47 readonly data?: Data
48 /**
49 * Timestamp.
50 */
51 readonly timestamp?: number
52 /**
53 * Message UUID.
54 */
55 readonly id?: string
56 }
57
58 /**
59 * Measurement statistics.
60 *
61 * @internal
62 */
63 export interface MeasurementStatistics {
64 /**
65 * Measurement aggregate.
66 */
67 aggregate: number
68 /**
69 * Measurement average.
70 */
71 average: number
72 /**
73 * Measurement median.
74 */
75 median: number
76 /**
77 * Measurement history.
78 */
79 history: CircularArray<number>
80 }
81
82 /**
83 * Event loop utilization measurement statistics.
84 *
85 * @internal
86 */
87 export interface EventLoopUtilizationMeasurementStatistics {
88 idle: MeasurementStatistics
89 active: MeasurementStatistics
90 utilization: number
91 }
92
93 /**
94 * Task statistics.
95 *
96 * @internal
97 */
98 export interface TaskStatistics {
99 /**
100 * Number of executed tasks.
101 */
102 executed: number
103 /**
104 * Number of executing tasks.
105 */
106 executing: number
107 /**
108 * Number of queued tasks.
109 */
110 readonly queued: number
111 /**
112 * Maximum number of queued tasks.
113 */
114 readonly maxQueued: number
115 /**
116 * Number of failed tasks.
117 */
118 failed: number
119 }
120
121 /**
122 * Worker usage statistics.
123 *
124 * @internal
125 */
126 export interface WorkerUsage {
127 /**
128 * Tasks statistics.
129 */
130 tasks: TaskStatistics
131 /**
132 * Tasks runtime statistics.
133 */
134 runTime: MeasurementStatistics
135 /**
136 * Tasks wait time statistics.
137 */
138 waitTime: MeasurementStatistics
139 /**
140 * Tasks event loop utilization statistics.
141 */
142 elu: EventLoopUtilizationMeasurementStatistics
143 }
144
145 /**
146 * Worker interface.
147 */
148 export interface IWorker {
149 /**
150 * Register an event listener.
151 *
152 * @param event - The event.
153 * @param handler - The event handler.
154 */
155 on: ((event: 'message', handler: MessageHandler<this>) => void) &
156 ((event: 'error', handler: ErrorHandler<this>) => void) &
157 ((event: 'online', handler: OnlineHandler<this>) => void) &
158 ((event: 'exit', handler: ExitHandler<this>) => void)
159 /**
160 * Register a listener to the exit event that will only be performed once.
161 *
162 * @param event - `'exit'`.
163 * @param handler - The exit handler.
164 */
165 once: (event: 'exit', handler: ExitHandler<this>) => void
166 }
167
168 /**
169 * Worker node interface.
170 *
171 * @typeParam Worker - Type of worker.
172 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
173 * @internal
174 */
175 export interface WorkerNode<Worker extends IWorker, Data = unknown> {
176 /**
177 * Worker node worker.
178 */
179 readonly worker: Worker
180 /**
181 * Worker node worker usage statistics.
182 */
183 workerUsage: WorkerUsage
184 /**
185 * Worker node tasks queue.
186 */
187 readonly tasksQueue: Queue<Task<Data>>
188 }