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
'node:worker_threads';
7 import chalk from
'chalk';
9 import { ChargingStationUtils
} from
'./ChargingStationUtils';
10 import type { AbstractUIServer
} from
'./ui-server/AbstractUIServer';
11 import { UIServerFactory
} from
'./ui-server/UIServerFactory';
12 import packageJson from
'../../package.json' assert
{ type: 'json' };
13 import { BaseError
} from
'../exception';
14 import { type Storage
, StorageFactory
} from
'../performance';
16 type ChargingStationData
,
17 type ChargingStationWorkerData
,
18 type ChargingStationWorkerMessage
,
19 type ChargingStationWorkerMessageData
,
20 ChargingStationWorkerMessageEvents
,
21 type StationTemplateUrl
,
24 import { Configuration
, ErrorUtils
, Utils
, logger
} from
'../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 = packageJson
.version
;
43 private initializedCounters
: boolean;
44 private started
: boolean;
45 private readonly workerScript
: string;
47 private constructor() {
48 // Enable unconditionally for now
49 ErrorUtils
.handleUnhandledRejection();
50 ErrorUtils
.handleUncaughtException();
51 this.initializedCounters
= false;
53 this.initializeCounters();
54 this.workerImplementation
= null;
55 this.workerScript
= path
.join(
56 path
.dirname(fileURLToPath(import.meta
.url
)),
57 `ChargingStationWorker${path.extname(fileURLToPath(import.meta.url))}`
59 Configuration
.getUIServer().enabled
=== true &&
60 (this.uiServer
= UIServerFactory
.getUIServerImplementation(Configuration
.getUIServer()));
61 Configuration
.getPerformanceStorage().enabled
=== true &&
62 (this.storage
= StorageFactory
.getStorage(
63 Configuration
.getPerformanceStorage().type,
64 Configuration
.getPerformanceStorage().uri
,
67 Configuration
.setConfigurationChangeCallback(async () => Bootstrap
.getInstance().restart());
70 public static getInstance(): Bootstrap
{
71 if (Bootstrap
.instance
=== null) {
72 Bootstrap
.instance
= new Bootstrap();
74 return Bootstrap
.instance
;
77 public async start(): Promise
<void> {
78 if (isMainThread
&& this.started
=== false) {
79 this.initializeCounters();
80 this.initializeWorkerImplementation();
81 await this.workerImplementation
?.start();
82 await this.storage
?.open();
83 this.uiServer
?.start();
84 // Start ChargingStation object instance in worker thread
85 for (const stationTemplateUrl
of Configuration
.getStationTemplateUrls()) {
87 const nbStations
= stationTemplateUrl
.numberOfStations
?? 0;
88 for (let index
= 1; index
<= nbStations
; index
++) {
89 await this.startChargingStation(index
, stationTemplateUrl
);
94 `Error at starting charging station with template file ${stationTemplateUrl.file}: `
102 `Charging stations simulator ${
104 } started with ${this.numberOfChargingStations.toString()} charging station(s) from ${this.numberOfChargingStationTemplates.toString()} configured charging station template(s) and ${
105 ChargingStationUtils.workerDynamicPoolInUse()
106 ? `${Configuration.getWorker().poolMinSize?.toString()}
/`
108 }${this.workerImplementation?.size}${
109 ChargingStationUtils.workerPoolInUse()
110 ? `/${Configuration.getWorker().poolMaxSize?.toString()}
`
112 } worker(s) concurrently running in '${Configuration.getWorker().processType}' mode${
113 !Utils.isNullOrUndefined(this.workerImplementation?.maxElementsPerWorker)
114 ? ` (${this.workerImplementation?.maxElementsPerWorker} charging
station(s
) per worker
)`
121 console
.error(chalk
.red('Cannot start an already started charging stations simulator'));
125 public async stop(): Promise
<void> {
126 if (isMainThread
&& this.started
=== true) {
127 await this.workerImplementation
?.stop();
128 this.workerImplementation
= null;
129 this.uiServer
?.stop();
130 await this.storage
?.close();
131 this.initializedCounters
= false;
132 this.started
= false;
134 console
.error(chalk
.red('Cannot stop a not started charging stations simulator'));
138 public async restart(): Promise
<void> {
143 private initializeWorkerImplementation(): void {
144 this.workerImplementation
=== null &&
145 (this.workerImplementation
= WorkerFactory
.getWorkerImplementation
<ChargingStationWorkerData
>(
147 Configuration
.getWorker().processType
,
149 workerStartDelay
: Configuration
.getWorker().startDelay
,
150 elementStartDelay
: Configuration
.getWorker().elementStartDelay
,
151 poolMaxSize
: Configuration
.getWorker().poolMaxSize
,
152 poolMinSize
: Configuration
.getWorker().poolMinSize
,
153 elementsPerWorker
: Configuration
.getWorker().elementsPerWorker
,
155 workerChoiceStrategy
: Configuration
.getWorker().poolStrategy
,
157 messageHandler
: this.messageHandler
.bind(this) as MessageHandler
<Worker
>,
162 private messageHandler(
163 msg
: ChargingStationWorkerMessage
<ChargingStationWorkerMessageData
>
166 // `${this.logPrefix()} ${moduleName}.messageHandler: Worker channel message received: ${JSON.stringify(
174 case ChargingStationWorkerMessageEvents
.started
:
175 this.workerEventStarted(msg
.data
as ChargingStationData
);
177 case ChargingStationWorkerMessageEvents
.stopped
:
178 this.workerEventStopped(msg
.data
as ChargingStationData
);
180 case ChargingStationWorkerMessageEvents
.updated
:
181 this.workerEventUpdated(msg
.data
as ChargingStationData
);
183 case ChargingStationWorkerMessageEvents
.performanceStatistics
:
184 this.workerEventPerformanceStatistics(msg
.data
as Statistics
);
188 `Unknown event type: '${msg.id}' for data: ${JSON.stringify(msg.data, null, 2)}`
193 `${this.logPrefix()} ${moduleName}.messageHandler: Error occurred while handling '${
201 private workerEventStarted
= (data
: ChargingStationData
) => {
202 this.uiServer
?.chargingStations
.set(data
.stationInfo
.hashId
, data
);
203 ++this.numberOfStartedChargingStations
;
205 `${this.logPrefix()} ${moduleName}.workerEventStarted: Charging station ${
206 data.stationInfo.chargingStationId
207 } (hashId: ${data.stationInfo.hashId}) started (${
208 this.numberOfStartedChargingStations
209 } started from ${this.numberOfChargingStations})`
213 private workerEventStopped
= (data
: ChargingStationData
) => {
214 this.uiServer
?.chargingStations
.set(data
.stationInfo
.hashId
, data
);
215 --this.numberOfStartedChargingStations
;
217 `${this.logPrefix()} ${moduleName}.workerEventStopped: Charging station ${
218 data.stationInfo.chargingStationId
219 } (hashId: ${data.stationInfo.hashId}) stopped (${
220 this.numberOfStartedChargingStations
221 } started from ${this.numberOfChargingStations})`
225 private workerEventUpdated
= (data
: ChargingStationData
) => {
226 this.uiServer
?.chargingStations
.set(data
.stationInfo
.hashId
, data
);
229 private workerEventPerformanceStatistics
= (data
: Statistics
) => {
230 this.storage
.storePerformanceStatistics(data
) as void;
233 private initializeCounters() {
234 if (this.initializedCounters
=== false) {
235 this.numberOfChargingStationTemplates
= 0;
236 this.numberOfChargingStations
= 0;
237 const stationTemplateUrls
= Configuration
.getStationTemplateUrls();
238 if (Utils
.isNotEmptyArray(stationTemplateUrls
)) {
239 this.numberOfChargingStationTemplates
= stationTemplateUrls
.length
;
240 for (const stationTemplateUrl
of stationTemplateUrls
) {
241 this.numberOfChargingStations
+= stationTemplateUrl
.numberOfStations
?? 0;
245 chalk
.yellow("'stationTemplateUrls' not defined or empty in configuration, exiting")
247 process
.exit(exitCodes
.missingChargingStationsConfiguration
);
249 if (this.numberOfChargingStations
=== 0) {
251 chalk
.yellow('No charging station template enabled in configuration, exiting')
253 process
.exit(exitCodes
.noChargingStationTemplates
);
255 this.numberOfStartedChargingStations
= 0;
256 this.initializedCounters
= true;
260 private async startChargingStation(
262 stationTemplateUrl
: StationTemplateUrl
264 await this.workerImplementation
?.addElement({
266 templateFile
: path
.join(
267 path
.dirname(fileURLToPath(import.meta
.url
)),
270 stationTemplateUrl
.file
275 private logPrefix
= (): string => {
276 return Utils
.logPrefix(' Bootstrap |');