| 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 extends WorkerAbstract<WorkerData> { |
| 10 | private readonly pool: DynamicThreadPool<WorkerData> |
| 11 | |
| 12 | /** |
| 13 | * Creates a new `WorkerDynamicPool`. |
| 14 | * |
| 15 | * @param workerScript - |
| 16 | * @param workerOptions - |
| 17 | */ |
| 18 | constructor (workerScript: string, workerOptions: WorkerOptions) { |
| 19 | super(workerScript, workerOptions) |
| 20 | this.pool = new DynamicThreadPool<WorkerData>( |
| 21 | this.workerOptions.poolMinSize, |
| 22 | this.workerOptions.poolMaxSize, |
| 23 | this.workerScript, |
| 24 | this.workerOptions.poolOptions |
| 25 | ) |
| 26 | } |
| 27 | |
| 28 | get info (): PoolInfo { |
| 29 | return this.pool.info |
| 30 | } |
| 31 | |
| 32 | get size (): number { |
| 33 | return this.pool.info.workerNodes |
| 34 | } |
| 35 | |
| 36 | get maxElementsPerWorker (): number | undefined { |
| 37 | return undefined |
| 38 | } |
| 39 | |
| 40 | get emitter (): EventEmitterAsyncResource | undefined { |
| 41 | return this.pool.emitter |
| 42 | } |
| 43 | |
| 44 | /** @inheritDoc */ |
| 45 | public async start (): Promise<void> { |
| 46 | // This is intentional |
| 47 | } |
| 48 | |
| 49 | /** @inheritDoc */ |
| 50 | public async stop (): Promise<void> { |
| 51 | await this.pool.destroy() |
| 52 | } |
| 53 | |
| 54 | /** @inheritDoc */ |
| 55 | public async addElement (elementData: WorkerData): Promise<void> { |
| 56 | await this.pool.execute(elementData) |
| 57 | // Start element sequentially to optimize memory at startup |
| 58 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 59 | this.workerOptions.elementStartDelay! > 0 && |
| 60 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 61 | (await sleep(randomizeDelay(this.workerOptions.elementStartDelay!))) |
| 62 | } |
| 63 | } |