X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerFactory.ts;h=3784e7ab0c572f36a30de55d1181309fa4e2191e;hb=8e4e1939784c644ae9cfd3f23dce1e4cf9f80ddd;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..3784e7ab 100644 --- a/src/worker/WorkerFactory.ts +++ b/src/worker/WorkerFactory.ts @@ -1,13 +1,32 @@ -import Configuration from '../utils/Configuration'; -import WorkerPool from './WorkerPool'; +import { WorkerOptions, WorkerProcessType } from '../types/Worker'; + +import Constants from '../utils/Constants'; +import WorkerAbstract from './WorkerAbstract'; +import WorkerDynamicPool from './WorkerDynamicPool'; import WorkerSet from './WorkerSet'; -import Wrk from './Worker'; +import WorkerStaticPool from './WorkerStaticPool'; +import { isMainThread } from 'worker_threads'; export default class WorkerFactory { - public static getWorkerImpl(workerScript: string): Wrk { - if (Configuration.useWorkerPool()) { - return new WorkerPool(workerScript); + public static getWorkerImplementation(workerScript: string, workerProcessType: WorkerProcessType, options?: WorkerOptions): WorkerAbstract | null { + if (!isMainThread) { + throw new Error('Trying to get a worker implementation outside the main thread'); + } + options = options ?? {} as WorkerOptions; + options.startDelay = options.startDelay ?? Constants.WORKER_START_DELAY; + switch (workerProcessType) { + case WorkerProcessType.WORKER_SET: + options.elementsPerWorker = options.elementsPerWorker ?? Constants.DEFAULT_CHARGING_STATIONS_PER_WORKER; + return new WorkerSet(workerScript, options.elementsPerWorker, options.startDelay); + case WorkerProcessType.STATIC_POOL: + options.poolMaxSize = options.poolMaxSize ?? Constants.DEFAULT_WORKER_POOL_MAX_SIZE; + return new WorkerStaticPool(workerScript, options.poolMaxSize, options.startDelay, options.poolOptions); + case WorkerProcessType.DYNAMIC_POOL: + options.poolMinSize = options.poolMinSize ?? Constants.DEFAULT_WORKER_POOL_MIN_SIZE; + options.poolMaxSize = options.poolMaxSize ?? Constants.DEFAULT_WORKER_POOL_MAX_SIZE; + return new WorkerDynamicPool(workerScript, options.poolMinSize, options.poolMaxSize, options.startDelay, options.poolOptions); + default: + return null; } - return new WorkerSet(workerScript, Configuration.getChargingStationsPerWorker()); } }