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