X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerStaticPool.ts;h=de1f5b29cfb89aebc39240584f9ed045ee6b1efb;hb=42486f2357b011f9244c6b29f4e05185138ce8d1;hp=8e591948ba362fee9d7f63d45a2c3640808e7bee;hpb=fd1fdf1b7bd53ee19679eb9c7d83b2592c32aed7;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerStaticPool.ts b/src/worker/WorkerStaticPool.ts index 8e591948..de1f5b29 100644 --- a/src/worker/WorkerStaticPool.ts +++ b/src/worker/WorkerStaticPool.ts @@ -1,43 +1,56 @@ -import { FixedThreadPool, PoolOptions } from 'poolifier'; +import type { Worker } from 'node:worker_threads'; -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 { type ErrorHandler, type ExitHandler, FixedThreadPool } from 'poolifier'; -export default class WorkerStaticPool extends WorkerAbstract { - private pool: StaticPool; +import { WorkerAbstract } from './WorkerAbstract'; +import type { WorkerData, WorkerOptions } from './WorkerTypes'; +import { WorkerUtils } from './WorkerUtils'; + +export class WorkerStaticPool extends WorkerAbstract { + private readonly pool: FixedThreadPool; /** * Create a new `WorkerStaticPool`. * - * @param {string} workerScript + * @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; + return this.pool.workerNodes.length; } - get maxElementsPerWorker(): number { - return 1; + get maxElementsPerWorker(): number | undefined { + return undefined; } /** * - * @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 { @@ -46,35 +59,14 @@ export default class WorkerStaticPool extends WorkerAbstract { /** * - * @return {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 WorkerUtils.sleep(this.workerOptions.elementStartDelay)); } }