X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerAbstract.ts;h=912f5256575bb2ca9a5b486bb8ed0cce8cd5bd9e;hb=42b8cf5cdca8eaab1e7442f7c92c2a5ed97434f6;hp=2803eea464da27115e45e1a6a3546fdee46418a9;hpb=59b6ed8d1db313ef3371efd8ab5e039cf3dedab0;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerAbstract.ts b/src/worker/WorkerAbstract.ts index 2803eea4..912f5256 100644 --- a/src/worker/WorkerAbstract.ts +++ b/src/worker/WorkerAbstract.ts @@ -1,13 +1,18 @@ -import fs from 'node:fs'; +import type { EventEmitterAsyncResource } from 'node:events'; +import { existsSync } from 'node:fs'; -import { WorkerConstants } from './WorkerConstants'; -import type { WorkerData, WorkerOptions } from './WorkerTypes'; +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 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. @@ -15,29 +20,36 @@ export abstract class WorkerAbstract { * @param workerScript - * @param workerOptions - */ - constructor( - workerScript: string, - workerOptions: WorkerOptions = { - workerStartDelay: WorkerConstants.DEFAULT_WORKER_START_DELAY, - elementStartDelay: WorkerConstants.DEFAULT_ELEMENT_START_DELAY, - poolMinSize: WorkerConstants.DEFAULT_POOL_MIN_SIZE, - poolMaxSize: WorkerConstants.DEFAULT_POOL_MAX_SIZE, - elementsPerWorker: WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER, - poolOptions: WorkerConstants.EMPTY_OBJECT, - messageHandler: WorkerConstants.EMPTY_FUNCTION, - } - ) { - if (!workerScript) { + constructor(workerScript: string, workerOptions: WorkerOptions) { + if (workerScript === null || workerScript === undefined) { throw new Error('Worker script is not defined'); } - if (!fs.existsSync(workerScript)) { + 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; }