X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerFactory.ts;h=41fa1d89715444ca5cd69b07f6684789f63b2ecc;hb=ce4d00ee7295ccdb12f35154eb2eb72973b6e75b;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..41fa1d89 100644 --- a/src/worker/WorkerFactory.ts +++ b/src/worker/WorkerFactory.ts @@ -1,13 +1,41 @@ -import Configuration from '../utils/Configuration'; -import WorkerPool from './WorkerPool'; +import { WorkerOptions, WorkerProcessType } from '../types/Worker'; + +import Utils from '../utils/Utils'; +import WorkerAbstract from './WorkerAbstract'; +import WorkerDynamicPool from './WorkerDynamicPool'; import WorkerSet from './WorkerSet'; -import Wrk from './Wrk'; +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 { + 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: + if (Utils.isUndefined(options.elementsPerWorker)) { + options.elementsPerWorker = 1; + } + return new WorkerSet(workerScript, options.elementsPerWorker); + case WorkerProcessType.STATIC_POOL: + if (Utils.isUndefined(options.poolMaxSize)) { + options.poolMaxSize = 16; + } + return new WorkerStaticPool(workerScript, options.poolMaxSize); + case WorkerProcessType.DYNAMIC_POOL: + 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; } - return new WorkerSet(workerScript, Configuration.getChargingStationsPerWorker()); } }