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