8c6d415a8198367639776712209b51e249720d4f
[e-mobility-charging-stations-simulator.git] / src / charging-station / Bootstrap.ts
1 import Configuration from '../utils/Configuration';
2 import { StationWorkerData } from '../types/Worker';
3 import Utils from '../utils/Utils';
4 import WorkerFactory from '../worker/WorkerFactory';
5 import Wrk from '../worker/Wrk';
6 import { isMainThread } from 'worker_threads';
7
8 export default class Bootstrap {
9 private static instance: Bootstrap;
10 private started: boolean;
11 private workerScript: string;
12 private workerImplementationInstance: Wrk;
13
14 private constructor() {
15 this.started = false;
16 this.workerScript = './dist/charging-station/StationWorker.js';
17 }
18
19 public static getInstance(): Bootstrap {
20 if (!Bootstrap.instance) {
21 Bootstrap.instance = new Bootstrap();
22 }
23 return Bootstrap.instance;
24 }
25
26 public async start(): Promise<void> {
27 if (isMainThread && !this.started) {
28 try {
29 let numStationsTotal = 0;
30 await this.getWorkerImplementationInstance().start();
31 // Start ChargingStation object in worker thread
32 if (Configuration.getStationTemplateURLs()) {
33 for (const stationURL of Configuration.getStationTemplateURLs()) {
34 try {
35 const nbStations = stationURL.numberOfStations ? stationURL.numberOfStations : 0;
36 for (let index = 1; index <= nbStations; index++) {
37 const workerData: StationWorkerData = {
38 index,
39 templateFile: stationURL.file
40 };
41 await this.getWorkerImplementationInstance().addElement(workerData);
42 numStationsTotal++;
43 }
44 } catch (error) {
45 // eslint-disable-next-line no-console
46 console.error('Charging station start with template file ' + stationURL.file + ' error ', error);
47 }
48 }
49 } else {
50 console.log('No stationTemplateURLs defined in configuration, exiting');
51 }
52 if (numStationsTotal === 0) {
53 console.log('No charging station template enabled in configuration, exiting');
54 } else {
55 console.log(`Charging station simulator 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} charging station(s) per worker)`);
56 }
57 this.started = true;
58 } catch (error) {
59 // eslint-disable-next-line no-console
60 console.error('Bootstrap start error ', error);
61 }
62 }
63 }
64
65 public async stop(): Promise<void> {
66 if (isMainThread && this.started) {
67 if (this.getWorkerImplementationInstance()) {
68 await this.getWorkerImplementationInstance().stop();
69 // Nullify to force worker implementation instance creation
70 this.workerImplementationInstance = null;
71 }
72 }
73 this.started = false;
74 }
75
76 public async restart(): Promise<void> {
77 await this.stop();
78 await this.start();
79 }
80
81 private getWorkerImplementationInstance(): Wrk {
82 if (!this.workerImplementationInstance) {
83 this.workerImplementationInstance = WorkerFactory.getWorkerImplementation<StationWorkerData>(this.workerScript, Configuration.getWorkerProcess(), {
84 poolMaxSize: Configuration.getWorkerPoolMaxSize(),
85 poolMinSize: Configuration.getWorkerPoolMinSize(),
86 elementsPerWorker: Configuration.getChargingStationsPerWorker()
87 });
88 }
89 return this.workerImplementationInstance;
90 }
91 }