X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerStaticPool.ts;h=b1cc718949b4d0ea94d3c338315bf8284cd45e43;hb=c1c97db8708f26af10385f6e86af7cd933891bb8;hp=99ea301d5d1a05ef8fbdd3627c3b04aec54be596;hpb=8114d10e3893e96bb725ce2fca9744429ee4b75b;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerStaticPool.ts b/src/worker/WorkerStaticPool.ts index 99ea301d..b1cc7189 100644 --- a/src/worker/WorkerStaticPool.ts +++ b/src/worker/WorkerStaticPool.ts @@ -1,66 +1,58 @@ -import { FixedThreadPool } from 'poolifier'; +import { FixedThreadPool, type PoolEmitter, type PoolInfo } from 'poolifier'; -import { WorkerData, WorkerOptions } from '../types/Worker'; -import Utils from '../utils/Utils'; -import WorkerAbstract from './WorkerAbstract'; -import { WorkerUtils } from './WorkerUtils'; +import { WorkerAbstract } from './WorkerAbstract'; +import type { WorkerData, WorkerOptions } from './WorkerTypes'; +import { sleep } from './WorkerUtils'; -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 + * @param workerScript - + * @param workerOptions - */ constructor(workerScript: string, workerOptions?: WorkerOptions) { super(workerScript, workerOptions); - this.workerOptions.poolOptions.exitHandler = - this.workerOptions?.poolOptions?.exitHandler ?? WorkerUtils.defaultExitHandler; 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 | null { - return null; + 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!)); } }