1 import { StationWorkerData
, WorkerEvents
, WorkerMessage
} from
'../types/Worker';
3 import Configuration from
'../utils/Configuration';
4 import { Storage
} from
'../utils/performance-storage/Storage';
5 import { StorageFactory
} from
'../utils/performance-storage/StorageFactory';
6 import Utils from
'../utils/Utils';
7 import WorkerAbstract from
'../worker/WorkerAbstract';
8 import WorkerFactory from
'../worker/WorkerFactory';
9 import chalk from
'chalk';
10 import { isMainThread
} from
'worker_threads';
11 import path from
'path';
12 import { version
} from
'../../package.json';
14 export default class Bootstrap
{
15 private static instance
: Bootstrap
| null = null;
16 private static workerImplementation
: WorkerAbstract
| null = null;
17 private static storage
: Storage
;
18 private version
: string = version
;
19 private started
: boolean;
20 private workerScript
: string;
22 private constructor() {
24 this.workerScript
= path
.join(path
.resolve(__dirname
, '../'), 'charging-station', 'StationWorker.js');
25 this.initWorkerImplementation();
26 Bootstrap
.storage
= StorageFactory
.getStorage(Configuration
.getPerformanceStorage().type, Configuration
.getPerformanceStorage().URI
, this.logPrefix());
27 Configuration
.setConfigurationChangeCallback(async () => Bootstrap
.getInstance().restart());
30 public static getInstance(): Bootstrap
{
31 if (!Bootstrap
.instance
) {
32 Bootstrap
.instance
= new Bootstrap();
34 return Bootstrap
.instance
;
37 public async start(): Promise
<void> {
38 if (isMainThread
&& !this.started
) {
40 let numStationsTotal
= 0;
41 await Bootstrap
.workerImplementation
.start();
42 // Start ChargingStation object in worker thread
43 if (Configuration
.getStationTemplateURLs()) {
44 for (const stationURL
of Configuration
.getStationTemplateURLs()) {
46 const nbStations
= stationURL
.numberOfStations
? stationURL
.numberOfStations
: 0;
47 for (let index
= 1; index
<= nbStations
; index
++) {
48 const workerData
: StationWorkerData
= {
50 templateFile
: path
.join(path
.resolve(__dirname
, '../'), 'assets', 'station-templates', path
.basename(stationURL
.file
))
52 await Bootstrap
.workerImplementation
.addElement(workerData
);
56 console
.error(chalk
.red('Charging station start with template file ' + stationURL
.file
+ ' error '), error
);
60 console
.warn(chalk
.yellow('No stationTemplateURLs defined in configuration, exiting'));
62 if (numStationsTotal
=== 0) {
63 console
.warn(chalk
.yellow('No charging station template enabled in configuration, exiting'));
65 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)` : ''}`));
69 console.error(chalk.red('Bootstrap start error '), error);
74 public async stop(): Promise<void> {
75 if (isMainThread && this.started) {
76 await Bootstrap.workerImplementation.stop();
81 public async restart(): Promise<void> {
83 this.initWorkerImplementation();
87 private initWorkerImplementation() {
88 Bootstrap.workerImplementation = WorkerFactory.getWorkerImplementation<StationWorkerData>(this.workerScript, Configuration.getWorkerProcess(),
90 startDelay: Configuration.getWorkerStartDelay(),
91 poolMaxSize: Configuration.getWorkerPoolMaxSize(),
92 poolMinSize: Configuration.getWorkerPoolMinSize(),
93 elementsPerWorker: Configuration.getChargingStationsPerWorker(),
95 workerChoiceStrategy: Configuration.getWorkerPoolStrategy()
97 }, (msg: WorkerMessage) => {
98 if (msg.id === WorkerEvents.PERFORMANCE_STATISTICS) {
99 Bootstrap.storage.storePerformanceStatistics(msg.data);
102 if (!Bootstrap.workerImplementation) {
103 throw new Error('Worker implementation not found');
107 private logPrefix(): string {
108 return Utils.logPrefix(' Bootstrap');