| 1 | import type { EventEmitter } from 'node:events'; |
| 2 | import { existsSync } from 'node:fs'; |
| 3 | import type { Worker } from 'node:worker_threads'; |
| 4 | |
| 5 | import type { ErrorHandler, ExitHandler, PoolEmitter, PoolInfo } from 'poolifier'; |
| 6 | |
| 7 | import { WorkerConstants } from './WorkerConstants'; |
| 8 | import type { SetInfo, WorkerData, WorkerOptions } from './WorkerTypes'; |
| 9 | import { defaultErrorHandler, defaultExitHandler } from './WorkerUtils'; |
| 10 | |
| 11 | export abstract class WorkerAbstract<T extends WorkerData> { |
| 12 | protected readonly workerScript: string; |
| 13 | protected readonly workerOptions: WorkerOptions; |
| 14 | public abstract readonly info: PoolInfo | SetInfo; |
| 15 | public abstract readonly size: number; |
| 16 | public abstract readonly maxElementsPerWorker: number | undefined; |
| 17 | public abstract readonly emitter: EventEmitter | PoolEmitter | undefined; |
| 18 | |
| 19 | /** |
| 20 | * `WorkerAbstract` constructor. |
| 21 | * |
| 22 | * @param workerScript - |
| 23 | * @param workerOptions - |
| 24 | */ |
| 25 | constructor( |
| 26 | workerScript: string, |
| 27 | workerOptions: WorkerOptions = { |
| 28 | workerStartDelay: WorkerConstants.DEFAULT_WORKER_START_DELAY, |
| 29 | elementStartDelay: WorkerConstants.DEFAULT_ELEMENT_START_DELAY, |
| 30 | poolMinSize: WorkerConstants.DEFAULT_POOL_MIN_SIZE, |
| 31 | poolMaxSize: WorkerConstants.DEFAULT_POOL_MAX_SIZE, |
| 32 | elementsPerWorker: WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER, |
| 33 | poolOptions: {}, |
| 34 | } |
| 35 | ) { |
| 36 | if (workerScript === null || workerScript === undefined) { |
| 37 | throw new Error('Worker script is not defined'); |
| 38 | } |
| 39 | if (typeof workerScript === 'string' && workerScript.trim().length === 0) { |
| 40 | throw new Error('Worker script is empty'); |
| 41 | } |
| 42 | if (!existsSync(workerScript)) { |
| 43 | throw new Error('Worker script file does not exist'); |
| 44 | } |
| 45 | this.workerScript = workerScript; |
| 46 | this.workerOptions = workerOptions; |
| 47 | this.workerOptions.poolOptions?.messageHandler?.bind(this); |
| 48 | this.workerOptions.poolOptions.errorHandler = ( |
| 49 | this.workerOptions?.poolOptions?.errorHandler ?? defaultErrorHandler |
| 50 | ).bind(this) as ErrorHandler<Worker>; |
| 51 | this.workerOptions.poolOptions?.onlineHandler?.bind(this); |
| 52 | this.workerOptions.poolOptions.exitHandler = ( |
| 53 | this.workerOptions?.poolOptions?.exitHandler ?? defaultExitHandler |
| 54 | ).bind(this) as ExitHandler<Worker>; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Starts the worker pool/set. |
| 59 | */ |
| 60 | public abstract start(): Promise<void>; |
| 61 | /** |
| 62 | * Stops the worker pool/set. |
| 63 | */ |
| 64 | public abstract stop(): Promise<void>; |
| 65 | /** |
| 66 | * Adds a task element to the worker pool/set. |
| 67 | * |
| 68 | * @param elementData - |
| 69 | */ |
| 70 | public abstract addElement(elementData: T): Promise<void>; |
| 71 | } |