import { DynamicThreadPool, PoolOptions } from 'poolifier';
-import Constants from '../utils/Constants';
import Utils from '../utils/Utils';
import { Worker } from 'worker_threads';
import WorkerAbstract from './WorkerAbstract';
* Create a new `WorkerDynamicPool`.
*
* @param {string} workerScript
- * @param min
- * @param max
+ * @param {number} min
+ * @param {number} max
+ * @param {number} workerStartDelay
+ * @param {PoolOptions} opts
*/
- constructor(workerScript: string, min: number, max: number,) {
- super(workerScript);
- this.pool = DynamicPool.getInstance(min, max, this.workerScript);
+ constructor(workerScript: string, min: number, max: number, workerStartDelay?: number, opts?: PoolOptions<Worker>) {
+ super(workerScript, workerStartDelay);
+ this.pool = DynamicPool.getInstance(min, max, this.workerScript, opts);
}
get size(): number {
return this.pool.workers.length;
}
- get maxElementsPerWorker(): number {
+ get maxElementsPerWorker(): number | null {
return null;
}
/**
*
- * @param elementData
+ * @param {T} elementData
* @returns {Promise<void>}
* @public
*/
public async addElement(elementData: T): Promise<void> {
await this.pool.execute(elementData);
// Start worker sequentially to optimize memory at startup
- await Utils.sleep(Constants.START_WORKER_DELAY);
+ await Utils.sleep(this.workerStartDelay);
}
}
super(min, max, workerScript, opts);
}
- public static getInstance(min: number, max: number, workerScript: string): DynamicPool {
+ public static getInstance(min: number, max: number, workerScript: string, opts?: PoolOptions<Worker>): DynamicPool {
if (!DynamicPool.instance) {
- DynamicPool.instance = new DynamicPool(min, max, workerScript,
- {
- exitHandler: (code) => {
- if (code !== 0) {
- console.error(`Worker stopped with exit code ${code}`);
- }
- }
+ opts.exitHandler = opts?.exitHandler ?? ((code) => {
+ if (code !== 0) {
+ console.error(`Worker stopped with exit code ${code}`);
}
- );
+ });
+ DynamicPool.instance = new DynamicPool(min, max, workerScript, opts);
}
return DynamicPool.instance;
}