Storage: use worker threads message passing to store performance records from
[e-mobility-charging-stations-simulator.git] / src / worker / WorkerDynamicPool.ts
1 import { DynamicThreadPool, PoolOptions } from 'poolifier';
2
3 import Utils from '../utils/Utils';
4 import { Worker } from 'worker_threads';
5 import WorkerAbstract from './WorkerAbstract';
6 import { WorkerData } from '../types/Worker';
7 import { WorkerUtils } from './WorkerUtils';
8
9 export default class WorkerDynamicPool<T> extends WorkerAbstract {
10 private pool: DynamicThreadPool<WorkerData>;
11
12 /**
13 * Create a new `WorkerDynamicPool`.
14 *
15 * @param workerScript
16 * @param min
17 * @param max
18 * @param workerStartDelay
19 * @param opts
20 * @param messageListenerCallback
21 */
22 constructor(workerScript: string, min: number, max: number, workerStartDelay?: number, opts?: PoolOptions<Worker>,
23 messageListenerCallback: (message: any) => void = () => { /* This is intentional */ }) {
24 super(workerScript, workerStartDelay, messageListenerCallback);
25 opts.exitHandler = opts?.exitHandler ?? WorkerUtils.defaultExitHandler;
26 this.pool = new DynamicThreadPool<WorkerData>(min, max, this.workerScript, opts);
27 }
28
29 get size(): number {
30 return this.pool.workers.length;
31 }
32
33 get maxElementsPerWorker(): number | null {
34 return null;
35 }
36
37 /**
38 *
39 * @returns
40 * @public
41 */
42 // eslint-disable-next-line @typescript-eslint/no-empty-function
43 public async start(): Promise<void> {
44 // This is intentional
45 }
46
47 /**
48 *
49 * @returns
50 * @public
51 */
52 // eslint-disable-next-line @typescript-eslint/require-await
53 public async stop(): Promise<void> {
54 return this.pool.destroy();
55 }
56
57 /**
58 *
59 * @param elementData
60 * @returns
61 * @public
62 */
63 public async addElement(elementData: T): Promise<void> {
64 await this.pool.execute(elementData);
65 // Start worker sequentially to optimize memory at startup
66 await Utils.sleep(this.workerStartDelay);
67 }
68 }