X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerDynamicPool.ts;h=b2cd20dd10d181fe047db0621571b76d99786eee;hb=5af9aa8a875b3dbd4b4d394ecac022d046fa725c;hp=eb570887f43d8df927b8310d9fb3166740f1f1c8;hpb=3340259a3cd75024ae3510433f4bf4232c8de7fb;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerDynamicPool.ts b/src/worker/WorkerDynamicPool.ts index eb570887..b2cd20dd 100644 --- a/src/worker/WorkerDynamicPool.ts +++ b/src/worker/WorkerDynamicPool.ts @@ -1,48 +1,53 @@ -import { DynamicThreadPool, PoolOptions } from 'poolifier'; +import { DynamicThreadPool } from 'poolifier'; -import Constants from '../utils/Constants'; +import type { WorkerData, WorkerOptions } from '../types/Worker'; import Utils from '../utils/Utils'; -import { Worker } from 'worker_threads'; import WorkerAbstract from './WorkerAbstract'; -import { WorkerData } from '../types/Worker'; +import { WorkerUtils } from './WorkerUtils'; -export default class WorkerDynamicPool extends WorkerAbstract { - private pool: DynamicPool; +export default class WorkerDynamicPool extends WorkerAbstract { + private readonly pool: DynamicThreadPool; /** * Create a new `WorkerDynamicPool`. * - * @param {string} workerScript - * @param min - * @param max + * @param workerScript + * @param workerOptions */ - constructor(workerScript: string, min: number, max: number,) { - super(workerScript); - this.pool = DynamicPool.getInstance(min, max, this.workerScript); + constructor(workerScript: string, workerOptions?: WorkerOptions) { + super(workerScript, workerOptions); + this.workerOptions.poolOptions.exitHandler = + this.workerOptions?.poolOptions?.exitHandler ?? WorkerUtils.defaultExitHandler; + this.pool = new DynamicThreadPool( + this.workerOptions.poolMinSize, + this.workerOptions.poolMaxSize, + this.workerScript, + this.workerOptions.poolOptions + ); } get size(): number { return this.pool.workers.length; } - get maxElementsPerWorker(): number { + get maxElementsPerWorker(): number | null { return null; } /** * - * @returns {Promise} + * @returns * @public */ - // eslint-disable-next-line @typescript-eslint/no-empty-function - public async start(): Promise { } + public async start(): Promise { + // This is intentional + } /** * - * @returns {Promise} + * @returns * @public */ - // eslint-disable-next-line @typescript-eslint/require-await public async stop(): Promise { return this.pool.destroy(); } @@ -50,35 +55,13 @@ export default class WorkerDynamicPool extends WorkerAbstract { /** * * @param elementData - * @returns {Promise} + * @returns * @public */ - public async addElement(elementData: T): Promise { + 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); - } -} - -class DynamicPool extends DynamicThreadPool { - private static instance: DynamicPool; - - private constructor(min: number, max: number, workerScript: string, opts?: PoolOptions) { - super(min, max, workerScript, opts); - } - - public static getInstance(min: number, max: number, workerScript: string): DynamicPool { - if (!DynamicPool.instance) { - DynamicPool.instance = new DynamicPool(min, max, workerScript, - { - exitHandler: (code) => { - if (code !== 0) { - console.error(`Worker stopped with exit code ${code}`); - } - } - } - ); - } - return DynamicPool.instance; + // Start element sequentially to optimize memory at startup + this.workerOptions.elementStartDelay > 0 && + (await Utils.sleep(this.workerOptions.elementStartDelay)); } }