Commit | Line | Data |
---|---|---|
ffd71f2c | 1 | import { Worker, isMainThread } from 'worker_threads'; |
c3ee95af | 2 | import { WorkerData, WorkerOptions, WorkerProcessType } from '../types/Worker'; |
b8da29bc | 3 | |
322c9192 | 4 | import Constants from '../utils/Constants'; |
ffd71f2c | 5 | import { PoolOptions } from 'poolifier'; |
73b9adec | 6 | import type WorkerAbstract from './WorkerAbstract'; |
a4624c96 | 7 | import WorkerDynamicPool from './WorkerDynamicPool'; |
6013bc53 | 8 | import WorkerSet from './WorkerSet'; |
a4624c96 | 9 | import WorkerStaticPool from './WorkerStaticPool'; |
6013bc53 JB |
10 | |
11 | export default class WorkerFactory { | |
6c3cfef8 JB |
12 | private constructor() { |
13 | // This is intentional | |
14 | } | |
8df3f0a9 | 15 | |
c3ee95af | 16 | public static getWorkerImplementation<T extends WorkerData>(workerScript: string, workerProcessType: WorkerProcessType, options?: WorkerOptions): WorkerAbstract<T> | null { |
ded13d97 JB |
17 | if (!isMainThread) { |
18 | throw new Error('Trying to get a worker implementation outside the main thread'); | |
19 | } | |
535aaa27 | 20 | options = options ?? {} as WorkerOptions; |
4bfd80fa JB |
21 | options.workerStartDelay = options?.workerStartDelay ?? Constants.WORKER_START_DELAY; |
22 | options.elementStartDelay = options?.elementStartDelay ?? Constants.ELEMENT_START_DELAY; | |
ffd71f2c | 23 | options.poolOptions = options?.poolOptions ?? {} as PoolOptions<Worker>; |
b55c9112 | 24 | options?.messageHandler && (options.poolOptions.messageHandler = options.messageHandler); |
c3ee95af | 25 | let workerImplementation: WorkerAbstract<T> = null; |
535aaa27 JB |
26 | switch (workerProcessType) { |
27 | case WorkerProcessType.WORKER_SET: | |
28 | options.elementsPerWorker = options.elementsPerWorker ?? Constants.DEFAULT_CHARGING_STATIONS_PER_WORKER; | |
4581acf3 JB |
29 | workerImplementation = new WorkerSet(workerScript, options.elementsPerWorker, |
30 | { | |
31 | workerStartDelay: options.workerStartDelay, | |
32 | elementStartDelay: options.elementStartDelay | |
33 | }, | |
34 | options); | |
535aaa27 JB |
35 | break; |
36 | case WorkerProcessType.STATIC_POOL: | |
37 | options.poolMaxSize = options.poolMaxSize ?? Constants.DEFAULT_WORKER_POOL_MAX_SIZE; | |
4581acf3 JB |
38 | workerImplementation = new WorkerStaticPool(workerScript, options.poolMaxSize, |
39 | { | |
40 | workerStartDelay: options.workerStartDelay, | |
41 | elementStartDelay: options.elementStartDelay | |
42 | }, | |
43 | options.poolOptions); | |
535aaa27 JB |
44 | break; |
45 | case WorkerProcessType.DYNAMIC_POOL: | |
46 | options.poolMinSize = options.poolMinSize ?? Constants.DEFAULT_WORKER_POOL_MIN_SIZE; | |
47 | options.poolMaxSize = options.poolMaxSize ?? Constants.DEFAULT_WORKER_POOL_MAX_SIZE; | |
4581acf3 JB |
48 | workerImplementation = new WorkerDynamicPool(workerScript, options.poolMinSize, options.poolMaxSize, |
49 | { | |
50 | workerStartDelay: options.workerStartDelay, | |
51 | elementStartDelay: options.elementStartDelay | |
52 | }, | |
53 | options.poolOptions); | |
535aaa27 | 54 | break; |
fb226c9b JB |
55 | default: |
56 | throw new Error(`Worker implementation type '${workerProcessType}' not found`); | |
6013bc53 | 57 | } |
535aaa27 | 58 | return workerImplementation; |
6013bc53 JB |
59 | } |
60 | } |