refactor: use task performance data structure in messages
[poolifier.git] / src / utility-types.ts
CommitLineData
fc3e6586
JB
1import type { Worker as ClusterWorker } from 'node:cluster'
2import type { MessagePort } from 'node:worker_threads'
62c15a68 3import type { EventLoopUtilization } from 'node:perf_hooks'
1a81f8af 4import type { KillBehavior } from './worker/worker-options'
02706357 5import type { IWorker, Task } from './pools/worker'
838898f1 6
729c563d 7/**
3832ad95 8 * Make all properties in T non-readonly.
b40ed511
JB
9 *
10 * @typeParam T - Type in which properties will be non-readonly.
729c563d 11 */
325f50bc
S
12export type Draft<T> = { -readonly [P in keyof T]?: T[P] }
13
d715b7bc
JB
14/**
15 * Task performance.
16 */
17export interface TaskPerformance {
18 /**
19 * Task performance timestamp.
20 */
21 timestamp: number
22 /**
23 * Task runtime.
24 */
25 runTime?: number
26 /**
27 * Task wait time.
28 */
29 waitTime?: number
30 /**
31 * Task event loop utilization.
32 */
33 elu?: EventLoopUtilization
34}
35
b6b32453
JB
36/**
37 * Performance statistics computation.
38 */
39export interface WorkerStatistics {
40 runTime: boolean
41 waitTime: boolean
42 elu: boolean
43}
44
729c563d 45/**
02706357 46 * Message object that is passed between main worker and worker.
c319c66b
JB
47 *
48 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
49 * @typeParam MainWorker - Type of main worker.
71ebe93b 50 * @internal
729c563d 51 */
838898f1
S
52export interface MessageValue<
53 Data = unknown,
4c0ada52 54 MainWorker extends ClusterWorker | MessagePort = ClusterWorker | MessagePort
02706357 55> extends Task<Data> {
729c563d
S
56 /**
57 * Kill code.
58 */
1a81f8af 59 readonly kill?: KillBehavior | 1
729c563d 60 /**
91ee39ed 61 * Task error.
729c563d 62 */
325f50bc 63 readonly error?: string
91ee39ed
JB
64 /**
65 * Task data triggering task error.
66 */
67 readonly errorData?: unknown
bf9549ae 68 /**
d715b7bc 69 * Task performance.
62c15a68 70 */
d715b7bc 71 readonly taskPerformance?: TaskPerformance
729c563d
S
72 /**
73 * Reference to main worker.
729c563d 74 */
838898f1 75 readonly parent?: MainWorker
b6b32453
JB
76 /**
77 * Whether to compute the given statistics or not.
78 */
79 readonly statistics?: WorkerStatistics
325f50bc 80}
be0676b3
APA
81
82/**
2740a743 83 * An object holding the execution response promise resolve/reject callbacks.
be0676b3 84 *
ae94ec4d 85 * @typeParam Worker - Type of worker.
2740a743 86 * @typeParam Response - Type of execution response. This can only be serializable data.
c319c66b 87 * @internal
be0676b3 88 */
c923ce56 89export interface PromiseResponseWrapper<
f06e48d8 90 Worker extends IWorker,
c923ce56
JB
91 Response = unknown
92> {
be0676b3
APA
93 /**
94 * Resolve callback to fulfill the promise.
95 */
96 readonly resolve: (value: Response) => void
97 /**
98 * Reject callback to reject the promise.
99 */
100 readonly reject: (reason?: string) => void
101 /**
a3445496 102 * The worker handling the execution.
be0676b3 103 */
c923ce56 104 readonly worker: Worker
be0676b3 105}