X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerStaticPool.ts;h=392981237289a78044af6d3a0cd8830eb6888210;hb=b2b606263e2676354259164d532ff9aa91ccdf87;hp=a7bb193270e88346347f42fa1d2c98efcc31f806;hpb=8df3f0a9d9fb9796c093a0e040b13dca8aaa3234;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerStaticPool.ts b/src/worker/WorkerStaticPool.ts index a7bb1932..39298123 100644 --- a/src/worker/WorkerStaticPool.ts +++ b/src/worker/WorkerStaticPool.ts @@ -1,62 +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: FixedThreadPool; +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); - 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 {Promise} - * @public - */ - // eslint-disable-next-line @typescript-eslint/no-empty-function - public async start(): Promise {} + get emitter(): PoolEmitter | undefined { + return this.pool?.emitter; + } - /** - * - * @returns {Promise} - * @public - */ + /** @inheritDoc */ + public async start(): Promise { + // This is intentional + } + + /** @inheritDoc */ public async stop(): Promise { return this.pool.destroy(); } - /** - * - * @param {T} elementData - * @returns {Promise} - * @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)); } }