X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerDynamicPool.ts;h=9527d67b60bd9b68ff81d4c96b8b415af6d30b61;hb=42b8cf5cdca8eaab1e7442f7c92c2a5ed97434f6;hp=af8b7e2add2e882195baa8983fc7a2e4465d15c4;hpb=8baf3f8f3e3330c90cbd3474b6f5c7d589be17ff;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerDynamicPool.ts b/src/worker/WorkerDynamicPool.ts index af8b7e2a..9527d67b 100644 --- a/src/worker/WorkerDynamicPool.ts +++ b/src/worker/WorkerDynamicPool.ts @@ -1,45 +1,46 @@ -import type { Worker } from 'node:worker_threads'; +import type { EventEmitterAsyncResource } from 'node:events'; -import { DynamicThreadPool, type ErrorHandler, type ExitHandler } from 'poolifier'; +import { DynamicThreadPool, type PoolInfo } from 'poolifier'; import { WorkerAbstract } from './WorkerAbstract'; import type { WorkerData, WorkerOptions } from './WorkerTypes'; -import { defaultErrorHandler, defaultExitHandler, sleep } from './WorkerUtils'; +import { randomizeDelay, sleep } from './WorkerUtils'; export class WorkerDynamicPool extends WorkerAbstract { private readonly pool: DynamicThreadPool; /** - * Create a new `WorkerDynamicPool`. + * Creates a new `WorkerDynamicPool`. * * @param workerScript - * @param workerOptions - */ - constructor(workerScript: string, workerOptions?: WorkerOptions) { + constructor(workerScript: string, workerOptions: WorkerOptions) { super(workerScript, workerOptions); - this.workerOptions.poolOptions.errorHandler = ( - this.workerOptions?.poolOptions?.errorHandler ?? defaultErrorHandler - ).bind(this) as ErrorHandler; - this.workerOptions.poolOptions.exitHandler = ( - this.workerOptions?.poolOptions?.exitHandler ?? defaultExitHandler - ).bind(this) as ExitHandler; - this.workerOptions.poolOptions.messageHandler.bind(this); this.pool = new DynamicThreadPool( this.workerOptions.poolMinSize, this.workerOptions.poolMaxSize, this.workerScript, - this.workerOptions.poolOptions + this.workerOptions.poolOptions, ); } + get info(): PoolInfo { + return this.pool.info; + } + get size(): number { - return this.pool.workerNodes.length; + return this.pool.info.workerNodes; } get maxElementsPerWorker(): number | undefined { return undefined; } + get emitter(): EventEmitterAsyncResource | undefined { + return this.pool?.emitter; + } + /** @inheritDoc */ public async start(): Promise { // This is intentional @@ -54,6 +55,7 @@ export class WorkerDynamicPool extends WorkerAbstract { public async addElement(elementData: WorkerData): Promise { await this.pool.execute(elementData); // Start element sequentially to optimize memory at startup - this.workerOptions.elementStartDelay > 0 && (await sleep(this.workerOptions.elementStartDelay)); + this.workerOptions.elementStartDelay! > 0 && + (await sleep(randomizeDelay(this.workerOptions.elementStartDelay!))); } }