X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerAbstract.ts;h=32319ab2ee87d6faf8af97a2dfcd9c74babaa9fc;hb=d929adcc32a8cc79f0c7182d16f70367b001d28c;hp=12cc7c735f257ed624c8de35d5198865361edec7;hpb=4d7227e61934a6b082a4d89268c454f7ee3605e1;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerAbstract.ts b/src/worker/WorkerAbstract.ts index 12cc7c73..32319ab2 100644 --- a/src/worker/WorkerAbstract.ts +++ b/src/worker/WorkerAbstract.ts @@ -1,33 +1,55 @@ -import { WorkerData, WorkerOptions } from '../types/Worker'; +import type { EventEmitter } from 'node:events'; +import { existsSync } from 'node:fs'; -import Constants from '../utils/Constants'; +import type { PoolEmitter, PoolInfo } from 'poolifier'; -export default abstract class WorkerAbstract { +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 | null; + public abstract readonly maxElementsPerWorker: number | undefined; + public abstract readonly emitter: EventEmitter | PoolEmitter | undefined; /** * `WorkerAbstract` constructor. * - * @param workerScript - * @param workerOptions + * @param workerScript - + * @param workerOptions - */ - constructor(workerScript: string, workerOptions: WorkerOptions = { - workerStartDelay: Constants.WORKER_START_DELAY, - elementStartDelay: Constants.ELEMENT_START_DELAY, - poolMinSize: Constants.DEFAULT_WORKER_POOL_MIN_SIZE, - poolMaxSize: Constants.DEFAULT_WORKER_POOL_MAX_SIZE, - elementsPerWorker: Constants.DEFAULT_CHARGING_STATIONS_PER_WORKER, - poolOptions: {}, - messageHandler: () => { /* 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.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; + /** + * Adds a task element to the worker pool/set. + * + * @param elementData - + */ public abstract addElement(elementData: T): Promise; }