X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerStaticPool.ts;h=c3209c251ed7c35b4d4511d84e198131a0eff174;hb=e0b0ee2176a4dc5a8914a3fb094ec16b4966a740;hp=6421762918cc1ce9986a277cb1e46e58a5e16ccc;hpb=e71cccf31ce6e57fc1b0a8aeb97ef3d218f22b28;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerStaticPool.ts b/src/worker/WorkerStaticPool.ts index 64217629..c3209c25 100644 --- a/src/worker/WorkerStaticPool.ts +++ b/src/worker/WorkerStaticPool.ts @@ -1,44 +1,57 @@ -import { FixedThreadPool, PoolOptions } from 'poolifier'; +import type { Worker } from 'worker_threads'; -import Constants from '../utils/Constants'; +import { type ErrorHandler, type ExitHandler, FixedThreadPool } from 'poolifier'; + +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 WorkerStaticPool extends WorkerAbstract { - private pool: StaticPool; +export default class WorkerStaticPool extends WorkerAbstract { + private readonly pool: FixedThreadPool; /** * Create a new `WorkerStaticPool`. * - * @param {string} workerScript - * @param {number} numberOfThreads + * @param workerScript - + * @param workerOptions - */ - constructor(workerScript: string, numberOfThreads: number) { - super(workerScript); - this.pool = StaticPool.getInstance(numberOfThreads, this.workerScript); + constructor(workerScript: string, workerOptions?: WorkerOptions) { + super(workerScript, workerOptions); + this.workerOptions.poolOptions.errorHandler = ( + this.workerOptions?.poolOptions?.errorHandler ?? WorkerUtils.defaultErrorHandler + ).bind(this) as ErrorHandler; + this.workerOptions.poolOptions.exitHandler = ( + this.workerOptions?.poolOptions?.exitHandler ?? WorkerUtils.defaultExitHandler + ).bind(this) as ExitHandler; + this.workerOptions.poolOptions.messageHandler.bind(this); + this.pool = new FixedThreadPool( + 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 */ public async stop(): Promise { @@ -47,36 +60,14 @@ export default class WorkerStaticPool extends WorkerAbstract { /** * - * @param elementData - * @returns {Promise} + * @param elementData - + * @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; + // Start element sequentially to optimize memory at startup + this.workerOptions.elementStartDelay > 0 && + (await Utils.sleep(this.workerOptions.elementStartDelay)); } }