X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerAbstract.ts;h=cd4dedf992fca40ff0b7cb787e91468d28020d55;hb=6a4032b5d8f3cbaa18d3beddcdfe9d335c1cba90;hp=c3e0ed88e9ae6fa4581b6e99b65bffa20931a546;hpb=e7aeea18e189dd087c8f951cf77a253e2818ae90;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerAbstract.ts b/src/worker/WorkerAbstract.ts index c3e0ed88..cd4dedf9 100644 --- a/src/worker/WorkerAbstract.ts +++ b/src/worker/WorkerAbstract.ts @@ -1,38 +1,58 @@ -import { WorkerData, WorkerOptions } from '../types/Worker'; +import type { EventEmitterAsyncResource } from 'node:events'; +import { existsSync } from 'node:fs'; -import Constants from '../utils/Constants'; +import type { 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: EventEmitterAsyncResource | 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) { + throw new TypeError('Worker script is not defined'); + } + if (typeof workerScript !== 'string') { + throw new TypeError('Worker script must be a string'); + } + if (workerScript.trim().length === 0) { + throw new Error('Worker script is an empty string'); + } + 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; }