X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerDynamicPool.ts;h=5217d7fe16a6772a81b946969702fe1189f0d5dc;hb=6e177b76d80dc30e471ec9f3500560285c48fc0f;hp=b2cd20dd10d181fe047db0621571b76d99786eee;hpb=6c1761d470507ea23d186be61b94ca7375c5144a;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerDynamicPool.ts b/src/worker/WorkerDynamicPool.ts index b2cd20dd..5217d7fe 100644 --- a/src/worker/WorkerDynamicPool.ts +++ b/src/worker/WorkerDynamicPool.ts @@ -1,67 +1,67 @@ -import { DynamicThreadPool } from 'poolifier'; +import type { EventEmitterAsyncResource } from 'node:events' -import type { WorkerData, WorkerOptions } from '../types/Worker'; -import Utils from '../utils/Utils'; -import WorkerAbstract from './WorkerAbstract'; -import { WorkerUtils } from './WorkerUtils'; +import { DynamicThreadPool, type PoolInfo } from 'poolifier' -export default class WorkerDynamicPool extends WorkerAbstract { - private readonly pool: DynamicThreadPool; +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 workerScript - * @param workerOptions + * @param workerScript - + * @param workerOptions - */ - constructor(workerScript: string, workerOptions?: WorkerOptions) { - super(workerScript, workerOptions); - this.workerOptions.poolOptions.exitHandler = - this.workerOptions?.poolOptions?.exitHandler ?? WorkerUtils.defaultExitHandler; - this.pool = new DynamicThreadPool( + 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 | null { - return null; + get size (): number { + return this.pool.info.workerNodes } - /** - * - * @returns - * @public - */ - public async start(): Promise { - // This is intentional + get maxElementsPerWorker (): number | undefined { + return undefined } - /** - * - * @returns - * @public - */ - public async stop(): Promise { - return this.pool.destroy(); + get emitter (): EventEmitterAsyncResource | undefined { + return this.pool.emitter } - /** - * - * @param elementData - * @returns - * @public - */ - public async addElement(elementData: WorkerData): Promise { - await this.pool.execute(elementData); + /** @inheritDoc */ + public start (): void { + this.pool.start() + } + + /** @inheritDoc */ + public async stop (): Promise { + await this.pool.destroy() + } + + /** @inheritDoc */ + public async addElement (elementData: D): Promise { + const response = await this.pool.execute(elementData) // Start element sequentially to optimize memory at startup - this.workerOptions.elementStartDelay > 0 && - (await Utils.sleep(this.workerOptions.elementStartDelay)); + // 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 } }