X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerAbstract.ts;h=cd4dedf992fca40ff0b7cb787e91468d28020d55;hb=6a4032b5d8f3cbaa18d3beddcdfe9d335c1cba90;hp=0ef5470649c2e943e0ff7134cb89b9a3ed29ae3a;hpb=be245fdab36274873e0a9651589cebd097548076;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerAbstract.ts b/src/worker/WorkerAbstract.ts index 0ef54706..cd4dedf9 100644 --- a/src/worker/WorkerAbstract.ts +++ b/src/worker/WorkerAbstract.ts @@ -1,10 +1,8 @@ -import type EventEmitterAsyncResource from 'node:events'; -import fs from 'node:fs'; -import type { Worker } from 'node:worker_threads'; +import type { EventEmitterAsyncResource } from 'node:events'; +import { existsSync } from 'node:fs'; -import type { ErrorHandler, ExitHandler, PoolInfo } from 'poolifier'; +import type { PoolInfo } from 'poolifier'; -import { WorkerConstants } from './WorkerConstants'; import type { SetInfo, WorkerData, WorkerOptions } from './WorkerTypes'; import { defaultErrorHandler, defaultExitHandler } from './WorkerUtils'; @@ -22,48 +20,37 @@ 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: {}, + constructor(workerScript: string, 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)) { + if (!existsSync(workerScript)) { throw new Error('Worker script file does not exist'); } this.workerScript = workerScript; this.workerOptions = workerOptions; - this.workerOptions.poolOptions?.messageHandler?.bind(this); - this.workerOptions.poolOptions.errorHandler = ( - this.workerOptions?.poolOptions?.errorHandler ?? defaultErrorHandler - ).bind(this) as ErrorHandler; - this.workerOptions.poolOptions?.onlineHandler?.bind(this); - this.workerOptions.poolOptions.exitHandler = ( - this.workerOptions?.poolOptions?.exitHandler ?? defaultExitHandler - ).bind(this) as ExitHandler; + this.workerOptions.poolOptions!.errorHandler = + this.workerOptions.poolOptions?.errorHandler ?? defaultErrorHandler; + this.workerOptions.poolOptions!.exitHandler = + this.workerOptions.poolOptions?.exitHandler ?? defaultExitHandler; } /** - * Start the worker pool/set. + * Starts the worker pool/set. */ public abstract start(): Promise; /** - * Stop the worker pool/set. + * Stops the worker pool/set. */ public abstract stop(): Promise; /** - * Add a task element to the worker pool/set. + * Adds a task element to the worker pool/set. * * @param elementData - */