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 | |
e7aeea18 JB |
16 | public static getWorkerImplementation<T extends WorkerData>( |
17 | workerScript: string, | |
18 | workerProcessType: WorkerProcessType, | |
19 | workerOptions?: WorkerOptions | |
20 | ): WorkerAbstract<T> | null { | |
ded13d97 JB |
21 | if (!isMainThread) { |
22 | throw new Error('Trying to get a worker implementation outside the main thread'); | |
23 | } | |
e7aeea18 JB |
24 | workerOptions = workerOptions ?? ({} as WorkerOptions); |
25 | workerOptions.workerStartDelay = | |
26 | workerOptions?.workerStartDelay ?? Constants.WORKER_START_DELAY; | |
27 | workerOptions.elementStartDelay = | |
28 | workerOptions?.elementStartDelay ?? Constants.ELEMENT_START_DELAY; | |
29 | workerOptions.poolOptions = workerOptions?.poolOptions ?? ({} as PoolOptions<Worker>); | |
30 | workerOptions?.messageHandler && | |
31 | (workerOptions.poolOptions.messageHandler = workerOptions.messageHandler); | |
c3ee95af | 32 | let workerImplementation: WorkerAbstract<T> = null; |
535aaa27 JB |
33 | switch (workerProcessType) { |
34 | case WorkerProcessType.WORKER_SET: | |
e7aeea18 JB |
35 | workerOptions.elementsPerWorker = |
36 | workerOptions?.elementsPerWorker ?? Constants.DEFAULT_CHARGING_STATIONS_PER_WORKER; | |
d070d967 | 37 | workerImplementation = new WorkerSet(workerScript, workerOptions); |
535aaa27 JB |
38 | break; |
39 | case WorkerProcessType.STATIC_POOL: | |
e7aeea18 JB |
40 | workerOptions.poolMaxSize = |
41 | workerOptions?.poolMaxSize ?? Constants.DEFAULT_WORKER_POOL_MAX_SIZE; | |
d070d967 | 42 | workerImplementation = new WorkerStaticPool(workerScript, workerOptions); |
535aaa27 JB |
43 | break; |
44 | case WorkerProcessType.DYNAMIC_POOL: | |
e7aeea18 JB |
45 | workerOptions.poolMinSize = |
46 | workerOptions?.poolMinSize ?? Constants.DEFAULT_WORKER_POOL_MIN_SIZE; | |
47 | workerOptions.poolMaxSize = | |
48 | workerOptions?.poolMaxSize ?? Constants.DEFAULT_WORKER_POOL_MAX_SIZE; | |
d070d967 | 49 | workerImplementation = new WorkerDynamicPool(workerScript, workerOptions); |
535aaa27 | 50 | break; |
fb226c9b JB |
51 | default: |
52 | throw new Error(`Worker implementation type '${workerProcessType}' not found`); | |
6013bc53 | 53 | } |
535aaa27 | 54 | return workerImplementation; |
6013bc53 JB |
55 | } |
56 | } |