dfa344231edf56e019d0a1192b720aa493eec018
1 import { WorkerData
, WorkerOptions
} from
'../types/Worker';
3 import { DynamicThreadPool
} from
'poolifier';
4 import Utils from
'../utils/Utils';
5 import WorkerAbstract from
'./WorkerAbstract';
6 import { WorkerUtils
} from
'./WorkerUtils';
8 export default class WorkerDynamicPool
extends WorkerAbstract
<WorkerData
> {
9 private readonly pool
: DynamicThreadPool
<WorkerData
>;
12 * Create a new `WorkerDynamicPool`.
15 * @param workerOptions
17 constructor(workerScript
: string, workerOptions
?: WorkerOptions
) {
18 super(workerScript
, workerOptions
);
19 this.workerOptions
.poolOptions
.exitHandler
=
20 this.workerOptions
?.poolOptions
?.exitHandler
?? WorkerUtils
.defaultExitHandler
;
21 this.pool
= new DynamicThreadPool
<WorkerData
>(
22 this.workerOptions
.poolMinSize
,
23 this.workerOptions
.poolMaxSize
,
25 this.workerOptions
.poolOptions
30 return this.pool
.workers
.length
;
33 get
maxElementsPerWorker(): number | null {
42 public async start(): Promise
<void> {
43 // This is intentional
51 public async stop(): Promise
<void> {
52 return this.pool
.destroy();
61 public async addElement(elementData
: WorkerData
): Promise
<void> {
62 await this.pool
.execute(elementData
);
63 // Start element sequentially to optimize memory at startup
64 this.workerOptions
.elementStartDelay
> 0 &&
65 (await Utils
.sleep(this.workerOptions
.elementStartDelay
));