1 import { DynamicThreadPool
, PoolOptions
} from
'poolifier';
3 import Utils from
'../utils/Utils';
4 import { Worker
} from
'worker_threads';
5 import WorkerAbstract from
'./WorkerAbstract';
6 import { WorkerData
} from
'../types/Worker';
7 import { WorkerUtils
} from
'./WorkerUtils';
9 export default class WorkerDynamicPool
<T
> extends WorkerAbstract
{
10 private pool
: DynamicThreadPool
<WorkerData
>;
13 * Create a new `WorkerDynamicPool`.
18 * @param workerStartDelay
20 * @param messageListenerCallback
22 constructor(workerScript
: string, min
: number, max
: number, workerStartDelay
?: number, opts
?: PoolOptions
<Worker
>,
23 messageListenerCallback
: (message
: any) => void = () => { /* This is intentional */ }) {
24 super(workerScript
, workerStartDelay
, messageListenerCallback
);
25 opts
.exitHandler
= opts
?.exitHandler
?? WorkerUtils
.defaultExitHandler
;
26 this.pool
= new DynamicThreadPool
<WorkerData
>(min
, max
, this.workerScript
, opts
);
30 return this.pool
.workers
.length
;
33 get
maxElementsPerWorker(): number | null {
42 // eslint-disable-next-line @typescript-eslint/no-empty-function
43 public async start(): Promise
<void> {
44 // This is intentional
52 // eslint-disable-next-line @typescript-eslint/require-await
53 public async stop(): Promise
<void> {
54 return this.pool
.destroy();
63 public async addElement(elementData
: T
): Promise
<void> {
64 await this.pool
.execute(elementData
);
65 // Start worker sequentially to optimize memory at startup
66 await Utils
.sleep(this.workerStartDelay
);