| 1 | import type { EventEmitterAsyncResource } from 'node:events' |
| 2 | |
| 3 | import { DynamicThreadPool, type PoolInfo } from 'poolifier' |
| 4 | |
| 5 | import { WorkerAbstract } from './WorkerAbstract.js' |
| 6 | import type { WorkerData, WorkerOptions } from './WorkerTypes.js' |
| 7 | import { randomizeDelay, sleep } from './WorkerUtils.js' |
| 8 | |
| 9 | export class WorkerDynamicPool<D extends WorkerData, R extends WorkerData> extends WorkerAbstract< |
| 10 | D, |
| 11 | R |
| 12 | > { |
| 13 | private readonly pool: DynamicThreadPool<D, R> |
| 14 | |
| 15 | /** |
| 16 | * Creates a new `WorkerDynamicPool`. |
| 17 | * |
| 18 | * @param workerScript - |
| 19 | * @param workerOptions - |
| 20 | */ |
| 21 | constructor (workerScript: string, workerOptions: WorkerOptions) { |
| 22 | super(workerScript, workerOptions) |
| 23 | this.pool = new DynamicThreadPool<D, R>( |
| 24 | this.workerOptions.poolMinSize, |
| 25 | this.workerOptions.poolMaxSize, |
| 26 | this.workerScript, |
| 27 | this.workerOptions.poolOptions |
| 28 | ) |
| 29 | } |
| 30 | |
| 31 | get info (): PoolInfo { |
| 32 | return this.pool.info |
| 33 | } |
| 34 | |
| 35 | get size (): number { |
| 36 | return this.pool.info.workerNodes |
| 37 | } |
| 38 | |
| 39 | get maxElementsPerWorker (): number | undefined { |
| 40 | return undefined |
| 41 | } |
| 42 | |
| 43 | get emitter (): EventEmitterAsyncResource | undefined { |
| 44 | return this.pool.emitter |
| 45 | } |
| 46 | |
| 47 | /** @inheritDoc */ |
| 48 | public start (): void { |
| 49 | this.pool.start() |
| 50 | } |
| 51 | |
| 52 | /** @inheritDoc */ |
| 53 | public async stop (): Promise<void> { |
| 54 | await this.pool.destroy() |
| 55 | } |
| 56 | |
| 57 | /** @inheritDoc */ |
| 58 | public async addElement (elementData: D): Promise<R> { |
| 59 | const response = await this.pool.execute(elementData) |
| 60 | // Start element sequentially to optimize memory at startup |
| 61 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 62 | this.workerOptions.elementAddDelay! > 0 && |
| 63 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 64 | (await sleep(randomizeDelay(this.workerOptions.elementAddDelay!))) |
| 65 | return response |
| 66 | } |
| 67 | } |