X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerFactory.ts;h=35fe82eb4c73a017d01316e265ddc7348fc72025;hb=c36e3cf0312f553b4b8c2b716da0d55cc87450cf;hp=dba59173510a3e63e33c7dafa191d444eb406385;hpb=adeb9b56509fe4d63239eff1cd8a9d84250817a6;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerFactory.ts b/src/worker/WorkerFactory.ts index dba59173..35fe82eb 100644 --- a/src/worker/WorkerFactory.ts +++ b/src/worker/WorkerFactory.ts @@ -1,41 +1,56 @@ -import { WorkerOptions, WorkerProcessType } from '../types/Worker'; +import { isMainThread } from 'node:worker_threads'; -import Utils from '../utils/Utils'; -import WorkerDynamicPool from './WorkerDynamicPool'; -import WorkerSet from './WorkerSet'; -import WorkerStaticPool from './WorkerStaticPool'; -import Wrk from './Wrk'; -import { isMainThread } from 'worker_threads'; +import type { ThreadPoolOptions } from 'poolifier'; -export default class WorkerFactory { - public static getWorkerImplementation(workerScript: string, workerProcessType: WorkerProcessType, options?: WorkerOptions): Wrk { +import type { WorkerAbstract } from './WorkerAbstract'; +import { WorkerConstants } 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('Trying to get a worker implementation outside the main thread'); - } - if (Utils.isUndefined(options)) { - options = {} as WorkerOptions; + throw new Error('Cannot 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 ThreadPoolOptions); + let workerImplementation: WorkerAbstract | null = null; 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); + case WorkerProcessType.workerSet: + workerOptions.elementsPerWorker = + workerOptions?.elementsPerWorker ?? WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER; + workerImplementation = new WorkerSet(workerScript, workerOptions); + break; + case WorkerProcessType.staticPool: + workerOptions.poolMaxSize = + workerOptions?.poolMaxSize ?? WorkerConstants.DEFAULT_POOL_MAX_SIZE; + workerImplementation = new WorkerStaticPool(workerScript, workerOptions); + break; + case WorkerProcessType.dynamicPool: + 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: - return null; + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions + throw new Error(`Worker implementation type '${workerProcessType}' not found`); } + return workerImplementation; } }