X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerFactory.ts;h=70bcc3862c4fca8c42a66d7e0f52e7568bfbef80;hb=6fee1791db82bd4605b180612fe642a7e2c4caa0;hp=dba59173510a3e63e33c7dafa191d444eb406385;hpb=1af50fac1ab71fed19f11864d1644261046698a3;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerFactory.ts b/src/worker/WorkerFactory.ts index dba59173..70bcc386 100644 --- a/src/worker/WorkerFactory.ts +++ b/src/worker/WorkerFactory.ts @@ -1,41 +1,60 @@ -import { WorkerOptions, WorkerProcessType } from '../types/Worker'; +import { Worker, isMainThread } from 'worker_threads'; +import { WorkerData, WorkerOptions, WorkerProcessType } from '../types/Worker'; -import Utils from '../utils/Utils'; +import { PoolOptions } from 'poolifier'; +import type WorkerAbstract from './WorkerAbstract'; +import WorkerConstants from './WorkerConstants'; import WorkerDynamicPool from './WorkerDynamicPool'; import WorkerSet from './WorkerSet'; import WorkerStaticPool from './WorkerStaticPool'; -import Wrk from './Wrk'; -import { isMainThread } from 'worker_threads'; export default class WorkerFactory { - public static getWorkerImplementation(workerScript: string, workerProcessType: WorkerProcessType, options?: WorkerOptions): Wrk { + 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; - } + 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 PoolOptions); + workerOptions?.messageHandler && + // eslint-disable-next-line @typescript-eslint/no-misused-promises + (workerOptions.poolOptions.messageHandler = workerOptions.messageHandler); + console.log('before'); + let workerImplementation: WorkerAbstract = null; + console.log(workerImplementation); switch (workerProcessType) { case WorkerProcessType.WORKER_SET: - if (Utils.isUndefined(options.elementsPerWorker)) { - options.elementsPerWorker = 1; - } - return new WorkerSet(workerScript, options.elementsPerWorker); + workerOptions.elementsPerWorker = + workerOptions?.elementsPerWorker ?? WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER; + workerImplementation = new WorkerSet(workerScript, workerOptions); + break; case WorkerProcessType.STATIC_POOL: - if (Utils.isUndefined(options.poolMaxSize)) { - options.poolMaxSize = 16; - } - return new WorkerStaticPool(workerScript, options.poolMaxSize); + workerOptions.poolMaxSize = + workerOptions?.poolMaxSize ?? WorkerConstants.DEFAULT_POOL_MAX_SIZE; + workerImplementation = new WorkerStaticPool(workerScript, workerOptions); + break; 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); + 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; } }