X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerDynamicPool.ts;h=4be6bd572bf5d258f40501ef4fa163eca3a4c8b9;hb=19bdf4ca58ec313de2404dc598553a328b425722;hp=e5119ff6a0c32020dbabdd4a38ca5ae93a700e0d;hpb=6c65a2958d57b4c91a60150c2bf567659b64d4f0;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerDynamicPool.ts b/src/worker/WorkerDynamicPool.ts index e5119ff6..4be6bd57 100644 --- a/src/worker/WorkerDynamicPool.ts +++ b/src/worker/WorkerDynamicPool.ts @@ -1,65 +1,60 @@ -import { DynamicThreadPool, PoolOptions } from 'poolifier'; +import type EventEmitterAsyncResource from 'node:events'; -import Utils from '../utils/Utils'; -import { Worker } from 'worker_threads'; -import WorkerAbstract from './WorkerAbstract'; -import { WorkerData } from '../types/Worker'; -import { WorkerUtils } from './WorkerUtils'; +import { DynamicThreadPool, type PoolInfo } from 'poolifier'; -export default class WorkerDynamicPool extends WorkerAbstract { - private pool: DynamicThreadPool; +import { WorkerAbstract } from './WorkerAbstract'; +import type { WorkerData, WorkerOptions } from './WorkerTypes'; +import { sleep } from './WorkerUtils'; + +export class WorkerDynamicPool extends WorkerAbstract { + private readonly pool: DynamicThreadPool; /** * Create a new `WorkerDynamicPool`. * - * @param workerScript - * @param min - * @param max - * @param workerStartDelay - * @param opts + * @param workerScript - + * @param workerOptions - */ - constructor(workerScript: string, min: number, max: number, workerStartDelay?: number, opts?: PoolOptions) { - super(workerScript, workerStartDelay); - opts.exitHandler = opts?.exitHandler ?? WorkerUtils.defaultExitHandler; - this.pool = new DynamicThreadPool(min, max, this.workerScript, opts); + constructor(workerScript: string, workerOptions?: WorkerOptions) { + super(workerScript, workerOptions); + this.pool = new DynamicThreadPool( + this.workerOptions.poolMinSize, + this.workerOptions.poolMaxSize, + this.workerScript, + this.workerOptions.poolOptions + ); + } + + get info(): PoolInfo { + return this.pool.info; } get size(): number { - return this.pool.workers.length; + return this.pool.info.workerNodes; } - get maxElementsPerWorker(): number | null { - return null; + get maxElementsPerWorker(): number | undefined { + return undefined; } - /** - * - * @returns - * @public - */ - // eslint-disable-next-line @typescript-eslint/no-empty-function + get emitter(): EventEmitterAsyncResource | undefined { + return this.pool?.emitter; + } + + /** @inheritDoc */ public async start(): Promise { // This is intentional } - /** - * - * @returns - * @public - */ + /** @inheritDoc */ public async stop(): Promise { return this.pool.destroy(); } - /** - * - * @param elementData - * @returns - * @public - */ - public async addElement(elementData: T): Promise { + /** @inheritDoc */ + 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 sleep(this.workerOptions.elementStartDelay)); } }