X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerAbstract.ts;h=2803eea464da27115e45e1a6a3546fdee46418a9;hb=93f0c2c87ceeb26d4925bee5708ea8156f4b335f;hp=715778161af13b59bed90aa2805b0120451c64b8;hpb=101196c654ddcd686e4992f58864f591ceea6c00;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerAbstract.ts b/src/worker/WorkerAbstract.ts index 71577816..2803eea4 100644 --- a/src/worker/WorkerAbstract.ts +++ b/src/worker/WorkerAbstract.ts @@ -1,24 +1,43 @@ -import Constants from '../utils/Constants'; -import { WorkerData } from '../types/Worker'; +import fs from 'node:fs'; -export default abstract class WorkerAbstract { +import { WorkerConstants } from './WorkerConstants'; +import type { WorkerData, WorkerOptions } from './WorkerTypes'; + +export abstract class WorkerAbstract { protected readonly workerScript: string; - protected readonly workerStartDelay: number; - public abstract size: number; - public abstract maxElementsPerWorker: number | null; + protected readonly workerOptions: WorkerOptions; + public abstract readonly size: number; + public abstract readonly maxElementsPerWorker: number | undefined; /** * `WorkerAbstract` constructor. * - * @param {string} workerScript - * @param {number} workerStartDelay + * @param workerScript - + * @param workerOptions - */ - constructor(workerScript: string, workerStartDelay: number = Constants.WORKER_START_DELAY) { + 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) { + throw new Error('Worker script is not defined'); + } + if (!fs.existsSync(workerScript)) { + throw new Error('Worker script file does not exist'); + } this.workerScript = workerScript; - this.workerStartDelay = workerStartDelay; + this.workerOptions = workerOptions; } public abstract start(): Promise; public abstract stop(): Promise; - public abstract addElement(elementData: WorkerData): Promise; + public abstract addElement(elementData: T): Promise; }