X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerStaticPool.ts;h=e49ac2613e97e0da7dc7857b7ec2a4b3cf3f2fd1;hb=361c98f57255e5b91d123d5f2ba43ab533134b1a;hp=076b1ee0af596989d6d8eaa3f9cb8ba54207eaac;hpb=9efbac5b97d10f6fd0d8a64f1130c40bdebc7c44;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerStaticPool.ts b/src/worker/WorkerStaticPool.ts index 076b1ee0..e49ac261 100644 --- a/src/worker/WorkerStaticPool.ts +++ b/src/worker/WorkerStaticPool.ts @@ -1,80 +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 { FixedThreadPool, type PoolInfo } from 'poolifier'; -export default class WorkerStaticPool extends WorkerAbstract { - private pool: StaticPool; +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`. + * Creates a new `WorkerStaticPool`. * - * @param {string} workerScript - * @param {number} numberOfThreads - * @param {number} startWorkerDelay - * @param {PoolOptions} opts + * @param workerScript - + * @param workerOptions - */ - constructor(workerScript: string, numberOfThreads: number, startWorkerDelay?: number, opts?: PoolOptions) { - super(workerScript, startWorkerDelay); - this.pool = StaticPool.getInstance(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 size(): number { - return this.pool.workers.length; + get info(): PoolInfo { + return this.pool.info; } - get maxElementsPerWorker(): number { - return null; + get size(): number { + return this.pool.info.workerNodes; } - /** - * - * @returns {Promise} - * @public - */ - // eslint-disable-next-line @typescript-eslint/no-empty-function - public async start(): Promise { } - - /** - * - * @returns {Promise} - * @public - */ - public async stop(): Promise { - return this.pool.destroy(); + get maxElementsPerWorker(): number | undefined { + return undefined; } - /** - * - * @param elementData - * @returns {Promise} - * @public - */ - public async addElement(elementData: T): Promise { - await this.pool.execute(elementData); - // Start worker sequentially to optimize memory at startup - await Utils.sleep(this.workerStartDelay); + get emitter(): EventEmitterAsyncResource | undefined { + return this.pool?.emitter; } -} -class StaticPool extends FixedThreadPool { - private static instance: StaticPool; + /** @inheritDoc */ + public async start(): Promise { + // This is intentional + } - private constructor(numberOfThreads: number, workerScript: string, opts?: PoolOptions) { - super(numberOfThreads, workerScript, opts); + /** @inheritDoc */ + public async stop(): Promise { + return this.pool.destroy(); } - public static getInstance(numberOfThreads: number, workerScript: string, opts?: PoolOptions): StaticPool { - if (!StaticPool.instance) { - opts.exitHandler = opts.exitHandler ?? ((code) => { - if (code !== 0) { - console.error(`Worker stopped with exit code ${code}`); - } - }); - StaticPool.instance = new StaticPool(numberOfThreads, workerScript, opts); - } - return StaticPool.instance; + /** @inheritDoc */ + public async addElement(elementData: WorkerData): Promise { + await this.pool.execute(elementData); + // Start element sequentially to optimize memory at startup + this.workerOptions.elementStartDelay > 0 && (await sleep(this.workerOptions.elementStartDelay)); } }