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