X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerDynamicPool.ts;h=5217d7fe16a6772a81b946969702fe1189f0d5dc;hb=6e177b76d80dc30e471ec9f3500560285c48fc0f;hp=a8f689782b37a414545799c3beacb2e8de6932ac;hpb=9efbac5b97d10f6fd0d8a64f1130c40bdebc7c44;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerDynamicPool.ts b/src/worker/WorkerDynamicPool.ts index a8f68978..5217d7fe 100644 --- a/src/worker/WorkerDynamicPool.ts +++ b/src/worker/WorkerDynamicPool.ts @@ -1,82 +1,67 @@ -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 { DynamicThreadPool, type PoolInfo } from 'poolifier' -export default class WorkerDynamicPool extends WorkerAbstract { - private pool: DynamicPool; +import { WorkerAbstract } from './WorkerAbstract.js' +import type { WorkerData, WorkerOptions } from './WorkerTypes.js' +import { randomizeDelay, sleep } from './WorkerUtils.js' + +export class WorkerDynamicPool extends WorkerAbstract< +D, +R +> { + private readonly pool: DynamicThreadPool /** - * Create a new `WorkerDynamicPool`. + * Creates a new `WorkerDynamicPool`. * - * @param {string} workerScript - * @param {number} min - * @param {number} max - * @param {number} workerStartDelay - * @param {PoolOptions} opts + * @param workerScript - + * @param workerOptions - */ - constructor(workerScript: string, min: number, max: number, workerStartDelay?: number, opts?: PoolOptions) { - super(workerScript, workerStartDelay); - this.pool = DynamicPool.getInstance(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 size(): number { - return this.pool.workers.length; + get info (): PoolInfo { + return this.pool.info } - get maxElementsPerWorker(): number { - return null; + get size (): number { + return this.pool.info.workerNodes } - /** - * - * @returns {Promise} - * @public - */ - // eslint-disable-next-line @typescript-eslint/no-empty-function - public async start(): Promise { } - - /** - * - * @returns {Promise} - * @public - */ - // eslint-disable-next-line @typescript-eslint/require-await - public async stop(): Promise { - return this.pool.destroy(); + get maxElementsPerWorker (): number | undefined { + return undefined } - /** - * - * @param elementData - * @returns {Promise} - * @public - */ - public async addElement(elementData: T): Promise { - await this.pool.execute(elementData); - // Start worker sequentially to optimize memory at startup - await Utils.sleep(this.workerStartDelay); + get emitter (): EventEmitterAsyncResource | undefined { + return this.pool.emitter } -} -class DynamicPool extends DynamicThreadPool { - private static instance: DynamicPool; + /** @inheritDoc */ + public start (): void { + this.pool.start() + } - private constructor(min: number, max: number, workerScript: string, opts?: PoolOptions) { - super(min, max, workerScript, opts); + /** @inheritDoc */ + public async stop (): Promise { + await this.pool.destroy() } - public static getInstance(min: number, max: number, workerScript: string, opts?: PoolOptions): DynamicPool { - if (!DynamicPool.instance) { - opts.exitHandler = opts.exitHandler ?? ((code) => { - if (code !== 0) { - console.error(`Worker stopped with exit code ${code}`); - } - }); - DynamicPool.instance = new DynamicPool(min, max, workerScript, opts); - } - return DynamicPool.instance; + /** @inheritDoc */ + public async addElement (elementData: D): Promise { + const response = await this.pool.execute(elementData) + // Start element sequentially to optimize memory at startup + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.workerOptions.elementAddDelay! > 0 && + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + (await sleep(randomizeDelay(this.workerOptions.elementAddDelay!))) + return response } }