X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerFactory.ts;h=b297f42b231715643ef14761ca1c45a93295a5cf;hb=e80bc57960515924d5721315fcd930975da52b45;hp=13880b2f81dd8f16c99a97f26949bc44c51f8be6;hpb=6013bc53ce820bacf728a4d85d875c3317ff2442;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerFactory.ts b/src/worker/WorkerFactory.ts index 13880b2f..b297f42b 100644 --- a/src/worker/WorkerFactory.ts +++ b/src/worker/WorkerFactory.ts @@ -1,13 +1,58 @@ -import Configuration from '../utils/Configuration'; -import WorkerPool from './WorkerPool'; +import { Worker, isMainThread } from 'worker_threads'; +import { WorkerData, WorkerOptions, WorkerProcessType } from '../types/Worker'; + +import { PoolOptions } from 'poolifier'; +import type WorkerAbstract from './WorkerAbstract'; +import WorkerConstants from './WorkerConstants'; +import WorkerDynamicPool from './WorkerDynamicPool'; import WorkerSet from './WorkerSet'; -import Wrk from './Worker'; +import WorkerStaticPool from './WorkerStaticPool'; export default class WorkerFactory { - public static getWorkerImpl(workerScript: string): Wrk { - if (Configuration.useWorkerPool()) { - return new WorkerPool(workerScript); + private constructor() { + // This is intentional + } + + public static getWorkerImplementation( + workerScript: string, + workerProcessType: WorkerProcessType, + workerOptions?: WorkerOptions + ): WorkerAbstract | null { + if (!isMainThread) { + throw new Error('Trying to get a worker implementation outside the main thread'); + } + workerOptions = workerOptions ?? ({} as WorkerOptions); + workerOptions.workerStartDelay = + workerOptions?.workerStartDelay ?? WorkerConstants.DEFAULT_WORKER_START_DELAY; + workerOptions.elementStartDelay = + workerOptions?.elementStartDelay ?? WorkerConstants.DEFAULT_ELEMENT_START_DELAY; + workerOptions.poolOptions = workerOptions?.poolOptions ?? ({} as PoolOptions); + workerOptions?.messageHandler && + // eslint-disable-next-line @typescript-eslint/no-misused-promises + (workerOptions.poolOptions.messageHandler = workerOptions.messageHandler); + let workerImplementation: WorkerAbstract = null; + switch (workerProcessType) { + case WorkerProcessType.WORKER_SET: + workerOptions.elementsPerWorker = + workerOptions?.elementsPerWorker ?? WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER; + workerImplementation = new WorkerSet(workerScript, workerOptions); + break; + case WorkerProcessType.STATIC_POOL: + workerOptions.poolMaxSize = + workerOptions?.poolMaxSize ?? WorkerConstants.DEFAULT_POOL_MAX_SIZE; + workerImplementation = new WorkerStaticPool(workerScript, workerOptions); + break; + case WorkerProcessType.DYNAMIC_POOL: + workerOptions.poolMinSize = + workerOptions?.poolMinSize ?? WorkerConstants.DEFAULT_POOL_MIN_SIZE; + workerOptions.poolMaxSize = + workerOptions?.poolMaxSize ?? WorkerConstants.DEFAULT_POOL_MAX_SIZE; + workerImplementation = new WorkerDynamicPool(workerScript, workerOptions); + break; + default: + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions + throw new Error(`Worker implementation type '${workerProcessType}' not found`); } - return new WorkerSet(workerScript, Configuration.getChargingStationsPerWorker()); + return workerImplementation; } }