X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerAbstract.ts;h=0ef5470649c2e943e0ff7134cb89b9a3ed29ae3a;hb=e028497c367f38b3d1303634d65bb90d764374cb;hp=7753e64d392f8f960f24f6b41c29340e0e71f79b;hpb=8baf3f8f3e3330c90cbd3474b6f5c7d589be17ff;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerAbstract.ts b/src/worker/WorkerAbstract.ts index 7753e64d..0ef54706 100644 --- a/src/worker/WorkerAbstract.ts +++ b/src/worker/WorkerAbstract.ts @@ -1,13 +1,20 @@ +import type EventEmitterAsyncResource from 'node:events'; import fs from 'node:fs'; +import type { Worker } from 'node:worker_threads'; + +import type { ErrorHandler, ExitHandler, PoolInfo } from 'poolifier'; import { WorkerConstants } from './WorkerConstants'; -import type { WorkerData, WorkerOptions } from './WorkerTypes'; +import type { SetInfo, WorkerData, WorkerOptions } from './WorkerTypes'; +import { defaultErrorHandler, defaultExitHandler } from './WorkerUtils'; export abstract class WorkerAbstract { protected readonly workerScript: string; protected readonly workerOptions: WorkerOptions; + public abstract readonly info: PoolInfo | SetInfo; public abstract readonly size: number; public abstract readonly maxElementsPerWorker: number | undefined; + public abstract readonly emitter: EventEmitterAsyncResource | undefined; /** * `WorkerAbstract` constructor. @@ -24,7 +31,6 @@ export abstract class WorkerAbstract { poolMaxSize: WorkerConstants.DEFAULT_POOL_MAX_SIZE, elementsPerWorker: WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER, poolOptions: {}, - messageHandler: WorkerConstants.EMPTY_FUNCTION, } ) { if (workerScript === null || workerScript === undefined) { @@ -38,6 +44,14 @@ export abstract class WorkerAbstract { } this.workerScript = workerScript; this.workerOptions = workerOptions; + this.workerOptions.poolOptions?.messageHandler?.bind(this); + this.workerOptions.poolOptions.errorHandler = ( + this.workerOptions?.poolOptions?.errorHandler ?? defaultErrorHandler + ).bind(this) as ErrorHandler; + this.workerOptions.poolOptions?.onlineHandler?.bind(this); + this.workerOptions.poolOptions.exitHandler = ( + this.workerOptions?.poolOptions?.exitHandler ?? defaultExitHandler + ).bind(this) as ExitHandler; } /**