X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerStaticPool.ts;h=473ff3bba41e0a332394f23a313a8040cf554179;hb=be245fdab36274873e0a9651589cebd097548076;hp=c3209c251ed7c35b4d4511d84e198131a0eff174;hpb=0e4fa348298d36900560f7466f21f44090d2fe1b;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerStaticPool.ts b/src/worker/WorkerStaticPool.ts index c3209c25..473ff3bb 100644 --- a/src/worker/WorkerStaticPool.ts +++ b/src/worker/WorkerStaticPool.ts @@ -1,13 +1,12 @@ -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 type { 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; /** @@ -18,13 +17,6 @@ export default class WorkerStaticPool extends WorkerAbstract { */ 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, @@ -32,42 +24,36 @@ export default 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 | null { - return null; + 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 Utils.sleep(this.workerOptions.elementStartDelay)); + this.workerOptions.elementStartDelay > 0 && (await sleep(this.workerOptions.elementStartDelay)); } }