X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerStaticPool.ts;h=473ff3bba41e0a332394f23a313a8040cf554179;hb=be245fdab36274873e0a9651589cebd097548076;hp=d5e9222618fb1ae37cdb1cfcf563ca4936a99594;hpb=81797102d5214fea2fc58eff2666fe8b8d9a5a11;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerStaticPool.ts b/src/worker/WorkerStaticPool.ts index d5e92226..473ff3bb 100644 --- a/src/worker/WorkerStaticPool.ts +++ b/src/worker/WorkerStaticPool.ts @@ -1,66 +1,59 @@ -import { FixedThreadPool, 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 { FixedThreadPool, type PoolInfo } from 'poolifier'; -export default class WorkerStaticPool extends WorkerAbstract { - private pool: FixedThreadPool; +import { WorkerAbstract } from './WorkerAbstract'; +import type { WorkerData, WorkerOptions } from './WorkerTypes'; +import { sleep } from './WorkerUtils'; + +export class WorkerStaticPool extends WorkerAbstract { + private readonly pool: FixedThreadPool; /** * Create a new `WorkerStaticPool`. * - * @param workerScript - * @param numberOfThreads - * @param startWorkerDelay - * @param opts - * @param messageListenerCallback + * @param workerScript - + * @param workerOptions - */ - constructor(workerScript: string, numberOfThreads: number, startWorkerDelay?: number, opts?: PoolOptions, - messageListenerCallback: (message: any) => void = () => { /* This is intentional */ }) { - super(workerScript, startWorkerDelay, messageListenerCallback); - opts.exitHandler = opts?.exitHandler ?? WorkerUtils.defaultExitHandler; - this.pool = new FixedThreadPool(numberOfThreads, this.workerScript, opts); + constructor(workerScript: string, workerOptions?: WorkerOptions) { + super(workerScript, workerOptions); + this.pool = new FixedThreadPool( + this.workerOptions.poolMaxSize, + this.workerScript, + this.workerOptions.poolOptions + ); + } + + get info(): PoolInfo { + return this.pool.info; } get size(): number { - return this.pool.workers.length; + return this.pool.info.workerNodes; } - get maxElementsPerWorker(): number | null { - return null; + get maxElementsPerWorker(): number | undefined { + return undefined; } - /** - * - * @returns - * @public - */ - // eslint-disable-next-line @typescript-eslint/no-empty-function + get emitter(): EventEmitterAsyncResource | undefined { + return this.pool?.emitter; + } + + /** @inheritDoc */ public async start(): Promise { // This is intentional } - /** - * - * @returns - * @public - */ + /** @inheritDoc */ public async stop(): Promise { return this.pool.destroy(); } - /** - * - * @param elementData - * @returns - * @public - */ - public async addElement(elementData: T): Promise { + /** @inheritDoc */ + public async addElement(elementData: WorkerData): Promise { await this.pool.execute(elementData); - // Start worker sequentially to optimize memory at startup - await Utils.sleep(this.workerStartDelay); + // Start element sequentially to optimize memory at startup + this.workerOptions.elementStartDelay > 0 && (await sleep(this.workerOptions.elementStartDelay)); } }