X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerStaticPool.ts;h=cf46cfa55f7662f671a5a24635c1f9566feb55f4;hb=e71712800639b0aaebec9af189fdbbaa4f453b92;hp=20d53b0e02d305e9f4623175cdca0301a737a81b;hpb=a4624c96a6c159b4885f5d0baaf592ceec0bab30;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerStaticPool.ts b/src/worker/WorkerStaticPool.ts index 20d53b0e..cf46cfa5 100644 --- a/src/worker/WorkerStaticPool.ts +++ b/src/worker/WorkerStaticPool.ts @@ -1,35 +1,38 @@ -import { FixedThreadPool, FixedThreadPoolOptions } from 'poolifier'; +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 Wrk from './Wrk'; -import { threadId } from 'worker_threads'; +import { WorkerUtils } from './WorkerUtils'; -export default class WorkerStaticPool extends Wrk { +export default class WorkerStaticPool extends WorkerAbstract { private pool: StaticPool; /** * Create a new `WorkerStaticPool`. * * @param {string} workerScript + * @param {number} numberOfThreads + * @param {number} startWorkerDelay + * @param {PoolOptions} opts */ - constructor(workerScript: string, numThreads: number) { - super(workerScript); - this.pool = StaticPool.getInstance(numThreads, this.workerScript); + constructor(workerScript: string, numberOfThreads: number, startWorkerDelay?: number, opts?: PoolOptions) { + super(workerScript, startWorkerDelay); + this.pool = StaticPool.getInstance(numberOfThreads, this.workerScript, opts); } get size(): number { return this.pool.workers.length; } - get maxElementsPerWorker(): number { - return 1; + get maxElementsPerWorker(): number | null { + return null; } /** * - * @return {Promise} + * @returns {Promise} * @public */ // eslint-disable-next-line @typescript-eslint/no-empty-function @@ -37,34 +40,37 @@ export default class WorkerStaticPool extends Wrk { /** * - * @return {Promise} + * @returns {Promise} * @public */ - public async addElement(elementData: WorkerData): Promise { + public async stop(): Promise { + return this.pool.destroy(); + } + + /** + * + * @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(Constants.START_WORKER_DELAY); + await Utils.sleep(this.workerStartDelay); } } class StaticPool extends FixedThreadPool { private static instance: StaticPool; - private constructor(numThreads: number, workerScript: string, opts?: FixedThreadPoolOptions) { - super(numThreads, workerScript, opts); + private constructor(numberOfThreads: number, workerScript: string, opts?: PoolOptions) { + super(numberOfThreads, workerScript, opts); } - public static getInstance(numThreads: number, workerScript: string): StaticPool { + public static getInstance(numberOfThreads: number, workerScript: string, opts?: PoolOptions): StaticPool { if (!StaticPool.instance) { - StaticPool.instance = new StaticPool(numThreads, workerScript, - { - exitHandler: (code) => { - if (code !== 0) { - console.error(`Worker ${threadId} stopped with exit code ${code}`); - } - } - } - ); + opts.exitHandler = opts?.exitHandler ?? WorkerUtils.defaultExitHandler; + StaticPool.instance = new StaticPool(numberOfThreads, workerScript, opts); } return StaticPool.instance; }