Commit | Line | Data |
---|---|---|
b4d34251 JB |
1 | // Partial Copyright Jerome Benoit. 2021. All Rights Reserved. |
2 | ||
2a370053 | 3 | import { ChargingStationWorkerData, WorkerMessage, WorkerMessageEvents } from '../types/Worker'; |
81797102 | 4 | |
ded13d97 | 5 | import Configuration from '../utils/Configuration'; |
a6b3c6c3 JB |
6 | import { Storage } from '../performance/storage/Storage'; |
7 | import { StorageFactory } from '../performance/storage/StorageFactory'; | |
ded13d97 | 8 | import Utils from '../utils/Utils'; |
fd1fdf1b | 9 | import WorkerAbstract from '../worker/WorkerAbstract'; |
ded13d97 | 10 | import WorkerFactory from '../worker/WorkerFactory'; |
8eac9a09 | 11 | import chalk from 'chalk'; |
ded13d97 | 12 | import { isMainThread } from 'worker_threads'; |
bf1866b2 | 13 | import path from 'path'; |
84e52c2c | 14 | import { version } from '../../package.json'; |
ded13d97 JB |
15 | |
16 | export default class Bootstrap { | |
535aaa27 JB |
17 | private static instance: Bootstrap | null = null; |
18 | private static workerImplementation: WorkerAbstract | null = null; | |
81797102 | 19 | private static storage: Storage; |
aef1b33a | 20 | private version: string = version; |
eb87fe87 | 21 | private started: boolean; |
ded13d97 | 22 | private workerScript: string; |
ded13d97 JB |
23 | |
24 | private constructor() { | |
eb87fe87 | 25 | this.started = false; |
07f35004 | 26 | this.workerScript = path.join(path.resolve(__dirname, '../'), 'charging-station', 'ChargingStationWorker.js'); |
8df3f0a9 | 27 | this.initWorkerImplementation(); |
81797102 | 28 | Bootstrap.storage = StorageFactory.getStorage(Configuration.getPerformanceStorage().type, Configuration.getPerformanceStorage().URI, this.logPrefix()); |
7874b0b1 | 29 | Configuration.setConfigurationChangeCallback(async () => Bootstrap.getInstance().restart()); |
ded13d97 JB |
30 | } |
31 | ||
32 | public static getInstance(): Bootstrap { | |
33 | if (!Bootstrap.instance) { | |
34 | Bootstrap.instance = new Bootstrap(); | |
35 | } | |
36 | return Bootstrap.instance; | |
37 | } | |
38 | ||
39 | public async start(): Promise<void> { | |
eb87fe87 | 40 | if (isMainThread && !this.started) { |
ded13d97 JB |
41 | try { |
42 | let numStationsTotal = 0; | |
2a370053 | 43 | await Bootstrap.storage.open(); |
535aaa27 | 44 | await Bootstrap.workerImplementation.start(); |
ded13d97 JB |
45 | // Start ChargingStation object in worker thread |
46 | if (Configuration.getStationTemplateURLs()) { | |
47 | for (const stationURL of Configuration.getStationTemplateURLs()) { | |
48 | try { | |
49 | const nbStations = stationURL.numberOfStations ? stationURL.numberOfStations : 0; | |
50 | for (let index = 1; index <= nbStations; index++) { | |
07f35004 | 51 | const workerData: ChargingStationWorkerData = { |
ded13d97 | 52 | index, |
bf1866b2 | 53 | templateFile: path.join(path.resolve(__dirname, '../'), 'assets', 'station-templates', path.basename(stationURL.file)) |
ded13d97 | 54 | }; |
535aaa27 | 55 | await Bootstrap.workerImplementation.addElement(workerData); |
ded13d97 JB |
56 | numStationsTotal++; |
57 | } | |
58 | } catch (error) { | |
8eac9a09 | 59 | console.error(chalk.red('Charging station start with template file ' + stationURL.file + ' error '), error); |
ded13d97 JB |
60 | } |
61 | } | |
62 | } else { | |
8eac9a09 | 63 | console.warn(chalk.yellow('No stationTemplateURLs defined in configuration, exiting')); |
ded13d97 JB |
64 | } |
65 | if (numStationsTotal === 0) { | |
8eac9a09 | 66 | console.warn(chalk.yellow('No charging station template enabled in configuration, exiting')); |
ded13d97 | 67 | } else { |
7429b96a | 68 | console.log(chalk.green(`Charging stations simulator ${this.version} started with ${numStationsTotal.toString()} charging station(s) and ${Utils.workerDynamicPoolInUse() ? `${Configuration.getWorkerPoolMinSize().toString()}/` : ''}${Bootstrap.workerImplementation.size}${Utils.workerPoolInUse() ? `/${Configuration.getWorkerPoolMaxSize().toString()}` : ''} worker(s) concurrently running in '${Configuration.getWorkerProcess()}' mode${Bootstrap.workerImplementation.maxElementsPerWorker ? ` (${Bootstrap.workerImplementation.maxElementsPerWorker} charging station(s) per worker)` : ''}`)); |
ded13d97 | 69 | } |
eb87fe87 | 70 | this.started = true; |
ded13d97 | 71 | } catch (error) { |
8eac9a09 | 72 | console.error(chalk.red('Bootstrap start error '), error); |
ded13d97 JB |
73 | } |
74 | } | |
75 | } | |
76 | ||
77 | public async stop(): Promise<void> { | |
eb87fe87 | 78 | if (isMainThread && this.started) { |
535aaa27 | 79 | await Bootstrap.workerImplementation.stop(); |
2a370053 | 80 | await Bootstrap.storage.close(); |
ded13d97 | 81 | } |
eb87fe87 | 82 | this.started = false; |
ded13d97 JB |
83 | } |
84 | ||
85 | public async restart(): Promise<void> { | |
86 | await this.stop(); | |
535aaa27 | 87 | this.initWorkerImplementation(); |
ded13d97 JB |
88 | await this.start(); |
89 | } | |
90 | ||
2a370053 | 91 | private initWorkerImplementation(): void { |
07f35004 | 92 | Bootstrap.workerImplementation = WorkerFactory.getWorkerImplementation<ChargingStationWorkerData>(this.workerScript, Configuration.getWorkerProcess(), |
8df3f0a9 JB |
93 | { |
94 | startDelay: Configuration.getWorkerStartDelay(), | |
95 | poolMaxSize: Configuration.getWorkerPoolMaxSize(), | |
96 | poolMinSize: Configuration.getWorkerPoolMinSize(), | |
97 | elementsPerWorker: Configuration.getChargingStationsPerWorker(), | |
98 | poolOptions: { | |
99 | workerChoiceStrategy: Configuration.getWorkerPoolStrategy() | |
ffd71f2c JB |
100 | }, |
101 | messageHandler: async (msg: WorkerMessage) => { | |
102 | if (msg.id === WorkerMessageEvents.PERFORMANCE_STATISTICS) { | |
103 | await Bootstrap.storage.storePerformanceStatistics(msg.data); | |
104 | } | |
81797102 | 105 | } |
535aaa27 | 106 | }); |
ded13d97 | 107 | } |
81797102 JB |
108 | |
109 | private logPrefix(): string { | |
689dca78 | 110 | return Utils.logPrefix(' Bootstrap |'); |
81797102 | 111 | } |
ded13d97 | 112 | } |