feat: add tasks wait time account per worker
[poolifier.git] / src / pools / selection-strategies / selection-strategies-types.ts
CommitLineData
bdaf31cd
JB
1/**
2 * Enumeration of worker choice strategies.
3 */
4export const WorkerChoiceStrategies = Object.freeze({
5 /**
6 * Round robin worker selection strategy.
7 */
8 ROUND_ROBIN: 'ROUND_ROBIN',
9 /**
737c6d97 10 * Less used worker selection strategy.
bdaf31cd 11 */
737c6d97 12 LESS_USED: 'LESS_USED',
168c526f
JB
13 /**
14 * Less busy worker selection strategy.
15 */
16 LESS_BUSY: 'LESS_BUSY',
23ff945a
JB
17 /**
18 * Fair share worker selection strategy.
19 */
20 FAIR_SHARE: 'FAIR_SHARE',
b3432a63
JB
21 /**
22 * Weighted round robin worker selection strategy.
23 */
24 WEIGHTED_ROUND_ROBIN: 'WEIGHTED_ROUND_ROBIN'
bdaf31cd
JB
25} as const)
26
27/**
28 * Worker choice strategy.
29 */
30export type WorkerChoiceStrategy = keyof typeof WorkerChoiceStrategies
31
ff733df7
JB
32/**
33 * Worker choice strategy options.
34 */
35export interface WorkerChoiceStrategyOptions {
b0623665
JB
36 /**
37 * Use tasks median run time instead of average run time.
d29bce7c
JB
38 *
39 * @defaultValue false
b0623665 40 */
ff733df7 41 medRunTime?: boolean
0567595a
JB
42 /**
43 * Use tasks median wait time instead of average run time.
44 *
45 * @defaultValue false
46 */
47 medWaitTime?: boolean
08f3f44c
JB
48 /**
49 * Worker weights to use for weighted round robin worker selection strategy.
50 * Weight is the tasks maximum average or median runtime in milliseconds.
51 *
52 * @defaultValue Computed worker weights automatically given the CPU performance.
53 */
54 weights?: Record<number, number>
ff733df7
JB
55}
56
10fcfaf4 57/**
9cd39dd4 58 * Pool worker tasks usage statistics requirements.
71ebe93b
JB
59 *
60 * @internal
10fcfaf4 61 */
78cea37e 62export interface RequiredStatistics {
243a550a
JB
63 /**
64 * Require tasks run time.
65 */
10fcfaf4 66 runTime: boolean
243a550a
JB
67 /**
68 * Require tasks average run time.
69 */
c6bd2650 70 avgRunTime: boolean
243a550a
JB
71 /**
72 * Require tasks median run time.
73 */
78099a15 74 medRunTime: boolean
0567595a
JB
75 /**
76 * Require tasks wait time.
77 */
78 waitTime: boolean
79 /**
80 * Require tasks average wait time.
81 */
82 avgWaitTime: boolean
83 /**
84 * Require tasks median wait time.
85 */
86 medWaitTime: boolean
10fcfaf4
JB
87}
88
bdaf31cd
JB
89/**
90 * Worker choice strategy interface.
bdaf31cd 91 */
17393ac8 92export interface IWorkerChoiceStrategy {
10fcfaf4 93 /**
f06e48d8 94 * Required tasks usage statistics.
10fcfaf4 95 */
ea7a90d3
JB
96 readonly requiredStatistics: RequiredStatistics
97 /**
138d29a8 98 * Resets strategy internals.
a4958de2
JB
99 *
100 * @returns `true` if the reset is successful, `false` otherwise.
ea7a90d3 101 */
78cea37e 102 reset: () => boolean
138d29a8 103 /**
c7e196ba 104 * Updates the worker node key strategy internals.
138d29a8
JB
105 *
106 * @returns `true` if the update is successful, `false` otherwise.
107 */
a4958de2 108 update: (workerNodeKey: number) => boolean
bdaf31cd 109 /**
f06e48d8 110 * Chooses a worker node in the pool and returns its key.
a4958de2
JB
111 *
112 * @returns The worker node key.
bdaf31cd 113 */
c923ce56 114 choose: () => number
97a2abc3 115 /**
c7e196ba 116 * Removes the worker node key from strategy internals.
97a2abc3 117 *
f06e48d8 118 * @param workerNodeKey - The worker node key.
138d29a8 119 * @returns `true` if the worker node key is removed, `false` otherwise.
97a2abc3 120 */
f06e48d8 121 remove: (workerNodeKey: number) => boolean
a20f0ba5
JB
122 /**
123 * Sets the worker choice strategy options.
124 *
125 * @param opts - The worker choice strategy options.
126 */
127 setOptions: (opts: WorkerChoiceStrategyOptions) => void
bdaf31cd 128}