X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerFixedPool.ts;h=836690413516bf9c241cbbd7d1d352992c04eec8;hb=a01134ed215b62a0c82475b9fbb35306fa792bbc;hp=f2a47cef5b82804f6b50541386e38490d0f2974e;hpb=1d8f226b47637e80c74106799208da5df93b2a04;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerFixedPool.ts b/src/worker/WorkerFixedPool.ts index f2a47cef..83669041 100644 --- a/src/worker/WorkerFixedPool.ts +++ b/src/worker/WorkerFixedPool.ts @@ -1,11 +1,13 @@ -import { FixedThreadPool, type PoolEmitter, type PoolInfo } from 'poolifier'; +import type { EventEmitterAsyncResource } from 'node:events' -import { WorkerAbstract } from './WorkerAbstract'; -import type { WorkerData, WorkerOptions } from './WorkerTypes'; -import { randomizeDelay, sleep } from './WorkerUtils'; +import { FixedThreadPool, type PoolInfo } from 'poolifier' + +import { WorkerAbstract } from './WorkerAbstract.js' +import type { WorkerData, WorkerOptions } from './WorkerTypes.js' +import { randomizeDelay, sleep } from './WorkerUtils.js' export class WorkerFixedPool extends WorkerAbstract { - private readonly pool: FixedThreadPool; + private readonly pool: FixedThreadPool /** * Creates a new `WorkerFixedPool`. @@ -13,46 +15,48 @@ export class WorkerFixedPool extends WorkerAbstract { * @param workerScript - * @param workerOptions - */ - constructor(workerScript: string, workerOptions: WorkerOptions) { - super(workerScript, workerOptions); + constructor (workerScript: string, workerOptions: WorkerOptions) { + super(workerScript, workerOptions) this.pool = new FixedThreadPool( this.workerOptions.poolMaxSize, this.workerScript, - this.workerOptions.poolOptions, - ); + this.workerOptions.poolOptions + ) } - get info(): PoolInfo { - return this.pool.info; + get info (): PoolInfo { + return this.pool.info } - get size(): number { - return this.pool.info.workerNodes; + get size (): number { + return this.pool.info.workerNodes } - get maxElementsPerWorker(): number | undefined { - return undefined; + get maxElementsPerWorker (): number | undefined { + return undefined } - get emitter(): PoolEmitter | undefined { - return this.pool?.emitter; + get emitter (): EventEmitterAsyncResource | undefined { + return this.pool.emitter } /** @inheritDoc */ - public async start(): Promise { + public async start (): Promise { // This is intentional } /** @inheritDoc */ - public async stop(): Promise { - return this.pool.destroy(); + public async stop (): Promise { + await this.pool.destroy() } /** @inheritDoc */ - public async addElement(elementData: WorkerData): Promise { - await this.pool.execute(elementData); + 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 && - (await sleep(randomizeDelay(this.workerOptions.elementStartDelay!))); + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + (await sleep(randomizeDelay(this.workerOptions.elementStartDelay!))) } }