feat: add ELU tasks accounting
[poolifier.git] / src / utility-types.ts
1 import type { Worker as ClusterWorker } from 'node:cluster'
2 import type { MessagePort } from 'node:worker_threads'
3 import type { EventLoopUtilization } from 'node:perf_hooks'
4 import type { KillBehavior } from './worker/worker-options'
5 import type { IWorker, Task } from './pools/worker'
6
7 /**
8 * Make all properties in T non-readonly.
9 *
10 * @typeParam T - Type in which properties will be non-readonly.
11 */
12 export type Draft<T> = { -readonly [P in keyof T]?: T[P] }
13
14 /**
15 * Message object that is passed between main worker and worker.
16 *
17 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
18 * @typeParam MainWorker - Type of main worker.
19 * @internal
20 */
21 export interface MessageValue<
22 Data = unknown,
23 MainWorker extends ClusterWorker | MessagePort = ClusterWorker | MessagePort
24 > extends Task<Data> {
25 /**
26 * Kill code.
27 */
28 readonly kill?: KillBehavior | 1
29 /**
30 * Task error.
31 */
32 readonly error?: string
33 /**
34 * Task data triggering task error.
35 */
36 readonly errorData?: unknown
37 /**
38 * Runtime.
39 */
40 readonly runTime?: number
41 /**
42 * Wait time.
43 */
44 readonly waitTime?: number
45 /**
46 * Event loop utilization.
47 */
48 readonly elu?: EventLoopUtilization
49 /**
50 * Reference to main worker.
51 */
52 readonly parent?: MainWorker
53 }
54
55 /**
56 * Worker synchronous function that can be executed.
57 *
58 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
59 * @typeParam Response - Type of execution response. This can only be serializable data.
60 */
61 export type WorkerSyncFunction<Data = unknown, Response = unknown> = (
62 data?: Data
63 ) => Response
64
65 /**
66 * Worker asynchronous function that can be executed.
67 * This function must return a promise.
68 *
69 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
70 * @typeParam Response - Type of execution response. This can only be serializable data.
71 */
72 export type WorkerAsyncFunction<Data = unknown, Response = unknown> = (
73 data?: Data
74 ) => Promise<Response>
75
76 /**
77 * Worker function that can be executed.
78 * This function can be synchronous or asynchronous.
79 *
80 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
81 * @typeParam Response - Type of execution response. This can only be serializable data.
82 */
83 export type WorkerFunction<Data = unknown, Response = unknown> =
84 | WorkerSyncFunction<Data, Response>
85 | WorkerAsyncFunction<Data, Response>
86
87 /**
88 * Worker functions that can be executed.
89 * This object can contain synchronous or asynchronous functions.
90 * The key is the name of the function.
91 * The value is the function itself.
92 *
93 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
94 * @typeParam Response - Type of execution response. This can only be serializable data.
95 */
96 export type TaskFunctions<Data = unknown, Response = unknown> = Record<
97 string,
98 WorkerFunction<Data, Response>
99 >
100
101 /**
102 * An object holding the execution response promise resolve/reject callbacks.
103 *
104 * @typeParam Worker - Type of worker.
105 * @typeParam Response - Type of execution response. This can only be serializable data.
106 * @internal
107 */
108 export interface PromiseResponseWrapper<
109 Worker extends IWorker,
110 Response = unknown
111 > {
112 /**
113 * Resolve callback to fulfill the promise.
114 */
115 readonly resolve: (value: Response) => void
116 /**
117 * Reject callback to reject the promise.
118 */
119 readonly reject: (reason?: string) => void
120 /**
121 * The worker handling the execution.
122 */
123 readonly worker: Worker
124 }