X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerStaticPool.ts;h=473ff3bba41e0a332394f23a313a8040cf554179;hb=be245fdab36274873e0a9651589cebd097548076;hp=9c2bc41d4adac0d905a4603e3adfd9c57187a646;hpb=1e924543e08745cfa9642dca53d303390d391f00;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerStaticPool.ts b/src/worker/WorkerStaticPool.ts index 9c2bc41d..473ff3bb 100644 --- a/src/worker/WorkerStaticPool.ts +++ b/src/worker/WorkerStaticPool.ts @@ -1,70 +1,59 @@ -import { FixedThreadPool, FixedThreadPoolOptions } from 'poolifier'; +import type EventEmitterAsyncResource from 'node:events'; -import Constants from '../utils/Constants'; -import Utils from '../utils/Utils'; -import { WorkerData } from '../types/Worker'; -import Wrk from './Wrk'; +import { FixedThreadPool, type PoolInfo } from 'poolifier'; -export default class WorkerStaticPool extends Wrk { - 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`. * - * @param {string} workerScript + * @param workerScript - + * @param workerOptions - */ - constructor(workerScript: string, numThreads: number) { - super(workerScript); - this.pool = StaticPool.getInstance(numThreads, this.workerScript); + 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 1; + get size(): number { + return this.pool.info.workerNodes; } - /** - * - * @return {Promise} - * @public - */ - // eslint-disable-next-line @typescript-eslint/no-empty-function - public async start(): Promise { } + get maxElementsPerWorker(): number | undefined { + return undefined; + } - /** - * - * @return {Promise} - * @public - */ - public async addElement(elementData: WorkerData): Promise { - await this.pool.execute(elementData); - // Start worker sequentially to optimize memory at startup - await Utils.sleep(Constants.START_WORKER_DELAY); + 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(numThreads: number, workerScript: string, opts?: FixedThreadPoolOptions) { - super(numThreads, workerScript, opts); + /** @inheritDoc */ + public async stop(): Promise { + return this.pool.destroy(); } - public static getInstance(numThreads: number, workerScript: string): StaticPool { - if (!StaticPool.instance) { - StaticPool.instance = new StaticPool(numThreads, workerScript, - { - exitHandler: (code) => { - if (code !== 0) { - console.error(`Worker stopped with exit code ${code}`); - } - } - } - ); - } - 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)); } }