1 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
3 import path from
'node:path';
4 import { fileURLToPath
} from
'node:url';
5 import { type Worker
, isMainThread
} from
'worker_threads';
7 import chalk from
'chalk';
9 import { type AbstractUIServer
, ChargingStationUtils
, UIServerFactory
} from
'./internal';
10 import { version
} from
'../../package.json';
11 import { BaseError
} from
'../exception';
12 import { type Storage
, StorageFactory
} from
'../performance';
14 type ChargingStationData
,
15 type ChargingStationWorkerData
,
16 type ChargingStationWorkerMessage
,
17 type ChargingStationWorkerMessageData
,
18 ChargingStationWorkerMessageEvents
,
19 type StationTemplateUrl
,
22 import { Configuration
} from
'../utils/Configuration';
23 import { logger
} from
'../utils/Logger';
24 import { Utils
} from
'../utils/Utils';
25 import { type MessageHandler
, type WorkerAbstract
, WorkerFactory
} from
'../worker';
27 const moduleName
= 'Bootstrap';
30 missingChargingStationsConfiguration
= 1,
31 noChargingStationTemplates
= 2,
34 export class Bootstrap
{
35 private static instance
: Bootstrap
| null = null;
36 public numberOfChargingStations
!: number;
37 public numberOfChargingStationTemplates
!: number;
38 private workerImplementation
: WorkerAbstract
<ChargingStationWorkerData
> | null;
39 private readonly uiServer
!: AbstractUIServer
| null;
40 private readonly storage
!: Storage
;
41 private numberOfStartedChargingStations
!: number;
42 private readonly version
: string = version
;
43 private initializedCounters
: boolean;
44 private started
: boolean;
45 private readonly workerScript
: string;
47 private constructor() {
48 // Enable unconditionally for now
49 this.logUnhandledRejection();
50 this.logUncaughtException();
51 this.initializedCounters
= false;
53 this.initializeCounters();
54 this.workerImplementation
= null;
55 this.workerScript
= path
.join(
56 path
.resolve(path
.dirname(fileURLToPath(import.meta
.url
)), '../'),
58 `ChargingStationWorker${path.extname(fileURLToPath(import.meta.url))}`
60 Configuration
.getUIServer().enabled
=== true &&
61 (this.uiServer
= UIServerFactory
.getUIServerImplementation(Configuration
.getUIServer()));
62 Configuration
.getPerformanceStorage().enabled
=== true &&
63 (this.storage
= StorageFactory
.getStorage(
64 Configuration
.getPerformanceStorage().type,
65 Configuration
.getPerformanceStorage().uri
,
68 Configuration
.setConfigurationChangeCallback(async () => Bootstrap
.getInstance().restart());
71 public static getInstance(): Bootstrap
{
72 if (Bootstrap
.instance
=== null) {
73 Bootstrap
.instance
= new Bootstrap();
75 return Bootstrap
.instance
;
78 public async start(): Promise
<void> {
79 if (isMainThread
&& this.started
=== false) {
80 this.initializeCounters();
81 this.initializeWorkerImplementation();
82 await this.workerImplementation
?.start();
83 await this.storage
?.open();
84 this.uiServer
?.start();
85 // Start ChargingStation object instance in worker thread
86 for (const stationTemplateUrl
of Configuration
.getStationTemplateUrls()) {
88 const nbStations
= stationTemplateUrl
.numberOfStations
?? 0;
89 for (let index
= 1; index
<= nbStations
; index
++) {
90 await this.startChargingStation(index
, stationTemplateUrl
);
95 `Error at starting charging station with template file ${stationTemplateUrl.file}: `
103 `Charging stations simulator ${
105 } started with ${this.numberOfChargingStations.toString()} charging station(s) from ${this.numberOfChargingStationTemplates.toString()} configured charging station template(s) and ${
106 ChargingStationUtils.workerDynamicPoolInUse()
107 ? `${Configuration.getWorker().poolMinSize?.toString()}
/`
109 }${this.workerImplementation?.size}${
110 ChargingStationUtils.workerPoolInUse()
111 ? `/${Configuration.getWorker().poolMaxSize?.toString()}
`
113 } worker(s) concurrently running in '${Configuration.getWorker().processType}' mode${
114 !Utils.isNullOrUndefined(this.workerImplementation?.maxElementsPerWorker)
115 ? ` (${this.workerImplementation?.maxElementsPerWorker} charging
station(s
) per worker
)`
122 console
.error(chalk
.red('Cannot start an already started charging stations simulator'));
126 public async stop(): Promise
<void> {
127 if (isMainThread
&& this.started
=== true) {
128 await this.workerImplementation
?.stop();
129 this.workerImplementation
= null;
130 this.uiServer
?.stop();
131 await this.storage
?.close();
132 this.initializedCounters
= false;
133 this.started
= false;
135 console
.error(chalk
.red('Cannot stop a not started charging stations simulator'));
139 public async restart(): Promise
<void> {
144 private initializeWorkerImplementation(): void {
145 this.workerImplementation
=== null &&
146 (this.workerImplementation
= WorkerFactory
.getWorkerImplementation
<ChargingStationWorkerData
>(
148 Configuration
.getWorker().processType
,
150 workerStartDelay
: Configuration
.getWorker().startDelay
,
151 elementStartDelay
: Configuration
.getWorker().elementStartDelay
,
152 poolMaxSize
: Configuration
.getWorker().poolMaxSize
,
153 poolMinSize
: Configuration
.getWorker().poolMinSize
,
154 elementsPerWorker
: Configuration
.getWorker().elementsPerWorker
,
156 workerChoiceStrategy
: Configuration
.getWorker().poolStrategy
,
158 messageHandler
: this.messageHandler
.bind(this) as MessageHandler
<Worker
>,
163 private messageHandler(
164 msg
: ChargingStationWorkerMessage
<ChargingStationWorkerMessageData
>
167 // `${this.logPrefix()} ${moduleName}.messageHandler: Worker channel message received: ${JSON.stringify(
175 case ChargingStationWorkerMessageEvents
.STARTED
:
176 this.workerEventStarted(msg
.data
as ChargingStationData
);
178 case ChargingStationWorkerMessageEvents
.STOPPED
:
179 this.workerEventStopped(msg
.data
as ChargingStationData
);
181 case ChargingStationWorkerMessageEvents
.UPDATED
:
182 this.workerEventUpdated(msg
.data
as ChargingStationData
);
184 case ChargingStationWorkerMessageEvents
.PERFORMANCE_STATISTICS
:
185 this.workerEventPerformanceStatistics(msg
.data
as Statistics
);
189 `Unknown event type: '${msg.id}' for data: ${JSON.stringify(msg.data, null, 2)}`
194 `${this.logPrefix()} ${moduleName}.messageHandler: Error occurred while handling '${
202 private workerEventStarted
= (data
: ChargingStationData
) => {
203 this.uiServer
?.chargingStations
.set(data
.stationInfo
.hashId
, data
);
204 ++this.numberOfStartedChargingStations
;
206 `${this.logPrefix()} ${moduleName}.workerEventStarted: Charging station ${
207 data.stationInfo.chargingStationId
208 } (hashId: ${data.stationInfo.hashId}) started (${
209 this.numberOfStartedChargingStations
210 } started from ${this.numberOfChargingStations})`
214 private workerEventStopped
= (data
: ChargingStationData
) => {
215 this.uiServer
?.chargingStations
.set(data
.stationInfo
.hashId
, data
);
216 --this.numberOfStartedChargingStations
;
218 `${this.logPrefix()} ${moduleName}.workerEventStopped: Charging station ${
219 data.stationInfo.chargingStationId
220 } (hashId: ${data.stationInfo.hashId}) stopped (${
221 this.numberOfStartedChargingStations
222 } started from ${this.numberOfChargingStations})`
226 private workerEventUpdated
= (data
: ChargingStationData
) => {
227 this.uiServer
?.chargingStations
.set(data
.stationInfo
.hashId
, data
);
230 private workerEventPerformanceStatistics
= (data
: Statistics
) => {
231 this.storage
.storePerformanceStatistics(data
) as void;
234 private initializeCounters() {
235 if (this.initializedCounters
=== false) {
236 this.numberOfChargingStationTemplates
= 0;
237 this.numberOfChargingStations
= 0;
238 const stationTemplateUrls
= Configuration
.getStationTemplateUrls();
239 if (Utils
.isNotEmptyArray(stationTemplateUrls
)) {
240 this.numberOfChargingStationTemplates
= stationTemplateUrls
.length
;
241 stationTemplateUrls
.forEach((stationTemplateUrl
) => {
242 this.numberOfChargingStations
+= stationTemplateUrl
.numberOfStations
?? 0;
246 chalk
.yellow("'stationTemplateUrls' not defined or empty in configuration, exiting")
248 process
.exit(exitCodes
.missingChargingStationsConfiguration
);
250 if (this.numberOfChargingStations
=== 0) {
252 chalk
.yellow('No charging station template enabled in configuration, exiting')
254 process
.exit(exitCodes
.noChargingStationTemplates
);
256 this.numberOfStartedChargingStations
= 0;
257 this.initializedCounters
= true;
261 private logUncaughtException(): void {
262 process
.on('uncaughtException', (error
: Error) => {
263 console
.error(chalk
.red('Uncaught exception: '), error
);
267 private logUnhandledRejection(): void {
268 process
.on('unhandledRejection', (reason
: unknown
) => {
269 console
.error(chalk
.red('Unhandled rejection: '), reason
);
273 private async startChargingStation(
275 stationTemplateUrl
: StationTemplateUrl
277 await this.workerImplementation
?.addElement({
279 templateFile
: path
.join(
280 path
.resolve(path
.dirname(fileURLToPath(import.meta
.url
)), '../'),
283 stationTemplateUrl
.file
288 private logPrefix
= (): string => {
289 return Utils
.logPrefix(' Bootstrap |');