X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerAbstract.ts;h=f6713f8976d2cdda47d74f5951880f8d1fdfd48d;hb=3b09e788c6dab8e5a8b3314e8e69ede16ff82fcc;hp=16d566bde2ae60049fe401c7f13f830af4ce17dd;hpb=ba516f9cbce4070d1b5eda82a2cd48b82a67c500;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerAbstract.ts b/src/worker/WorkerAbstract.ts index 16d566bd..f6713f89 100644 --- a/src/worker/WorkerAbstract.ts +++ b/src/worker/WorkerAbstract.ts @@ -1,13 +1,17 @@ -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' -export abstract class WorkerAbstract { - protected readonly workerScript: string; - protected readonly workerOptions: WorkerOptions; - public abstract readonly size: number; - public abstract readonly maxElementsPerWorker: number | undefined; +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. @@ -15,32 +19,35 @@ 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: {}, - messageHandler: WorkerConstants.EMPTY_FUNCTION, + constructor (workerScript: string | undefined, workerOptions: WorkerOptions) { + if (workerScript == null) { + throw new TypeError('Worker script is not defined') } - ) { - if (workerScript === null || workerScript === undefined) { - throw new Error('Worker script is not defined'); + if (typeof workerScript !== 'string') { + throw new TypeError('Worker script must be a string') } - if (typeof workerScript === 'string' && workerScript.trim().length === 0) { - throw new Error('Worker script is empty'); + if (workerScript.trim().length === 0) { + throw new Error('Worker script is an empty string') } - if (!fs.existsSync(workerScript)) { - throw new Error('Worker script file does not exist'); + if (!existsSync(workerScript)) { + throw new Error('Worker script file does not exist') } - this.workerScript = workerScript; - this.workerOptions = workerOptions; + 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 }