1 import Configuration from
'../utils/Configuration';
2 import { StationWorkerData
} from
'../types/Worker';
3 import Utils from
'../utils/Utils';
4 import WorkerAbstract from
'../worker/WorkerAbstract';
5 import WorkerFactory from
'../worker/WorkerFactory';
6 import { isMainThread
} from
'worker_threads';
7 import path from
'path';
8 import { version
} from
'../../package.json';
10 export default class Bootstrap
{
11 private static instance
: Bootstrap
;
12 private version
: string = version
;
13 private started
: boolean;
14 private workerScript
: string;
15 private workerImplementationInstance
: WorkerAbstract
| null = null;
17 private constructor() {
19 this.workerScript
= path
.join(path
.resolve(__dirname
, '../'), 'charging-station', 'StationWorker.js');
20 Configuration
.setConfigurationChangeCallback(async () => await Bootstrap
.getInstance().restart());
23 public static getInstance(): Bootstrap
{
24 if (!Bootstrap
.instance
) {
25 Bootstrap
.instance
= new Bootstrap();
27 return Bootstrap
.instance
;
30 public async start(): Promise
<void> {
31 if (isMainThread
&& !this.started
) {
33 let numStationsTotal
= 0;
34 await this.getWorkerImplementationInstance()?.start();
35 // Start ChargingStation object in worker thread
36 if (Configuration
.getStationTemplateURLs()) {
37 for (const stationURL
of Configuration
.getStationTemplateURLs()) {
39 const nbStations
= stationURL
.numberOfStations
? stationURL
.numberOfStations
: 0;
40 for (let index
= 1; index
<= nbStations
; index
++) {
41 const workerData
: StationWorkerData
= {
43 templateFile
: path
.join(path
.resolve(__dirname
, '../'), 'assets', 'station-templates', path
.basename(stationURL
.file
))
45 await this.getWorkerImplementationInstance()?.addElement(workerData
);
49 console
.error('Charging station start with template file ' + stationURL
.file
+ ' error ', error
);
53 console
.log('No stationTemplateURLs defined in configuration, exiting');
55 if (numStationsTotal
=== 0) {
56 console
.log('No charging station template enabled in configuration, exiting');
58 console
.log(`Charging station simulator ${this.version} started with ${numStationsTotal.toString()} charging station(s) and ${Utils.workerDynamicPoolInUse() ? `${Configuration.getWorkerPoolMinSize().toString()}/` : ''}${this.getWorkerImplementationInstance().size}${Utils.workerPoolInUse() ? `/${Configuration.getWorkerPoolMaxSize().toString()}
` : ''} worker(s) concurrently running in '${Configuration.getWorkerProcess()}' mode${this.getWorkerImplementationInstance().maxElementsPerWorker ? ` (${this.getWorkerImplementationInstance().maxElementsPerWorker} charging station(s) per worker)` : ''}`);
62 console.error('Bootstrap start error ', error);
67 public async stop(): Promise<void> {
68 if (isMainThread && this.started) {
69 await this.getWorkerImplementationInstance()?.stop();
70 // Nullify to force worker implementation instance creation
71 this.workerImplementationInstance = null;
76 public async restart(): Promise<void> {
81 private getWorkerImplementationInstance(): WorkerAbstract | null {
82 if (!this.workerImplementationInstance) {
83 this.workerImplementationInstance = WorkerFactory.getWorkerImplementation<StationWorkerData>(this.workerScript, Configuration.getWorkerProcess(),
85 startDelay: Configuration.getWorkerStartDelay(),
86 poolMaxSize: Configuration.getWorkerPoolMaxSize(),
87 poolMinSize: Configuration.getWorkerPoolMinSize(),
88 elementsPerWorker: Configuration.getChargingStationsPerWorker(),
90 workerChoiceStrategy: Configuration.getWorkerPoolStrategy()
94 return this.workerImplementationInstance;