X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerFactory.ts;h=41fa1d89715444ca5cd69b07f6684789f63b2ecc;hb=ce4d00ee7295ccdb12f35154eb2eb72973b6e75b;hp=2d923daab084bb95b77a783509053bed4b519a7a;hpb=a4624c96a6c159b4885f5d0baaf592ceec0bab30;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerFactory.ts b/src/worker/WorkerFactory.ts index 2d923daa..41fa1d89 100644 --- a/src/worker/WorkerFactory.ts +++ b/src/worker/WorkerFactory.ts @@ -1,19 +1,39 @@ -import Configuration from '../utils/Configuration'; +import { WorkerOptions, WorkerProcessType } from '../types/Worker'; + +import Utils from '../utils/Utils'; +import WorkerAbstract from './WorkerAbstract'; import WorkerDynamicPool from './WorkerDynamicPool'; -import { WorkerProcessType } from '../types/Worker'; import WorkerSet from './WorkerSet'; import WorkerStaticPool from './WorkerStaticPool'; -import Wrk from './Wrk'; +import { isMainThread } from 'worker_threads'; export default class WorkerFactory { - public static getWorkerImpl(workerScript: string): Wrk { - switch (Configuration.getWorkerProcess()) { + public static getWorkerImplementation(workerScript: string, workerProcessType: WorkerProcessType, options?: WorkerOptions): WorkerAbstract { + if (!isMainThread) { + throw new Error('Trying to get a worker implementation outside the main thread'); + } + if (Utils.isUndefined(options)) { + options = {} as WorkerOptions; + } + switch (workerProcessType) { case WorkerProcessType.WORKER_SET: - return new WorkerSet(workerScript, Configuration.getChargingStationsPerWorker()); + if (Utils.isUndefined(options.elementsPerWorker)) { + options.elementsPerWorker = 1; + } + return new WorkerSet(workerScript, options.elementsPerWorker); case WorkerProcessType.STATIC_POOL: - return new WorkerStaticPool(workerScript, Configuration.getWorkerPoolMaxSize()); + if (Utils.isUndefined(options.poolMaxSize)) { + options.poolMaxSize = 16; + } + return new WorkerStaticPool(workerScript, options.poolMaxSize); case WorkerProcessType.DYNAMIC_POOL: - return new WorkerDynamicPool(workerScript, Configuration.getWorkerPoolMinSize(), Configuration.getWorkerPoolMaxSize()); + if (Utils.isUndefined(options.poolMinSize)) { + options.poolMinSize = 4; + } + if (Utils.isUndefined(options.poolMaxSize)) { + options.poolMaxSize = 16; + } + return new WorkerDynamicPool(workerScript, options.poolMinSize, options.poolMaxSize); default: return null; }