e27e803b40b9f338d8997bb484705a3a57649208
[e-mobility-charging-stations-simulator.git] / src / worker / WorkerDynamicPool.ts
1 import type { EventEmitterAsyncResource } from 'node:events';
2
3 import { DynamicThreadPool, type PoolInfo } from 'poolifier';
4
5 import { WorkerAbstract } from './WorkerAbstract.js';
6 import type { WorkerData, WorkerOptions } from './WorkerTypes.js';
7 import { randomizeDelay, sleep } from './WorkerUtils.js';
8
9 export class WorkerDynamicPool extends WorkerAbstract<WorkerData> {
10 private readonly pool: DynamicThreadPool<WorkerData>;
11
12 /**
13 * Creates a new `WorkerDynamicPool`.
14 *
15 * @param workerScript -
16 * @param workerOptions -
17 */
18 constructor(workerScript: string, workerOptions: WorkerOptions) {
19 super(workerScript, workerOptions);
20 this.pool = new DynamicThreadPool<WorkerData>(
21 this.workerOptions.poolMinSize,
22 this.workerOptions.poolMaxSize,
23 this.workerScript,
24 this.workerOptions.poolOptions,
25 );
26 }
27
28 get info(): PoolInfo {
29 return this.pool.info;
30 }
31
32 get size(): number {
33 return this.pool.info.workerNodes;
34 }
35
36 get maxElementsPerWorker(): number | undefined {
37 return undefined;
38 }
39
40 get emitter(): EventEmitterAsyncResource | undefined {
41 return this.pool?.emitter;
42 }
43
44 /** @inheritDoc */
45 public async start(): Promise<void> {
46 // This is intentional
47 }
48
49 /** @inheritDoc */
50 public async stop(): Promise<void> {
51 return this.pool.destroy();
52 }
53
54 /** @inheritDoc */
55 public async addElement(elementData: WorkerData): Promise<void> {
56 await this.pool.execute(elementData);
57 // Start element sequentially to optimize memory at startup
58 this.workerOptions.elementStartDelay! > 0 &&
59 (await sleep(randomizeDelay(this.workerOptions.elementStartDelay!)));
60 }
61 }