X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerStaticPool.ts;h=818fd21af48f1c466b9e8dc8920ed94bb73a915a;hb=5767874516784070db4445321b5212ad7e5a84b4;hp=d5d6390c7b2e43ad5175fa46b21c41122879e7ed;hpb=f2bf9948496c724976a2e05d2f20cb17700373ec;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerStaticPool.ts b/src/worker/WorkerStaticPool.ts index d5d6390c..818fd21a 100644 --- a/src/worker/WorkerStaticPool.ts +++ b/src/worker/WorkerStaticPool.ts @@ -1,26 +1,23 @@ -import { FixedThreadPool, PoolOptions } from 'poolifier'; +import { WorkerData, WorkerOptions } from '../types/Worker'; +import { FixedThreadPool } from 'poolifier'; import Utils from '../utils/Utils'; -import { Worker } from 'worker_threads'; import WorkerAbstract from './WorkerAbstract'; -import { WorkerData } from '../types/Worker'; import { WorkerUtils } from './WorkerUtils'; -export default class WorkerStaticPool extends WorkerAbstract { +export default class WorkerStaticPool extends WorkerAbstract { private readonly pool: FixedThreadPool; /** * Create a new `WorkerStaticPool`. * * @param workerScript - * @param numberOfThreads - * @param startWorkerDelay - * @param opts + * @param workerOptions */ - constructor(workerScript: string, numberOfThreads: number, startWorkerDelay?: number, opts?: PoolOptions) { - super(workerScript, startWorkerDelay); - opts.exitHandler = opts?.exitHandler ?? WorkerUtils.defaultExitHandler; - this.pool = new FixedThreadPool(numberOfThreads, this.workerScript, opts); + constructor(workerScript: string, workerOptions?: WorkerOptions) { + super(workerScript, workerOptions); + this.workerOptions.poolOptions.exitHandler = this.workerOptions?.poolOptions?.exitHandler ?? WorkerUtils.defaultExitHandler; + this.pool = new FixedThreadPool(this.workerOptions.poolMaxSize, this.workerScript, this.workerOptions.poolOptions); } get size(): number { @@ -55,9 +52,9 @@ export default class WorkerStaticPool extends WorkerAbstract { * @returns * @public */ - public async addElement(elementData: T): Promise { + public async addElement(elementData: WorkerData): Promise { await this.pool.execute(elementData); - // Start worker sequentially to optimize memory at startup - await Utils.sleep(this.workerStartDelay); + // Start element sequentially to optimize memory at startup + this.workerOptions.elementStartDelay > 0 && await Utils.sleep(this.workerOptions.elementStartDelay); } }