Commit | Line | Data |
---|---|---|
ded13d97 JB |
1 | import Configuration from '../utils/Configuration'; |
2 | import { StationWorkerData } from '../types/Worker'; | |
3 | import Utils from '../utils/Utils'; | |
4 | import WorkerFactory from '../worker/WorkerFactory'; | |
5 | import Wrk from '../worker/Wrk'; | |
6 | import { isMainThread } from 'worker_threads'; | |
7 | ||
8 | export default class Bootstrap { | |
9 | private static instance: Bootstrap; | |
10 | private isStarted: boolean; | |
11 | private workerScript: string; | |
12 | private workerImplementation: Wrk; | |
13 | ||
14 | private constructor() { | |
15 | this.isStarted = false; | |
16 | this.workerScript = './dist/charging-station/StationWorker.js'; | |
17 | } | |
18 | ||
19 | public static getInstance(): Bootstrap { | |
20 | if (!Bootstrap.instance) { | |
21 | Bootstrap.instance = new Bootstrap(); | |
22 | } | |
23 | return Bootstrap.instance; | |
24 | } | |
25 | ||
26 | public async start(): Promise<void> { | |
27 | if (isMainThread && !this.isStarted) { | |
28 | try { | |
29 | let numStationsTotal = 0; | |
30 | await this.getWorkerImplementation().start(); | |
31 | // Start ChargingStation object in worker thread | |
32 | if (Configuration.getStationTemplateURLs()) { | |
33 | for (const stationURL of Configuration.getStationTemplateURLs()) { | |
34 | try { | |
35 | const nbStations = stationURL.numberOfStations ? stationURL.numberOfStations : 0; | |
36 | for (let index = 1; index <= nbStations; index++) { | |
37 | const workerData: StationWorkerData = { | |
38 | index, | |
39 | templateFile: stationURL.file | |
40 | }; | |
41 | await this.getWorkerImplementation().addElement(workerData); | |
42 | numStationsTotal++; | |
43 | } | |
44 | } catch (error) { | |
45 | // eslint-disable-next-line no-console | |
46 | console.error('Charging station start with template file ' + stationURL.file + ' error ', error); | |
47 | } | |
48 | } | |
49 | } else { | |
50 | console.log('No stationTemplateURLs defined in configuration, exiting'); | |
51 | } | |
52 | if (numStationsTotal === 0) { | |
53 | console.log('No charging station template enabled in configuration, exiting'); | |
54 | } else { | |
55 | console.log(`Charging station simulator started with ${numStationsTotal.toString()} charging station(s) and ${Utils.workerDynamicPoolInUse() ? `${Configuration.getWorkerPoolMinSize().toString()}/` : ''}${this.getWorkerImplementation().size}${Utils.workerPoolInUse() ? `/${Configuration.getWorkerPoolMaxSize().toString()}` : ''} worker(s) concurrently running in '${Configuration.getWorkerProcess()}' mode (${this.getWorkerImplementation().maxElementsPerWorker} charging station(s) per worker)`); | |
56 | } | |
57 | this.isStarted = true; | |
58 | } catch (error) { | |
59 | // eslint-disable-next-line no-console | |
60 | console.error('Bootstrap start error ', error); | |
61 | } | |
62 | } | |
63 | } | |
64 | ||
65 | public async stop(): Promise<void> { | |
66 | if (isMainThread && this.isStarted) { | |
67 | await this.getWorkerImplementation().stop(); | |
68 | if (this.workerImplementation) { | |
69 | // Nullify to force worker implementation instance creation | |
70 | this.workerImplementation = null; | |
71 | } | |
72 | } | |
73 | this.isStarted = false; | |
74 | } | |
75 | ||
76 | public async restart(): Promise<void> { | |
77 | await this.stop(); | |
78 | await this.start(); | |
79 | } | |
80 | ||
81 | private getWorkerImplementation(): Wrk { | |
82 | if (!this.workerImplementation) { | |
83 | this.workerImplementation = WorkerFactory.getWorkerImpl<StationWorkerData>(this.workerScript, Configuration.getWorkerProcess(), { | |
84 | poolMaxSize: Configuration.getWorkerPoolMaxSize(), | |
85 | poolMinSize: Configuration.getWorkerPoolMinSize(), | |
86 | elementsPerWorker: Configuration.getChargingStationsPerWorker() | |
87 | }); | |
88 | } | |
89 | return this.workerImplementation; | |
90 | } | |
91 | } |