X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerAbstract.ts;h=0fccdb812d7ed4c2a40b28596a3cfc883ed606e3;hb=59395dc149654128f2996f4999bccb66270f54e4;hp=fa28597c55784d585fcec7231f0aea69e298bee1;hpb=f2bf9948496c724976a2e05d2f20cb17700373ec;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerAbstract.ts b/src/worker/WorkerAbstract.ts index fa28597c..0fccdb81 100644 --- a/src/worker/WorkerAbstract.ts +++ b/src/worker/WorkerAbstract.ts @@ -1,24 +1,45 @@ -import Constants from '../utils/Constants'; -import { WorkerData } from '../types/Worker'; +import fs from 'fs'; -export default abstract class WorkerAbstract { +import WorkerConstants from './WorkerConstants'; +import type { WorkerData, WorkerOptions } from '../types/Worker'; + +export default abstract class WorkerAbstract { protected readonly workerScript: string; - protected readonly workerStartDelay: number; + protected readonly workerOptions: WorkerOptions; public abstract readonly size: number; public abstract readonly maxElementsPerWorker: number | null; /** * `WorkerAbstract` constructor. * - * @param workerScript - * @param 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: {}, + messageHandler: () => { + /* This is intentional */ + }, + } + ) { + 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; }