X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerStaticPool.ts;h=6034c6e041ac77a487e252ccc0ac8c6a3c045030;hb=8baf3f8f3e3330c90cbd3474b6f5c7d589be17ff;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..6034c6e0 100644 --- a/src/worker/WorkerStaticPool.ts +++ b/src/worker/WorkerStaticPool.ts @@ -1,13 +1,12 @@ -import type { Worker } from 'worker_threads'; +import type { Worker } from 'node:worker_threads'; import { type ErrorHandler, type ExitHandler, FixedThreadPool } from 'poolifier'; -import WorkerAbstract from './WorkerAbstract'; -import { WorkerUtils } from './WorkerUtils'; -import type { WorkerData, WorkerOptions } from '../types/Worker'; -import Utils from '../utils/Utils'; +import { WorkerAbstract } from './WorkerAbstract'; +import type { WorkerData, WorkerOptions } from './WorkerTypes'; +import { defaultErrorHandler, defaultExitHandler, sleep } from './WorkerUtils'; -export default class WorkerStaticPool extends WorkerAbstract { +export class WorkerStaticPool extends WorkerAbstract { private readonly pool: FixedThreadPool; /** @@ -19,10 +18,10 @@ export default class WorkerStaticPool extends WorkerAbstract { constructor(workerScript: string, workerOptions?: WorkerOptions) { super(workerScript, workerOptions); this.workerOptions.poolOptions.errorHandler = ( - this.workerOptions?.poolOptions?.errorHandler ?? WorkerUtils.defaultErrorHandler + this.workerOptions?.poolOptions?.errorHandler ?? defaultErrorHandler ).bind(this) as ErrorHandler; this.workerOptions.poolOptions.exitHandler = ( - this.workerOptions?.poolOptions?.exitHandler ?? WorkerUtils.defaultExitHandler + this.workerOptions?.poolOptions?.exitHandler ?? defaultExitHandler ).bind(this) as ExitHandler; this.workerOptions.poolOptions.messageHandler.bind(this); this.pool = new FixedThreadPool( @@ -33,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 | 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)); } }