X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fworker%2FWorkerStaticPool.ts;h=d5d6390c7b2e43ad5175fa46b21c41122879e7ed;hb=9ab62ed05a70a1feda05803f085a449ce35a4f14;hp=1de7cba23304a7f3812dd906a9c64162a3f39dc9;hpb=ded13d9799aa6fb958da48a6b702d4193e7954f2;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerStaticPool.ts b/src/worker/WorkerStaticPool.ts index 1de7cba2..d5d6390c 100644 --- a/src/worker/WorkerStaticPool.ts +++ b/src/worker/WorkerStaticPool.ts @@ -1,42 +1,48 @@ -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 { WorkerUtils } from './WorkerUtils'; -export default class WorkerStaticPool extends Wrk { - 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, numThreads: number) { - super(workerScript); - this.pool = StaticPool.getInstance(numThreads, 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 { - return 1; + get maxElementsPerWorker(): number | null { + return null; } /** * - * @return {Promise} + * @returns * @public */ - // eslint-disable-next-line @typescript-eslint/no-empty-function - public async start(): Promise { } + public async start(): Promise { + // This is intentional + } /** * - * @return {Promise} + * @returns * @public */ public async stop(): Promise { @@ -45,35 +51,13 @@ export default class WorkerStaticPool extends Wrk { /** * - * @return {Promise} + * @param elementData + * @returns * @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); - } -} - -class StaticPool extends FixedThreadPool { - private static instance: StaticPool; - - private constructor(numThreads: number, workerScript: string, opts?: FixedThreadPoolOptions) { - super(numThreads, workerScript, opts); - } - - 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; + await Utils.sleep(this.workerStartDelay); } }