X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerFactory.ts;h=70bcc3862c4fca8c42a66d7e0f52e7568bfbef80;hb=6fee1791db82bd4605b180612fe642a7e2c4caa0;hp=067188d846401d13c3a76c1bdff6236442de6859;hpb=144cabe085e5e1f3821aecb2ef8f388a1030b92b;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerFactory.ts b/src/worker/WorkerFactory.ts index 067188d8..70bcc386 100644 --- a/src/worker/WorkerFactory.ts +++ b/src/worker/WorkerFactory.ts @@ -1,13 +1,60 @@ -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 './Wrk'; +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); + console.log('before'); + let workerImplementation: WorkerAbstract = null; + console.log(workerImplementation); + 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; } }