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