X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerDynamicPool.ts;h=5824d8adfa21fc44e3c59d22d9c3fd94461e496e;hb=d929adcc32a8cc79f0c7182d16f70367b001d28c;hp=f081b3c3018ae0f144001b3138e355fbff52e159;hpb=0045cef5dae3be8131969d08f70c10748922524e;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerDynamicPool.ts b/src/worker/WorkerDynamicPool.ts index f081b3c3..5824d8ad 100644 --- a/src/worker/WorkerDynamicPool.ts +++ b/src/worker/WorkerDynamicPool.ts @@ -1,64 +1,59 @@ -import { DynamicThreadPool, PoolOptions } from 'poolifier'; +import { DynamicThreadPool, type PoolEmitter, type PoolInfo } 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'; +import { WorkerAbstract } from './WorkerAbstract'; +import type { WorkerData, WorkerOptions } from './WorkerTypes'; +import { sleep } from './WorkerUtils'; -export default class WorkerDynamicPool extends WorkerAbstract { - private pool: DynamicThreadPool; +export class WorkerDynamicPool extends WorkerAbstract { + private readonly pool: DynamicThreadPool; /** - * Create a new `WorkerDynamicPool`. + * Creates 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 - */ + get emitter(): PoolEmitter | 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!)); } }