X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerAbstract.ts;h=f6713f8976d2cdda47d74f5951880f8d1fdfd48d;hb=8fb8f42ace8b9295865adebaeb7845e064c05e71;hp=351a61d5284b54b037e6c181882526d5ddc7fe0b;hpb=4bfd80fa794b4b2f89427ef9f5944664ce3bdade;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerAbstract.ts b/src/worker/WorkerAbstract.ts index 351a61d5..f6713f89 100644 --- a/src/worker/WorkerAbstract.ts +++ b/src/worker/WorkerAbstract.ts @@ -1,30 +1,53 @@ -import { WorkerData, WorkerStartOptions } 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 { - protected readonly workerScript: string; - protected readonly workerStartDelay: number; - protected readonly elementStartDelay: number; - public abstract readonly size: number; - public abstract readonly maxElementsPerWorker: number | null; +import type { SetInfo, WorkerData, WorkerOptions } from './WorkerTypes.js' + +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. * - * @param workerScript - * @param workerStartOptions + * @param workerScript - + * @param workerOptions - */ - constructor(workerScript: string, workerStartOptions: WorkerStartOptions = { - workerStartDelay: Constants.WORKER_START_DELAY, - elementStartDelay: Constants.ELEMENT_START_DELAY - }) { - this.workerScript = workerScript; - this.workerStartDelay = workerStartOptions.workerStartDelay; - this.elementStartDelay = workerStartOptions.elementStartDelay; + constructor (workerScript: string | undefined, 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 } - public abstract start(): Promise; - public abstract stop(): Promise; - public abstract addElement(elementData: T): Promise; + /** + * Starts the worker pool/set. + */ + public abstract start (): void | 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: D): Promise }