feat: conditional task performance computation at the worker level
[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 * Performance statistics computation.
16 */
17 export interface WorkerStatistics {
18 runTime: boolean
19 waitTime: boolean
20 elu: boolean
21 }
22
23 /**
24 * Message object that is passed between main worker and worker.
25 *
26 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
27 * @typeParam MainWorker - Type of main worker.
28 * @internal
29 */
30 export interface MessageValue<
31 Data = unknown,
32 MainWorker extends ClusterWorker | MessagePort = ClusterWorker | MessagePort
33 > extends Task<Data> {
34 /**
35 * Kill code.
36 */
37 readonly kill?: KillBehavior | 1
38 /**
39 * Task error.
40 */
41 readonly error?: string
42 /**
43 * Task data triggering task error.
44 */
45 readonly errorData?: unknown
46 /**
47 * Runtime.
48 */
49 readonly runTime?: number
50 /**
51 * Wait time.
52 */
53 readonly waitTime?: number
54 /**
55 * Event loop utilization.
56 */
57 readonly elu?: EventLoopUtilization
58 /**
59 * Reference to main worker.
60 */
61 readonly parent?: MainWorker
62 /**
63 * Whether to compute the given statistics or not.
64 */
65 readonly statistics?: WorkerStatistics
66 }
67
68 /**
69 * An object holding the execution response promise resolve/reject callbacks.
70 *
71 * @typeParam Worker - Type of worker.
72 * @typeParam Response - Type of execution response. This can only be serializable data.
73 * @internal
74 */
75 export interface PromiseResponseWrapper<
76 Worker extends IWorker,
77 Response = unknown
78 > {
79 /**
80 * Resolve callback to fulfill the promise.
81 */
82 readonly resolve: (value: Response) => void
83 /**
84 * Reject callback to reject the promise.
85 */
86 readonly reject: (reason?: string) => void
87 /**
88 * The worker handling the execution.
89 */
90 readonly worker: Worker
91 }