1 // Partial Copyright Jerome Benoit. 2021. All Rights Reserved.
3 import { ChargingStationWorkerData
, WorkerMessage
, WorkerMessageEvents
} from
'../types/Worker';
5 import Configuration from
'../utils/Configuration';
6 import { Storage
} from
'../performance/storage/Storage';
7 import { StorageFactory
} from
'../performance/storage/StorageFactory';
8 import Utils from
'../utils/Utils';
9 import WorkerAbstract from
'../worker/WorkerAbstract';
10 import WorkerFactory from
'../worker/WorkerFactory';
11 import chalk from
'chalk';
12 import { isMainThread
} from
'worker_threads';
13 import path from
'path';
14 import { version
} from
'../../package.json';
16 export default class Bootstrap
{
17 private static instance
: Bootstrap
| null = null;
18 private static workerImplementation
: WorkerAbstract
| null = null;
19 private static storage
: Storage
;
20 private version
: string = version
;
21 private started
: boolean;
22 private workerScript
: string;
24 private constructor() {
26 this.workerScript
= path
.join(path
.resolve(__dirname
, '../'), 'charging-station', 'ChargingStationWorker.js');
27 this.initWorkerImplementation();
28 Bootstrap
.storage
= StorageFactory
.getStorage(Configuration
.getPerformanceStorage().type, Configuration
.getPerformanceStorage().URI
, this.logPrefix());
29 Configuration
.setConfigurationChangeCallback(async () => Bootstrap
.getInstance().restart());
32 public static getInstance(): Bootstrap
{
33 if (!Bootstrap
.instance
) {
34 Bootstrap
.instance
= new Bootstrap();
36 return Bootstrap
.instance
;
39 public async start(): Promise
<void> {
40 if (isMainThread
&& !this.started
) {
42 let numStationsTotal
= 0;
43 await Bootstrap
.storage
.open();
44 await Bootstrap
.workerImplementation
.start();
45 // Start ChargingStation object in worker thread
46 if (Configuration
.getStationTemplateURLs()) {
47 for (const stationURL
of Configuration
.getStationTemplateURLs()) {
49 const nbStations
= stationURL
.numberOfStations
? stationURL
.numberOfStations
: 0;
50 for (let index
= 1; index
<= nbStations
; index
++) {
51 const workerData
: ChargingStationWorkerData
= {
53 templateFile
: path
.join(path
.resolve(__dirname
, '../'), 'assets', 'station-templates', path
.basename(stationURL
.file
))
55 await Bootstrap
.workerImplementation
.addElement(workerData
);
59 console
.error(chalk
.red('Charging station start with template file ' + stationURL
.file
+ ' error '), error
);
63 console
.warn(chalk
.yellow('No stationTemplateURLs defined in configuration, exiting'));
65 if (numStationsTotal
=== 0) {
66 console
.warn(chalk
.yellow('No charging station template enabled in configuration, exiting'));
68 console
.log(chalk
.green(`Charging station 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)` : ''}`));
72 console.error(chalk.red('Bootstrap start error '), error);
77 public async stop(): Promise<void> {
78 if (isMainThread && this.started) {
79 await Bootstrap.workerImplementation.stop();
80 await Bootstrap.storage.close();
85 public async restart(): Promise<void> {
87 this.initWorkerImplementation();
91 private initWorkerImplementation(): void {
92 Bootstrap.workerImplementation = WorkerFactory.getWorkerImplementation<ChargingStationWorkerData>(this.workerScript, Configuration.getWorkerProcess(),
94 startDelay: Configuration.getWorkerStartDelay(),
95 poolMaxSize: Configuration.getWorkerPoolMaxSize(),
96 poolMinSize: Configuration.getWorkerPoolMinSize(),
97 elementsPerWorker: Configuration.getChargingStationsPerWorker(),
99 workerChoiceStrategy: Configuration.getWorkerPoolStrategy()
101 messageHandler: async (msg: WorkerMessage) => {
102 if (msg.id === WorkerMessageEvents.PERFORMANCE_STATISTICS) {
103 await Bootstrap.storage.storePerformanceStatistics(msg.data);
109 private logPrefix(): string {
110 return Utils.logPrefix(' Bootstrap |');