X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerFixedPool.ts;h=836690413516bf9c241cbbd7d1d352992c04eec8;hb=0b1828224edf798044ef54672ddfc69598cd99a5;hp=33fdae494c38bac6260b7158f3daea9a884bf369;hpb=2b59e7f7c17363a618ec80a4ed3c6ecd5be20f76;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerFixedPool.ts b/src/worker/WorkerFixedPool.ts index 33fdae49..83669041 100644 --- a/src/worker/WorkerFixedPool.ts +++ b/src/worker/WorkerFixedPool.ts @@ -1,13 +1,13 @@ -import type { EventEmitter } from 'node:events'; +import type { EventEmitterAsyncResource } from 'node:events' -import { FixedThreadPool, type PoolInfo } from 'poolifier'; +import { FixedThreadPool, type PoolInfo } from 'poolifier' -import { WorkerAbstract } from './WorkerAbstract'; -import type { WorkerData, WorkerOptions } from './WorkerTypes'; -import { randomizeDelay, sleep } from './WorkerUtils'; +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`. @@ -15,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(): EventEmitter | undefined { - return this.pool?.emitter as EventEmitter; + 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!))) } }