X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerStaticPool.ts;h=b1cc718949b4d0ea94d3c338315bf8284cd45e43;hb=c1c97db8708f26af10385f6e86af7cd933891bb8;hp=2ec049e15514d356fbb0b787c43f48e3efd9d1ff;hpb=72092cfcf8a31c06e4592b25e060e2d74d2ed99c;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerStaticPool.ts b/src/worker/WorkerStaticPool.ts index 2ec049e1..b1cc7189 100644 --- a/src/worker/WorkerStaticPool.ts +++ b/src/worker/WorkerStaticPool.ts @@ -1,73 +1,58 @@ -import type { Worker } from 'worker_threads'; +import { FixedThreadPool, type PoolEmitter, type PoolInfo } from 'poolifier'; -import { type ErrorHandler, type ExitHandler, FixedThreadPool } from 'poolifier'; +import { WorkerAbstract } from './WorkerAbstract'; +import type { WorkerData, WorkerOptions } from './WorkerTypes'; +import { sleep } from './WorkerUtils'; -import WorkerAbstract from './WorkerAbstract'; -import { WorkerUtils } from './WorkerUtils'; -import type { WorkerData, WorkerOptions } from '../types/Worker'; -import Utils from '../utils/Utils'; - -export default class WorkerStaticPool extends WorkerAbstract { +export class WorkerStaticPool extends WorkerAbstract { private readonly pool: FixedThreadPool; /** - * Create a new `WorkerStaticPool`. + * Creates a new `WorkerStaticPool`. * * @param workerScript - * @param workerOptions - */ 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 + this.workerOptions.poolOptions, ); } + get info(): PoolInfo { + return this.pool.info; + } + get size(): number { - return this.pool.workers.length; + return this.pool.info.workerNodes; } get maxElementsPerWorker(): number | undefined { return undefined; } - /** - * - * @returns - * @public - */ + get emitter(): PoolEmitter | undefined { + return this.pool?.emitter; + } + + /** @inheritDoc */ public async start(): Promise { // This is intentional } - /** - * - * @returns - * @public - */ + /** @inheritDoc */ public async stop(): Promise { return this.pool.destroy(); } - /** - * - * @param elementData - - * @returns - * @public - */ + /** @inheritDoc */ public async addElement(elementData: WorkerData): Promise { await this.pool.execute(elementData); // Start element sequentially to optimize memory at startup - this.workerOptions.elementStartDelay > 0 && - (await Utils.sleep(this.workerOptions.elementStartDelay)); + this.workerOptions.elementStartDelay! > 0 && + (await sleep(this.workerOptions.elementStartDelay!)); } }