X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerStaticPool.ts;h=1cbd18e8b9b1cf89094350c71ac2b1488d2d0aac;hb=68250247081144b3ccc237926c350037a3db28ed;hp=97664dbae58ddb378aa03239610f6b68dcac056e;hpb=3340259a3cd75024ae3510433f4bf4232c8de7fb;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerStaticPool.ts b/src/worker/WorkerStaticPool.ts index 97664dba..1cbd18e8 100644 --- a/src/worker/WorkerStaticPool.ts +++ b/src/worker/WorkerStaticPool.ts @@ -1,44 +1,48 @@ import { FixedThreadPool, PoolOptions } from 'poolifier'; -import Constants from '../utils/Constants'; 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 WorkerStaticPool extends WorkerAbstract { - private pool: StaticPool; +export default class WorkerStaticPool extends WorkerAbstract { + private readonly pool: FixedThreadPool; /** * Create a new `WorkerStaticPool`. * - * @param {string} workerScript + * @param workerScript * @param numberOfThreads + * @param startWorkerDelay + * @param opts */ - constructor(workerScript: string, numberOfThreads: number) { - super(workerScript); - this.pool = StaticPool.getInstance(numberOfThreads, this.workerScript); + 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); } 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 */ public async stop(): Promise { @@ -48,35 +52,12 @@ export default class WorkerStaticPool 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 StaticPool extends FixedThreadPool { - private static instance: StaticPool; - - private constructor(numberOfThreads: number, workerScript: string, opts?: PoolOptions) { - super(numberOfThreads, workerScript, opts); - } - - public static getInstance(numberOfThreads: number, workerScript: string): StaticPool { - if (!StaticPool.instance) { - StaticPool.instance = new StaticPool(numberOfThreads, workerScript, - { - exitHandler: (code) => { - if (code !== 0) { - console.error(`Worker stopped with exit code ${code}`); - } - } - } - ); - } - return StaticPool.instance; + await Utils.sleep(this.workerStartDelay); } }