X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerAbstract.ts;h=38d762762634ad01339d081e2db0205d6b609e89;hb=88702641858698449f0388a3fae2d39367fe1c3a;hp=6a8a0a0ff07dc9003816ea6f76ef9e356d22a1de;hpb=95250e1b3f40c916420bb693502fda4b0e49ac33;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerAbstract.ts b/src/worker/WorkerAbstract.ts index 6a8a0a0f..38d76276 100644 --- a/src/worker/WorkerAbstract.ts +++ b/src/worker/WorkerAbstract.ts @@ -1,20 +1,53 @@ -import { WorkerData } from '../types/Worker'; +import type { EventEmitterAsyncResource } from 'node:events' +import { existsSync } from 'node:fs' -export default abstract class WorkerAbstract { - protected workerScript: string; - public abstract size: number; - public abstract maxElementsPerWorker: number; +import type { PoolInfo } from 'poolifier' + +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 /** - * Create a new `Worker` implementation. + * `WorkerAbstract` constructor. * - * @param {string} workerScript + * @param workerScript - + * @param workerOptions - */ - constructor(workerScript: string) { - this.workerScript = workerScript; + 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 } - public abstract start(): Promise; - public abstract stop(): Promise; - public abstract addElement(elementData: WorkerData): Promise; + /** + * 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 }