X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerAbstract.ts;h=912f5256575bb2ca9a5b486bb8ed0cce8cd5bd9e;hb=ee7c1da0e12c70134f31537a1c1e7040d309af5a;hp=d5f9a58e4a00eeed20e24d3e9605776f943f2114;hpb=81797102d5214fea2fc58eff2666fe8b8d9a5a11;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerAbstract.ts b/src/worker/WorkerAbstract.ts index d5f9a58e..912f5256 100644 --- a/src/worker/WorkerAbstract.ts +++ b/src/worker/WorkerAbstract.ts @@ -1,28 +1,55 @@ -import Constants from '../utils/Constants'; -import { WorkerData } from '../types/Worker'; +import type { EventEmitterAsyncResource } from 'node:events'; +import { existsSync } from 'node:fs'; -export default abstract class WorkerAbstract { +import type { PoolInfo } from 'poolifier'; + +import type { SetInfo, WorkerData, WorkerOptions } from './WorkerTypes'; +import { defaultErrorHandler, defaultExitHandler } from './WorkerUtils'; + +export abstract class WorkerAbstract { protected readonly workerScript: string; - protected readonly workerStartDelay: number; - protected readonly messageListener: (message: any) => void; - public abstract size: number; - public abstract maxElementsPerWorker: number | null; + 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. * - * @param workerScript - * @param workerStartDelay - * @param messageListenerCallback + * @param workerScript - + * @param workerOptions - */ - constructor(workerScript: string, workerStartDelay: number = Constants.WORKER_START_DELAY, - messageListenerCallback: (message: any) => void = () => { /* This is intentional */ }) { + constructor(workerScript: string, workerOptions: WorkerOptions) { + if (workerScript === null || workerScript === undefined) { + throw new Error('Worker script is not defined'); + } + if (typeof workerScript === 'string' && workerScript.trim().length === 0) { + throw new Error('Worker script is empty'); + } + if (!existsSync(workerScript)) { + throw new Error('Worker script file does not exist'); + } this.workerScript = workerScript; - this.workerStartDelay = workerStartDelay; - this.messageListener = messageListenerCallback; + this.workerOptions = workerOptions; + this.workerOptions.poolOptions!.errorHandler = + this.workerOptions.poolOptions?.errorHandler ?? defaultErrorHandler; + this.workerOptions.poolOptions!.exitHandler = + this.workerOptions.poolOptions?.exitHandler ?? defaultExitHandler; } + /** + * Starts the worker pool/set. + */ public abstract start(): Promise; + /** + * Stops the worker pool/set. + */ public abstract stop(): Promise; - public abstract addElement(elementData: WorkerData): Promise; + /** + * Adds a task element to the worker pool/set. + * + * @param elementData - + */ + public abstract addElement(elementData: T): Promise; }