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
: DynamicPool
;
13 * Create a new `WorkerDynamicPool`.
15 * @param {string} workerScript
18 * @param {number} workerStartDelay
19 * @param {PoolOptions} opts
21 constructor(workerScript
: string, min
: number, max
: number, workerStartDelay
?: number, opts
?: PoolOptions
<Worker
>) {
22 super(workerScript
, workerStartDelay
);
23 this.pool
= DynamicPool
.getInstance(min
, max
, this.workerScript
, opts
);
27 return this.pool
.workers
.length
;
30 get
maxElementsPerWorker(): number | null {
36 * @returns {Promise<void>}
39 // eslint-disable-next-line @typescript-eslint/no-empty-function
40 public async start(): Promise
<void> { }
44 * @returns {Promise<void>}
47 // eslint-disable-next-line @typescript-eslint/require-await
48 public async stop(): Promise
<void> {
49 return this.pool
.destroy();
54 * @param {T} elementData
55 * @returns {Promise<void>}
58 public async addElement(elementData
: T
): Promise
<void> {
59 await this.pool
.execute(elementData
);
60 // Start worker sequentially to optimize memory at startup
61 await Utils
.sleep(this.workerStartDelay
);
65 class DynamicPool
extends DynamicThreadPool
<WorkerData
> {
66 private static instance
: DynamicPool
;
68 private constructor(min
: number, max
: number, workerScript
: string, opts
?: PoolOptions
<Worker
>) {
69 super(min
, max
, workerScript
, opts
);
72 public static getInstance(min
: number, max
: number, workerScript
: string, opts
?: PoolOptions
<Worker
>): DynamicPool
{
73 if (!DynamicPool
.instance
) {
74 opts
.exitHandler
= opts
?.exitHandler
?? WorkerUtils
.defaultExitHandler
;
75 DynamicPool
.instance
= new DynamicPool(min
, max
, workerScript
, opts
);
77 return DynamicPool
.instance
;