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