Commit | Line | Data |
---|---|---|
bdaf31cd JB |
1 | import type { IPoolInternal } from '../pool-internal' |
2 | import { PoolType } from '../pool-internal' | |
f06e48d8 | 3 | import type { IWorker } from '../worker' |
10fcfaf4 JB |
4 | import type { |
5 | IWorkerChoiceStrategy, | |
da309861 JB |
6 | RequiredStatistics, |
7 | WorkerChoiceStrategyOptions | |
10fcfaf4 | 8 | } from './selection-strategies-types' |
bdaf31cd JB |
9 | |
10 | /** | |
9cd39dd4 | 11 | * Worker choice strategy abstract base class. |
bdaf31cd | 12 | * |
38e795c1 JB |
13 | * @typeParam Worker - Type of worker which manages the strategy. |
14 | * @typeParam Data - Type of data sent to the worker. This can only be serializable data. | |
15 | * @typeParam Response - Type of response of execution. This can only be serializable data. | |
bdaf31cd JB |
16 | */ |
17 | export abstract class AbstractWorkerChoiceStrategy< | |
f06e48d8 | 18 | Worker extends IWorker, |
b2b1d84e JB |
19 | Data = unknown, |
20 | Response = unknown | |
17393ac8 | 21 | > implements IWorkerChoiceStrategy { |
afc003b2 | 22 | /** @inheritDoc */ |
8b4d4500 | 23 | protected readonly isDynamicPool: boolean |
afc003b2 | 24 | /** @inheritDoc */ |
da309861 | 25 | public readonly requiredStatistics: RequiredStatistics = { |
c6bd2650 | 26 | runTime: false, |
78099a15 JB |
27 | avgRunTime: false, |
28 | medRunTime: false | |
10fcfaf4 | 29 | } |
bdaf31cd JB |
30 | |
31 | /** | |
6533c3e6 | 32 | * Constructs a worker choice strategy bound to the pool. |
bdaf31cd | 33 | * |
38e795c1 | 34 | * @param pool - The pool instance. |
da309861 | 35 | * @param opts - The worker choice strategy options. |
bdaf31cd JB |
36 | */ |
37 | public constructor ( | |
da309861 JB |
38 | protected readonly pool: IPoolInternal<Worker, Data, Response>, |
39 | protected readonly opts: WorkerChoiceStrategyOptions = { medRunTime: false } | |
b8f3418c | 40 | ) { |
f9f00b5f | 41 | this.checkOptions(this.opts) |
b8f3418c | 42 | this.isDynamicPool = this.pool.type === PoolType.DYNAMIC |
1086026a | 43 | this.choose.bind(this) |
b8f3418c | 44 | } |
bdaf31cd | 45 | |
f9f00b5f JB |
46 | private checkOptions (opts: WorkerChoiceStrategyOptions): void { |
47 | if (this.requiredStatistics.avgRunTime && opts.medRunTime === true) { | |
da309861 JB |
48 | this.requiredStatistics.medRunTime = true |
49 | } | |
50 | } | |
51 | ||
afc003b2 | 52 | /** @inheritDoc */ |
a6f7f1b4 | 53 | public abstract reset (): boolean |
ea7a90d3 | 54 | |
afc003b2 | 55 | /** @inheritDoc */ |
c923ce56 | 56 | public abstract choose (): number |
97a2abc3 | 57 | |
afc003b2 | 58 | /** @inheritDoc */ |
f06e48d8 | 59 | public abstract remove (workerNodeKey: number): boolean |
bdaf31cd | 60 | } |