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; | |
eb87fe87 | 10 | private started: boolean; |
ded13d97 | 11 | private workerScript: string; |
adeb9b56 | 12 | private workerImplementationInstance: Wrk; |
ded13d97 JB |
13 | |
14 | private constructor() { | |
eb87fe87 | 15 | this.started = false; |
ded13d97 JB |
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> { | |
eb87fe87 | 27 | if (isMainThread && !this.started) { |
ded13d97 JB |
28 | try { |
29 | let numStationsTotal = 0; | |
adeb9b56 | 30 | await this.getWorkerImplementationInstance().start(); |
ded13d97 JB |
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 | }; | |
adeb9b56 | 41 | await this.getWorkerImplementationInstance().addElement(workerData); |
ded13d97 JB |
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 { | |
adeb9b56 | 55 | console.log(`Charging station simulator started with ${numStationsTotal.toString()} charging station(s) and ${Utils.workerDynamicPoolInUse() ? `${Configuration.getWorkerPoolMinSize().toString()}/` : ''}${this.getWorkerImplementationInstance().size}${Utils.workerPoolInUse() ? `/${Configuration.getWorkerPoolMaxSize().toString()}` : ''} worker(s) concurrently running in '${Configuration.getWorkerProcess()}' mode (${this.getWorkerImplementationInstance().maxElementsPerWorker} charging station(s) per worker)`); |
ded13d97 | 56 | } |
eb87fe87 | 57 | this.started = true; |
ded13d97 JB |
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> { | |
eb87fe87 | 66 | if (isMainThread && this.started) { |
adeb9b56 JB |
67 | await this.getWorkerImplementationInstance().stop(); |
68 | if (this.getWorkerImplementationInstance()) { | |
ded13d97 | 69 | // Nullify to force worker implementation instance creation |
adeb9b56 | 70 | this.workerImplementationInstance = null; |
ded13d97 JB |
71 | } |
72 | } | |
eb87fe87 | 73 | this.started = false; |
ded13d97 JB |
74 | } |
75 | ||
76 | public async restart(): Promise<void> { | |
77 | await this.stop(); | |
78 | await this.start(); | |
79 | } | |
80 | ||
adeb9b56 JB |
81 | private getWorkerImplementationInstance(): Wrk { |
82 | if (!this.workerImplementationInstance) { | |
83 | this.workerImplementationInstance = WorkerFactory.getWorkerImplementation<StationWorkerData>(this.workerScript, Configuration.getWorkerProcess(), { | |
ded13d97 JB |
84 | poolMaxSize: Configuration.getWorkerPoolMaxSize(), |
85 | poolMinSize: Configuration.getWorkerPoolMinSize(), | |
86 | elementsPerWorker: Configuration.getChargingStationsPerWorker() | |
87 | }); | |
88 | } | |
adeb9b56 | 89 | return this.workerImplementationInstance; |
ded13d97 JB |
90 | } |
91 | } |