1 import { FixedThreadPool
, type PoolEmitter
, type PoolInfo
} from
'poolifier';
3 import { WorkerAbstract
} from
'./WorkerAbstract';
4 import type { WorkerData
, WorkerOptions
} from
'./WorkerTypes';
5 import { sleep
} from
'./WorkerUtils';
7 export class WorkerStaticPool
extends WorkerAbstract
<WorkerData
> {
8 private readonly pool
: FixedThreadPool
<WorkerData
>;
11 * Creates a new `WorkerStaticPool`.
13 * @param workerScript -
14 * @param workerOptions -
16 constructor(workerScript
: string, workerOptions
?: WorkerOptions
) {
17 super(workerScript
, workerOptions
);
18 this.pool
= new FixedThreadPool(
19 this.workerOptions
.poolMaxSize
,
21 this.workerOptions
.poolOptions
,
25 get
info(): PoolInfo
{
26 return this.pool
.info
;
30 return this.pool
.info
.workerNodes
;
33 get
maxElementsPerWorker(): number | undefined {
37 get
emitter(): PoolEmitter
| undefined {
38 return this.pool
?.emitter
;
42 public async start(): Promise
<void> {
43 // This is intentional
47 public async stop(): Promise
<void> {
48 return this.pool
.destroy();
52 public async addElement(elementData
: WorkerData
): Promise
<void> {
53 await this.pool
.execute(elementData
);
54 // Start element sequentially to optimize memory at startup
55 this.workerOptions
.elementStartDelay
! > 0 &&
56 (await sleep(this.workerOptions
.elementStartDelay
!));