1 import type { EventEmitterAsyncResource
} from
'node:events'
3 import { DynamicThreadPool
, type PoolInfo
} from
'poolifier'
5 import { WorkerAbstract
} from
'./WorkerAbstract.js'
6 import type { WorkerData
, WorkerOptions
} from
'./WorkerTypes.js'
7 import { randomizeDelay
, sleep
} from
'./WorkerUtils.js'
9 export class WorkerDynamicPool
extends WorkerAbstract
<WorkerData
> {
10 private readonly pool
: DynamicThreadPool
<WorkerData
>
13 * Creates a new `WorkerDynamicPool`.
15 * @param workerScript -
16 * @param workerOptions -
18 constructor (workerScript
: string, workerOptions
: WorkerOptions
) {
19 super(workerScript
, workerOptions
)
20 this.pool
= new DynamicThreadPool
<WorkerData
>(
21 this.workerOptions
.poolMinSize
,
22 this.workerOptions
.poolMaxSize
,
24 this.workerOptions
.poolOptions
28 get
info (): PoolInfo
{
33 return this.pool
.info
.workerNodes
36 get
maxElementsPerWorker (): number | undefined {
40 get
emitter (): EventEmitterAsyncResource
| undefined {
41 return this.pool
.emitter
45 public async start (): Promise
<void> {
46 // This is intentional
50 public async stop (): Promise
<void> {
51 await this.pool
.destroy()
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
!)))