| 1 | import { DynamicThreadPool } from 'poolifier'; |
| 2 | |
| 3 | import type { WorkerData, WorkerOptions } from '../types/Worker'; |
| 4 | import Utils from '../utils/Utils'; |
| 5 | import WorkerAbstract from './WorkerAbstract'; |
| 6 | import { WorkerUtils } from './WorkerUtils'; |
| 7 | |
| 8 | export default class WorkerDynamicPool extends WorkerAbstract<WorkerData> { |
| 9 | private readonly pool: DynamicThreadPool<WorkerData>; |
| 10 | |
| 11 | /** |
| 12 | * Create a new `WorkerDynamicPool`. |
| 13 | * |
| 14 | * @param workerScript |
| 15 | * @param workerOptions |
| 16 | */ |
| 17 | constructor(workerScript: string, workerOptions?: WorkerOptions) { |
| 18 | super(workerScript, workerOptions); |
| 19 | this.workerOptions.poolOptions.errorHandler = |
| 20 | this.workerOptions?.poolOptions?.errorHandler ?? WorkerUtils.defaultErrorHandler; |
| 21 | this.workerOptions.poolOptions.exitHandler = |
| 22 | this.workerOptions?.poolOptions?.exitHandler ?? WorkerUtils.defaultExitHandler; |
| 23 | this.pool = new DynamicThreadPool<WorkerData>( |
| 24 | this.workerOptions.poolMinSize, |
| 25 | this.workerOptions.poolMaxSize, |
| 26 | this.workerScript, |
| 27 | this.workerOptions.poolOptions |
| 28 | ); |
| 29 | } |
| 30 | |
| 31 | get size(): number { |
| 32 | return this.pool.workers.length; |
| 33 | } |
| 34 | |
| 35 | get maxElementsPerWorker(): number | null { |
| 36 | return null; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * |
| 41 | * @returns |
| 42 | * @public |
| 43 | */ |
| 44 | public async start(): Promise<void> { |
| 45 | // This is intentional |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * |
| 50 | * @returns |
| 51 | * @public |
| 52 | */ |
| 53 | public async stop(): Promise<void> { |
| 54 | return this.pool.destroy(); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * |
| 59 | * @param elementData |
| 60 | * @returns |
| 61 | * @public |
| 62 | */ |
| 63 | public async addElement(elementData: WorkerData): Promise<void> { |
| 64 | await this.pool.execute(elementData); |
| 65 | // Start element sequentially to optimize memory at startup |
| 66 | this.workerOptions.elementStartDelay > 0 && |
| 67 | (await Utils.sleep(this.workerOptions.elementStartDelay)); |
| 68 | } |
| 69 | } |