X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerFixedPool.ts;h=96060854743112a438dc408dcc8285842e1bbd19;hb=3b09e788c6dab8e5a8b3314e8e69ede16ff82fcc;hp=f15a1176beca886a418ec2e8de4779bc21e20f64;hpb=38b2428f0046c84963f4e873b76bc440155da0f2;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerFixedPool.ts b/src/worker/WorkerFixedPool.ts index f15a1176..96060854 100644 --- a/src/worker/WorkerFixedPool.ts +++ b/src/worker/WorkerFixedPool.ts @@ -1,13 +1,16 @@ -import type { EventEmitterAsyncResource } 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; +export class WorkerFixedPool extends WorkerAbstract< +D, +R +> { + private readonly pool: FixedThreadPool /** * Creates a new `WorkerFixedPool`. @@ -15,46 +18,49 @@ export class WorkerFixedPool extends WorkerAbstract { * @param workerScript - * @param workerOptions - */ - constructor(workerScript: string, workerOptions: WorkerOptions) { - super(workerScript, workerOptions); - this.pool = new FixedThreadPool( + 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(): EventEmitterAsyncResource | undefined { - return this.pool?.emitter; + get emitter (): EventEmitterAsyncResource | undefined { + return this.pool.emitter } /** @inheritDoc */ - public async start(): Promise { - // This is intentional + public start (): void { + this.pool.start() } /** @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: D): Promise { + const response = await this.pool.execute(elementData) // Start element sequentially to optimize memory at startup - this.workerOptions.elementStartDelay! > 0 && - (await sleep(randomizeDelay(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 } }