fix: ensure task function ops sync worker choice strategies
[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
7d91a8cd
JB
103 /**
104 * Array of transferable objects.
105 */
6a3ecc50 106 readonly transferList?: readonly TransferListItem[]
5c4d16da
JB
107 /**
108 * Timestamp.
109 */
110 readonly timestamp?: number
111 /**
7629bdf1 112 * Task UUID.
5c4d16da 113 */
7629bdf1 114 readonly taskId?: string
5c4d16da
JB
115}
116
729c563d 117/**
02706357 118 * Message object that is passed between main worker and worker.
c319c66b 119 *
e102732c
JB
120 * @typeParam Data - Type of data sent to the worker or execution response. This can only be structured-cloneable data.
121 * @typeParam ErrorData - Type of data sent to the worker triggering an error. This can only be structured-cloneable data.
71ebe93b 122 * @internal
729c563d 123 */
6677a3d3
JB
124export interface MessageValue<Data = unknown, ErrorData = unknown>
125 extends Task<Data> {
7379799c
JB
126 /**
127 * Worker id.
128 */
129 readonly workerId?: number
729c563d
S
130 /**
131 * Kill code.
132 */
1e3214b6 133 readonly kill?: KillBehavior | true | 'success' | 'failure'
729c563d 134 /**
6703b9f4 135 * Worker error.
729c563d 136 */
6703b9f4 137 readonly workerError?: WorkerError<ErrorData>
bf9549ae 138 /**
d715b7bc 139 * Task performance.
62c15a68 140 */
d715b7bc 141 readonly taskPerformance?: TaskPerformance
6703b9f4
JB
142 /**
143 * Task function operation:
6703b9f4 144 * - `'add'` - Add a task function.
16248b23 145 * - `'remove'` - Remove a task function.
6703b9f4
JB
146 * - `'default'` - Set a task function as default.
147 */
edbc15c6
JB
148 readonly taskFunctionOperation?: 'add' | 'remove' | 'default'
149 /**
150 * Whether the task function operation is successful or not.
151 */
6703b9f4
JB
152 readonly taskFunctionOperationStatus?: boolean
153 /**
31847469 154 * Task function properties.
6703b9f4 155 */
31847469 156 readonly taskFunctionProperties?: TaskFunctionProperties
6703b9f4 157 /**
31847469 158 * Task function serialized to string.
6703b9f4 159 */
31847469 160 readonly taskFunction?: string
90d7d101 161 /**
31847469 162 * Task functions properties.
90d7d101 163 */
31847469 164 readonly taskFunctionsProperties?: TaskFunctionProperties[]
b6b32453 165 /**
f59e1027 166 * Whether the worker computes the given statistics or not.
b6b32453
JB
167 */
168 readonly statistics?: WorkerStatistics
f59e1027 169 /**
7c8381eb 170 * Whether the worker is ready or not.
f59e1027 171 */
7c8381eb 172 readonly ready?: boolean
75d3401a 173 /**
b0a4db63 174 * Whether the worker starts or stops its activity check.
75d3401a 175 */
b0a4db63 176 readonly checkActive?: boolean
85aeb3f3
JB
177 /**
178 * Message port.
179 */
180 readonly port?: MessagePort
325f50bc 181}
be0676b3
APA
182
183/**
e5d3809a 184 * An object holding the task execution response promise resolve/reject callbacks.
be0676b3 185 *
e102732c 186 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
c319c66b 187 * @internal
be0676b3 188 */
501aea93 189export interface PromiseResponseWrapper<Response = unknown> {
be0676b3
APA
190 /**
191 * Resolve callback to fulfill the promise.
192 */
d2c73f82 193 readonly resolve: (value: Response | PromiseLike<Response>) => void
be0676b3
APA
194 /**
195 * Reject callback to reject the promise.
196 */
d2c73f82 197 readonly reject: (reason?: unknown) => void
be0676b3 198 /**
e5d3809a 199 * The worker node key executing the task.
be0676b3 200 */
501aea93 201 readonly workerNodeKey: number
f18fd12b
JB
202 /**
203 * The asynchronous resource used to track the task execution.
204 */
205 readonly asyncResource?: AsyncResource
be0676b3 206}
ff3f866a
JB
207
208export type Writable<T> = { -readonly [P in keyof T]: T[P] }