refactor: cleanup task handling in worker code
[poolifier.git] / src / utility-types.ts
1 import type { EventLoopUtilization } from 'node:perf_hooks'
2 import type { KillBehavior } from './worker/worker-options'
3 import type { IWorker } from './pools/worker'
4
5 /**
6 * Task error.
7 *
8 * @typeParam Data - Type of data sent to the worker triggering an error. This can only be structured-cloneable data.
9 */
10 export interface TaskError<Data = unknown> {
11 /**
12 * Task name triggering the error.
13 */
14 readonly name: string
15 /**
16 * Error message.
17 */
18 readonly message: string
19 /**
20 * Data triggering the error.
21 */
22 readonly data?: Data
23 }
24
25 /**
26 * Task performance.
27 *
28 * @internal
29 */
30 export interface TaskPerformance {
31 /**
32 * Task name.
33 */
34 readonly name: string
35 /**
36 * Task performance timestamp.
37 */
38 readonly timestamp: number
39 /**
40 * Task runtime.
41 */
42 readonly runTime?: number
43 /**
44 * Task event loop utilization.
45 */
46 readonly elu?: EventLoopUtilization
47 }
48
49 /**
50 * Performance statistics computation.
51 *
52 * @internal
53 */
54 export interface WorkerStatistics {
55 runTime: boolean
56 elu: boolean
57 }
58
59 /**
60 * Message object that is passed as a task between main worker and worker.
61 *
62 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
63 * @internal
64 */
65 export interface Task<Data = unknown> {
66 /**
67 * Worker id.
68 */
69 readonly workerId: number
70 /**
71 * Task name.
72 */
73 readonly name?: string
74 /**
75 * Task input data that will be passed to the worker.
76 */
77 readonly data?: Data
78 /**
79 * Timestamp.
80 */
81 readonly timestamp?: number
82 /**
83 * Message UUID.
84 */
85 readonly id?: string
86 }
87
88 /**
89 * Message object that is passed between main worker and worker.
90 *
91 * @typeParam Data - Type of data sent to the worker or execution response. This can only be structured-cloneable data.
92 * @typeParam ErrorData - Type of data sent to the worker triggering an error. This can only be structured-cloneable data.
93 * @internal
94 */
95 export interface MessageValue<Data = unknown, ErrorData = unknown>
96 extends Task<Data> {
97 /**
98 * Kill code.
99 */
100 readonly kill?: KillBehavior | true
101 /**
102 * Task error.
103 */
104 readonly taskError?: TaskError<ErrorData>
105 /**
106 * Task performance.
107 */
108 readonly taskPerformance?: TaskPerformance
109 /**
110 * Whether the worker computes the given statistics or not.
111 */
112 readonly statistics?: WorkerStatistics
113 /**
114 * Whether the worker is ready or not.
115 */
116 readonly ready?: boolean
117 /**
118 * Whether the worker starts or stops its aliveness check.
119 */
120 readonly checkAlive?: boolean
121 }
122
123 /**
124 * An object holding the execution response promise resolve/reject callbacks.
125 *
126 * @typeParam Worker - Type of worker.
127 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
128 * @internal
129 */
130 export interface PromiseResponseWrapper<
131 Worker extends IWorker,
132 Response = unknown
133 > {
134 /**
135 * Resolve callback to fulfill the promise.
136 */
137 readonly resolve: (value: Response) => void
138 /**
139 * Reject callback to reject the promise.
140 */
141 readonly reject: (reason?: string) => void
142 /**
143 * The worker handling the execution.
144 */
145 readonly worker: Worker
146 }