Add configuration tunable for pool worker choice strategy
[e-mobility-charging-stations-simulator.git] / src / worker / WorkerDynamicPool.ts
CommitLineData
c32882b0 1import { DynamicThreadPool, PoolOptions } from 'poolifier';
a4624c96 2
a4624c96 3import Utils from '../utils/Utils';
c32882b0 4import { Worker } from 'worker_threads';
fd1fdf1b 5import WorkerAbstract from './WorkerAbstract';
a4624c96 6import { WorkerData } from '../types/Worker';
a4624c96 7
fd1fdf1b 8export default class WorkerDynamicPool<T> extends WorkerAbstract {
a4624c96
JB
9 private pool: DynamicPool;
10
11 /**
12 * Create a new `WorkerDynamicPool`.
13 *
14 * @param {string} workerScript
e71cccf3
JB
15 * @param {number} min
16 * @param {number} max
322c9192 17 * @param {number} workerStartDelay
9efbac5b 18 * @param {PoolOptions} opts
a4624c96 19 */
9efbac5b 20 constructor(workerScript: string, min: number, max: number, workerStartDelay?: number, opts?: PoolOptions<Worker>) {
322c9192 21 super(workerScript, workerStartDelay);
9efbac5b 22 this.pool = DynamicPool.getInstance(min, max, this.workerScript, opts);
a4624c96
JB
23 }
24
25 get size(): number {
26 return this.pool.workers.length;
27 }
28
29 get maxElementsPerWorker(): number {
85f78bc0 30 return null;
a4624c96
JB
31 }
32
33 /**
34 *
3340259a 35 * @returns {Promise<void>}
a4624c96
JB
36 * @public
37 */
38 // eslint-disable-next-line @typescript-eslint/no-empty-function
39 public async start(): Promise<void> { }
40
ded13d97
JB
41 /**
42 *
3340259a 43 * @returns {Promise<void>}
ded13d97
JB
44 * @public
45 */
fd1fdf1b 46 // eslint-disable-next-line @typescript-eslint/require-await
ded13d97
JB
47 public async stop(): Promise<void> {
48 return this.pool.destroy();
49 }
50
a4624c96
JB
51 /**
52 *
3340259a
JB
53 * @param elementData
54 * @returns {Promise<void>}
a4624c96
JB
55 * @public
56 */
8434025b 57 public async addElement(elementData: T): Promise<void> {
a4624c96
JB
58 await this.pool.execute(elementData);
59 // Start worker sequentially to optimize memory at startup
322c9192 60 await Utils.sleep(this.workerStartDelay);
a4624c96
JB
61 }
62}
63
64class DynamicPool extends DynamicThreadPool<WorkerData> {
65 private static instance: DynamicPool;
66
63b19acd
JB
67 private constructor(min: number, max: number, workerScript: string, opts?: PoolOptions<Worker>) {
68 super(min, max, workerScript, opts);
a4624c96
JB
69 }
70
9efbac5b 71 public static getInstance(min: number, max: number, workerScript: string, opts?: PoolOptions<Worker>): DynamicPool {
a4624c96 72 if (!DynamicPool.instance) {
9efbac5b
JB
73 opts.exitHandler = opts.exitHandler ?? ((code) => {
74 if (code !== 0) {
75 console.error(`Worker stopped with exit code ${code}`);
a4624c96 76 }
9efbac5b
JB
77 });
78 DynamicPool.instance = new DynamicPool(min, max, workerScript, opts);
a4624c96
JB
79 }
80 return DynamicPool.instance;
81 }
82}