X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fworker%2FWorkerFactory.ts;h=59fbf3a44fe46409a0d84c5a11ed0abcf7fce7f1;hb=acfa5fd10b8489f5d85654e654356e65bfd1e0d0;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..59fbf3a4 100644 --- a/src/worker/WorkerFactory.ts +++ b/src/worker/WorkerFactory.ts @@ -1,21 +1,41 @@ -import Configuration from '../utils/Configuration'; -import WorkerDynamicPool from './WorkerDynamicPool'; -import { WorkerProcessType } from '../types/Worker'; -import WorkerSet from './WorkerSet'; -import WorkerStaticPool from './WorkerStaticPool'; -import Wrk from './Wrk'; +import { isMainThread } from 'node:worker_threads'; -export default class WorkerFactory { - public static getWorkerImpl(workerScript: string): Wrk { - switch (Configuration.getWorkerProcess()) { - case WorkerProcessType.WORKER_SET: - return new WorkerSet(workerScript, Configuration.getChargingStationsPerWorker()); - case WorkerProcessType.STATIC_POOL: - return new WorkerStaticPool(workerScript, Configuration.getWorkerPoolMaxSize()); - case WorkerProcessType.DYNAMIC_POOL: - return new WorkerDynamicPool(workerScript, Configuration.getWorkerPoolMinSize(), Configuration.getWorkerPoolMaxSize()); +import type { WorkerAbstract } from './WorkerAbstract'; +import { DEFAULT_WORKER_OPTIONS } from './WorkerConstants'; +import { WorkerDynamicPool } from './WorkerDynamicPool'; +import { WorkerSet } from './WorkerSet'; +import { WorkerStaticPool } from './WorkerStaticPool'; +import { type WorkerData, type WorkerOptions, WorkerProcessType } from './WorkerTypes'; + +export class WorkerFactory { + private constructor() { + // This is intentional + } + + public static getWorkerImplementation( + workerScript: string, + workerProcessType: WorkerProcessType, + workerOptions?: WorkerOptions, + ): WorkerAbstract | null { + if (!isMainThread) { + throw new Error('Cannot get a worker implementation outside the main thread'); + } + workerOptions = { ...DEFAULT_WORKER_OPTIONS, ...workerOptions }; + let workerImplementation: WorkerAbstract | null = null; + switch (workerProcessType) { + case WorkerProcessType.workerSet: + workerImplementation = new WorkerSet(workerScript, workerOptions); + break; + case WorkerProcessType.staticPool: + workerImplementation = new WorkerStaticPool(workerScript, workerOptions); + break; + case WorkerProcessType.dynamicPool: + workerImplementation = new WorkerDynamicPool(workerScript, workerOptions); + break; default: - return null; + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions + throw new Error(`Worker implementation type '${workerProcessType}' not found`); } + return workerImplementation; } }