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