X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=inline;f=src%2Fworker%2FWorkerStaticPool.ts;h=e49ac2613e97e0da7dc7857b7ec2a4b3cf3f2fd1;hb=361c98f57255e5b91d123d5f2ba43ab533134b1a;hp=58e4c65a072091ced7c0fff24bd68d09bddf7bdc;hpb=268a74bb051fcbbad532fd833f0d8fd2b33b6c64;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerStaticPool.ts b/src/worker/WorkerStaticPool.ts index 58e4c65a..e49ac261 100644 --- a/src/worker/WorkerStaticPool.ts +++ b/src/worker/WorkerStaticPool.ts @@ -1,29 +1,22 @@ -import type { Worker } from 'worker_threads'; +import type EventEmitterAsyncResource from 'node:events'; -import { type ErrorHandler, type ExitHandler, FixedThreadPool } from 'poolifier'; +import { FixedThreadPool, type PoolInfo } from 'poolifier'; import { WorkerAbstract } from './WorkerAbstract'; import type { WorkerData, WorkerOptions } from './WorkerTypes'; -import { WorkerUtils } from './WorkerUtils'; +import { sleep } from './WorkerUtils'; 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, @@ -31,42 +24,36 @@ export class WorkerStaticPool extends WorkerAbstract { ); } + 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(): EventEmitterAsyncResource | 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 WorkerUtils.sleep(this.workerOptions.elementStartDelay)); + this.workerOptions.elementStartDelay > 0 && (await sleep(this.workerOptions.elementStartDelay)); } }