build(deps-dev): apply updates
[e-mobility-charging-stations-simulator.git] / src / worker / WorkerStaticPool.ts
CommitLineData
d972af76 1import { FixedThreadPool, type PoolEmitter, type PoolInfo } from 'poolifier';
8114d10e 2
268a74bb
JB
3import { WorkerAbstract } from './WorkerAbstract';
4import type { WorkerData, WorkerOptions } from './WorkerTypes';
be245fda 5import { sleep } from './WorkerUtils';
a4624c96 6
268a74bb 7export class WorkerStaticPool extends WorkerAbstract<WorkerData> {
f2bf9948 8 private readonly pool: FixedThreadPool<WorkerData>;
a4624c96
JB
9
10 /**
361c98f5 11 * Creates a new `WorkerStaticPool`.
a4624c96 12 *
0e4fa348
JB
13 * @param workerScript -
14 * @param workerOptions -
a4624c96 15 */
4d7227e6
JB
16 constructor(workerScript: string, workerOptions?: WorkerOptions) {
17 super(workerScript, workerOptions);
e7aeea18
JB
18 this.pool = new FixedThreadPool(
19 this.workerOptions.poolMaxSize,
20 this.workerScript,
5edd8ba0 21 this.workerOptions.poolOptions,
e7aeea18 22 );
a4624c96
JB
23 }
24
b779c0f8
JB
25 get info(): PoolInfo {
26 return this.pool.info;
27 }
28
a4624c96 29 get size(): number {
b779c0f8 30 return this.pool.info.workerNodes;
a4624c96
JB
31 }
32
72092cfc
JB
33 get maxElementsPerWorker(): number | undefined {
34 return undefined;
a4624c96
JB
35 }
36
d972af76 37 get emitter(): PoolEmitter | undefined {
962a8159
JB
38 return this.pool?.emitter;
39 }
40
8baf3f8f 41 /** @inheritDoc */
81797102
JB
42 public async start(): Promise<void> {
43 // This is intentional
44 }
a4624c96 45
8baf3f8f 46 /** @inheritDoc */
ded13d97
JB
47 public async stop(): Promise<void> {
48 return this.pool.destroy();
49 }
50
8baf3f8f 51 /** @inheritDoc */
c3ee95af 52 public async addElement(elementData: WorkerData): Promise<void> {
a4624c96 53 await this.pool.execute(elementData);
4bfd80fa 54 // Start element sequentially to optimize memory at startup
e1d9a0f4
JB
55 this.workerOptions.elementStartDelay! > 0 &&
56 (await sleep(this.workerOptions.elementStartDelay!));
a4624c96
JB
57 }
58}