X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerStaticPool.ts;h=392981237289a78044af6d3a0cd8830eb6888210;hb=c36e3cf0312f553b4b8c2b716da0d55cc87450cf;hp=cf46cfa55f7662f671a5a24635c1f9566feb55f4;hpb=7874b0b1fbf94d34e8c65ed3e669f1c97f74dd1d;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerStaticPool.ts b/src/worker/WorkerStaticPool.ts index cf46cfa5..39298123 100644 --- a/src/worker/WorkerStaticPool.ts +++ b/src/worker/WorkerStaticPool.ts @@ -1,77 +1,57 @@ -import { FixedThreadPool, PoolOptions } from 'poolifier'; +import { FixedThreadPool, type PoolEmitter, type PoolInfo } from 'poolifier'; -import Utils from '../utils/Utils'; -import { Worker } from 'worker_threads'; -import WorkerAbstract from './WorkerAbstract'; -import { WorkerData } from '../types/Worker'; -import { WorkerUtils } from './WorkerUtils'; +import { WorkerAbstract } from './WorkerAbstract'; +import type { WorkerData, WorkerOptions } from './WorkerTypes'; +import { sleep } from './WorkerUtils'; -export default class WorkerStaticPool extends WorkerAbstract { - private pool: StaticPool; +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 | null { - 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 {T} 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(): PoolEmitter | 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 ?? WorkerUtils.defaultExitHandler; - 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)); } }