]>
Commit | Line | Data |
---|---|---|
ded253e2 | 1 | import type { AsyncResource } from 'node:async_hooks' |
62c15a68 | 2 | import type { EventLoopUtilization } from 'node:perf_hooks' |
a1c3f741 | 3 | import type { MessagePort, Transferable } from 'node:worker_threads' |
ded253e2 | 4 | |
31847469 | 5 | import type { WorkerChoiceStrategy } from './pools/selection-strategies/selection-strategies-types.js' |
d35e5717 | 6 | import type { KillBehavior } from './worker/worker-options.js' |
838898f1 | 7 | |
213e817d | 8 | /** |
5de88a2a JB |
9 | * Message object that is passed between main worker and worker. |
10 | * @typeParam Data - Type of data sent to the worker or execution response. This can only be structured-cloneable data. | |
11 | * @typeParam ErrorData - Type of data sent to the worker triggering an error. This can only be structured-cloneable data. | |
12 | * @internal | |
213e817d | 13 | */ |
5de88a2a JB |
14 | export interface MessageValue<Data = unknown, ErrorData = unknown> |
15 | extends Task<Data> { | |
ff128cc9 | 16 | /** |
5de88a2a | 17 | * Whether the worker starts or stops its activity check. |
ff128cc9 | 18 | */ |
5de88a2a | 19 | readonly checkActive?: boolean |
7e582d64 | 20 | /** |
5de88a2a | 21 | * Kill code. |
7e582d64 | 22 | */ |
5de88a2a | 23 | readonly kill?: 'failure' | 'success' | KillBehavior | true |
82f36766 | 24 | /** |
5de88a2a | 25 | * Message port. |
82f36766 | 26 | */ |
5de88a2a | 27 | readonly port?: MessagePort |
82f36766 | 28 | /** |
5de88a2a | 29 | * Whether the worker is ready or not. |
82f36766 | 30 | */ |
5de88a2a | 31 | readonly ready?: boolean |
5fd1e6bf | 32 | /** |
5de88a2a | 33 | * Whether the worker computes the given statistics or not. |
5fd1e6bf | 34 | */ |
5de88a2a | 35 | readonly statistics?: WorkerStatistics |
197b4aa5 | 36 | /** |
5de88a2a | 37 | * Task function serialized to string. |
197b4aa5 | 38 | */ |
5de88a2a | 39 | readonly taskFunction?: string |
d715b7bc | 40 | /** |
5de88a2a JB |
41 | * Task function operation: |
42 | * - `'add'` - Add a task function. | |
43 | * - `'remove'` - Remove a task function. | |
44 | * - `'default'` - Set a task function as default. | |
d715b7bc | 45 | */ |
5de88a2a | 46 | readonly taskFunctionOperation?: 'add' | 'default' | 'remove' |
d715b7bc | 47 | /** |
5de88a2a | 48 | * Whether the task function operation is successful or not. |
d715b7bc | 49 | */ |
5de88a2a | 50 | readonly taskFunctionOperationStatus?: boolean |
d715b7bc | 51 | /** |
5de88a2a | 52 | * Task function properties. |
d715b7bc | 53 | */ |
5de88a2a | 54 | readonly taskFunctionProperties?: TaskFunctionProperties |
5b49e864 | 55 | /** |
5de88a2a | 56 | * Task functions properties. |
5b49e864 | 57 | */ |
5de88a2a | 58 | readonly taskFunctionsProperties?: TaskFunctionProperties[] |
f4289ecb JB |
59 | /** |
60 | * Task operation: | |
61 | * - `'abort'` - Abort a task. | |
62 | */ | |
63 | readonly taskOperation?: 'abort' | |
97231086 | 64 | /** |
5de88a2a | 65 | * Task performance. |
97231086 | 66 | */ |
5de88a2a JB |
67 | readonly taskPerformance?: TaskPerformance |
68 | /** | |
69 | * Worker error. | |
70 | */ | |
71 | readonly workerError?: WorkerError<ErrorData> | |
72 | /** | |
73 | * Worker id. | |
74 | */ | |
75 | readonly workerId?: number | |
b6b32453 JB |
76 | } |
77 | ||
31847469 | 78 | /** |
5de88a2a JB |
79 | * An object holding the task execution response promise resolve/reject callbacks. |
80 | * @typeParam Response - Type of execution response. This can only be structured-cloneable data. | |
81 | * @internal | |
31847469 | 82 | */ |
5de88a2a | 83 | export interface PromiseResponseWrapper<Response = unknown> { |
f4289ecb JB |
84 | /** |
85 | * The task abort signal. | |
86 | */ | |
87 | readonly abortSignal?: AbortSignal | |
31847469 | 88 | /** |
5de88a2a | 89 | * The asynchronous resource used to track the task execution. |
31847469 | 90 | */ |
5de88a2a | 91 | readonly asyncResource?: AsyncResource |
31847469 | 92 | /** |
5de88a2a | 93 | * Reject callback to reject the promise. |
31847469 | 94 | */ |
5de88a2a | 95 | readonly reject: (reason?: unknown) => void |
31847469 | 96 | /** |
5de88a2a | 97 | * Resolve callback to fulfill the promise. |
31847469 | 98 | */ |
5de88a2a JB |
99 | readonly resolve: (value: PromiseLike<Response> | Response) => void |
100 | /** | |
101 | * The worker node key executing the task. | |
102 | */ | |
103 | readonly workerNodeKey: number | |
31847469 JB |
104 | } |
105 | ||
5c4d16da JB |
106 | /** |
107 | * Message object that is passed as a task between main worker and worker. | |
5c4d16da JB |
108 | * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. |
109 | * @internal | |
110 | */ | |
111 | export interface Task<Data = unknown> { | |
f4289ecb JB |
112 | /** |
113 | * Whether the task is abortable or not. | |
114 | */ | |
115 | readonly abortable?: boolean | |
5c4d16da JB |
116 | /** |
117 | * Task input data that will be passed to the worker. | |
118 | */ | |
119 | readonly data?: Data | |
97231086 JB |
120 | /** |
121 | * Task name. | |
122 | */ | |
123 | readonly name?: string | |
95d1a734 JB |
124 | /** |
125 | * Task priority. Lower values have higher priority. | |
95d1a734 JB |
126 | * @defaultValue 0 |
127 | */ | |
128 | readonly priority?: number | |
129 | /** | |
130 | * Task worker choice strategy. | |
131 | */ | |
132 | readonly strategy?: WorkerChoiceStrategy | |
7d91a8cd | 133 | /** |
97231086 | 134 | * Task UUID. |
7d91a8cd | 135 | */ |
97231086 | 136 | readonly taskId?: `${string}-${string}-${string}-${string}-${string}` |
5c4d16da JB |
137 | /** |
138 | * Timestamp. | |
139 | */ | |
140 | readonly timestamp?: number | |
141 | /** | |
97231086 | 142 | * Array of transferable objects. |
5c4d16da | 143 | */ |
a1c3f741 | 144 | readonly transferList?: readonly Transferable[] |
5c4d16da JB |
145 | } |
146 | ||
729c563d | 147 | /** |
5de88a2a | 148 | * Task function properties. |
729c563d | 149 | */ |
5de88a2a | 150 | export interface TaskFunctionProperties { |
729c563d | 151 | /** |
5de88a2a | 152 | * Task function name. |
729c563d | 153 | */ |
5de88a2a | 154 | readonly name: string |
729c563d | 155 | /** |
5de88a2a | 156 | * Task function priority. Lower values have higher priority. |
729c563d | 157 | */ |
5de88a2a | 158 | readonly priority?: number |
bf9549ae | 159 | /** |
5de88a2a | 160 | * Task function worker choice strategy. |
62c15a68 | 161 | */ |
5de88a2a JB |
162 | readonly strategy?: WorkerChoiceStrategy |
163 | } | |
164 | ||
165 | /** | |
166 | * Task performance. | |
167 | * @internal | |
168 | */ | |
169 | export interface TaskPerformance { | |
97231086 | 170 | /** |
5de88a2a | 171 | * Task event loop utilization. |
97231086 | 172 | */ |
5de88a2a | 173 | readonly elu?: EventLoopUtilization |
97231086 | 174 | /** |
5de88a2a | 175 | * Task name. |
97231086 | 176 | */ |
5de88a2a | 177 | readonly name: string |
6703b9f4 | 178 | /** |
5de88a2a | 179 | * Task runtime. |
6703b9f4 | 180 | */ |
5de88a2a | 181 | readonly runTime?: number |
edbc15c6 | 182 | /** |
5de88a2a | 183 | * Task performance timestamp. |
edbc15c6 | 184 | */ |
5de88a2a JB |
185 | readonly timestamp: number |
186 | } | |
187 | ||
188 | /** | |
189 | * Worker error. | |
190 | * @typeParam Data - Type of data sent to the worker triggering an error. This can only be structured-cloneable data. | |
191 | */ | |
192 | export interface WorkerError<Data = unknown> { | |
f4289ecb JB |
193 | /** |
194 | * Whether the error is an abort error or not. | |
195 | */ | |
196 | readonly aborted: boolean | |
6703b9f4 | 197 | /** |
5de88a2a | 198 | * Data triggering the error. |
6703b9f4 | 199 | */ |
5de88a2a | 200 | readonly data?: Data |
90d7d101 | 201 | /** |
5de88a2a | 202 | * Error object. |
90d7d101 | 203 | */ |
5de88a2a | 204 | readonly error?: Error |
b6b32453 | 205 | /** |
5de88a2a | 206 | * Error message. |
f59e1027 | 207 | */ |
5de88a2a | 208 | readonly message: string |
75d3401a | 209 | /** |
5de88a2a | 210 | * Task function name triggering the error. |
75d3401a | 211 | */ |
5de88a2a | 212 | readonly name?: string |
85aeb3f3 | 213 | /** |
5de88a2a | 214 | * Error stack trace. |
85aeb3f3 | 215 | */ |
5de88a2a | 216 | readonly stack?: string |
325f50bc | 217 | } |
be0676b3 APA |
218 | |
219 | /** | |
5de88a2a | 220 | * Worker task performance statistics computation settings. |
c319c66b | 221 | * @internal |
be0676b3 | 222 | */ |
5de88a2a | 223 | export interface WorkerStatistics { |
be0676b3 | 224 | /** |
5de88a2a | 225 | * Whether the worker computes the task event loop utilization (ELU) or not. |
be0676b3 | 226 | */ |
5de88a2a | 227 | readonly elu: boolean |
f18fd12b | 228 | /** |
5de88a2a | 229 | * Whether the worker computes the task runtime or not. |
f18fd12b | 230 | */ |
5de88a2a | 231 | readonly runTime: boolean |
be0676b3 | 232 | } |
ff3f866a | 233 | |
097dea68 JB |
234 | /** |
235 | * Remove readonly modifier from all properties of T. | |
236 | * @typeParam T - Type to remove readonly modifier. | |
237 | * @internal | |
238 | */ | |
ff3f866a | 239 | export type Writable<T> = { -readonly [P in keyof T]: T[P] } |