X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fworker%2FWorkerDynamicPool.ts;h=817536f0237251b88af5c242fb0800914e4574d5;hb=093ca832a5c03ea03f32368b4bf13c1d08ef3a54;hp=37b6ddd15c32a71f43a2de51a91af308ce31ac3e;hpb=c3ee95af4edd541595e3873c8aa4c93d38e59474;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerDynamicPool.ts b/src/worker/WorkerDynamicPool.ts index 37b6ddd1..817536f0 100644 --- a/src/worker/WorkerDynamicPool.ts +++ b/src/worker/WorkerDynamicPool.ts @@ -1,64 +1,63 @@ -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 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 { + 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 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 { + get maxElementsPerWorker (): number | undefined { + return undefined + } + + get emitter (): EventEmitterAsyncResource | undefined { + return this.pool.emitter + } + + /** @inheritDoc */ + public async start (): Promise { // This is intentional } - /** - * - * @returns - * @public - */ - public async stop(): Promise { - return this.pool.destroy(); + /** @inheritDoc */ + public async stop (): Promise { + await this.pool.destroy() } - /** - * - * @param elementData - * @returns - * @public - */ - public async addElement(elementData: WorkerData): Promise { - await this.pool.execute(elementData); - // Start worker sequentially to optimize memory at startup - await Utils.sleep(this.workerStartDelay); + /** @inheritDoc */ + public async addElement (elementData: WorkerData): Promise { + 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.elementStartDelay! > 0 && + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + (await sleep(randomizeDelay(this.workerOptions.elementStartDelay!))) } }