X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerStaticPool.ts;h=6034c6e041ac77a487e252ccc0ac8c6a3c045030;hb=8baf3f8f3e3330c90cbd3474b6f5c7d589be17ff;hp=6fc91524173e614e1fb4ff1a7bae586c5e216b22;hpb=6c1761d470507ea23d186be61b94ca7375c5144a;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerStaticPool.ts b/src/worker/WorkerStaticPool.ts index 6fc91524..6034c6e0 100644 --- a/src/worker/WorkerStaticPool.ts +++ b/src/worker/WorkerStaticPool.ts @@ -1,23 +1,29 @@ -import { FixedThreadPool } from 'poolifier'; +import type { Worker } from 'node:worker_threads'; -import type { WorkerData, WorkerOptions } from '../types/Worker'; -import Utils from '../utils/Utils'; -import WorkerAbstract from './WorkerAbstract'; -import { WorkerUtils } from './WorkerUtils'; +import { type ErrorHandler, type ExitHandler, FixedThreadPool } from 'poolifier'; -export default class WorkerStaticPool extends WorkerAbstract { +import { WorkerAbstract } from './WorkerAbstract'; +import type { WorkerData, WorkerOptions } from './WorkerTypes'; +import { defaultErrorHandler, defaultExitHandler, sleep } from './WorkerUtils'; + +export class WorkerStaticPool extends WorkerAbstract { private readonly pool: FixedThreadPool; /** * Create 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.workerOptions.poolOptions.errorHandler = ( + this.workerOptions?.poolOptions?.errorHandler ?? defaultErrorHandler + ).bind(this) as ErrorHandler; + this.workerOptions.poolOptions.exitHandler = ( + this.workerOptions?.poolOptions?.exitHandler ?? defaultExitHandler + ).bind(this) as ExitHandler; + this.workerOptions.poolOptions.messageHandler.bind(this); this.pool = new FixedThreadPool( this.workerOptions.poolMaxSize, this.workerScript, @@ -26,41 +32,27 @@ export default class WorkerStaticPool extends WorkerAbstract { } get size(): number { - return this.pool.workers.length; + return this.pool.workerNodes.length; } - get maxElementsPerWorker(): number | null { - return null; + get maxElementsPerWorker(): number | undefined { + return undefined; } - /** - * - * @returns - * @public - */ + /** @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)); } }