3eaffff4b48a7c11e4c6c5fa3026b4c9f042b551
1 import { WorkerData
, WorkerOptions
} from
'../types/Worker';
3 import { FixedThreadPool
} from
'poolifier';
4 import Utils from
'../utils/Utils';
5 import WorkerAbstract from
'./WorkerAbstract';
6 import { WorkerUtils
} from
'./WorkerUtils';
8 export default class WorkerStaticPool
extends WorkerAbstract
<WorkerData
> {
9 private readonly pool
: FixedThreadPool
<WorkerData
>;
12 * Create a new `WorkerStaticPool`.
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 FixedThreadPool(
22 this.workerOptions
.poolMaxSize
,
24 this.workerOptions
.poolOptions
29 return this.pool
.workers
.length
;
32 get
maxElementsPerWorker(): number | null {
41 public async start(): Promise
<void> {
42 // This is intentional
50 public async stop(): Promise
<void> {
51 return this.pool
.destroy();
60 public async addElement(elementData
: WorkerData
): Promise
<void> {
61 await this.pool
.execute(elementData
);
62 // Start element sequentially to optimize memory at startup
63 this.workerOptions
.elementStartDelay
> 0 &&
64 (await Utils
.sleep(this.workerOptions
.elementStartDelay
));