Merge pull request #813 from SAP/combined-prs-branch
[e-mobility-charging-stations-simulator.git] / src / charging-station / Bootstrap.ts
index 37fe0e6b5ae7c5506df07f5d17b94af3b37abfcf..db3c62448877e08cde03de80e7352fed279ba8e7 100644 (file)
@@ -2,13 +2,14 @@
 
 import { EventEmitter } from 'node:events';
 import { dirname, extname, join } from 'node:path';
+import { exit } from 'node:process';
 import { fileURLToPath } from 'node:url';
 import { isMainThread } from 'node:worker_threads';
 
 import chalk from 'chalk';
 import { availableParallelism } from 'poolifier';
 
-import { waitChargingStationEvents } from './ChargingStationUtils';
+import { waitChargingStationEvents } from './Helpers';
 import type { AbstractUIServer } from './ui-server/AbstractUIServer';
 import { UIServerFactory } from './ui-server/UIServerFactory';
 import { version } from '../../package.json' assert { type: 'json' };
@@ -45,8 +46,10 @@ import { type WorkerAbstract, WorkerFactory } from '../worker';
 const moduleName = 'Bootstrap';
 
 enum exitCodes {
+  succeeded = 0,
   missingChargingStationsConfiguration = 1,
   noChargingStationTemplates = 2,
+  gracefulShutdownError = 3,
 }
 
 export class Bootstrap extends EventEmitter {
@@ -97,7 +100,7 @@ export class Bootstrap extends EventEmitter {
         performanceStorageConfiguration.uri!,
         this.logPrefix(),
       ));
-    Configuration.setConfigurationChangeCallback(async () => Bootstrap.getInstance().restart());
+    Configuration.configurationChangeCallback = async () => Bootstrap.getInstance().restart();
   }
 
   public static getInstance(): Bootstrap {
@@ -109,7 +112,7 @@ export class Bootstrap extends EventEmitter {
 
   public async start(): Promise<void> {
     if (!isMainThread) {
-      throw new Error('Cannot start charging stations simulator from worker thread');
+      throw new BaseError('Cannot start charging stations simulator from worker thread');
     }
     if (this.started === false) {
       if (this.starting === false) {
@@ -160,7 +163,7 @@ export class Bootstrap extends EventEmitter {
         Configuration.workerDynamicPoolInUse() &&
           console.warn(
             chalk.yellow(
-              'Charging stations simulator is using dynamic pool mode. This is an experimental feature with known issues.\nPlease consider using static pool or worker set mode instead',
+              'Charging stations simulator is using dynamic pool mode. This is an experimental feature with known issues.\nPlease consider using fixed pool or worker set mode instead',
             ),
           );
         console.info(chalk.green('Worker set/pool information:'), this.workerImplementation?.info);
@@ -176,7 +179,7 @@ export class Bootstrap extends EventEmitter {
 
   public async stop(): Promise<void> {
     if (!isMainThread) {
-      throw new Error('Cannot stop charging stations simulator from worker thread');
+      throw new BaseError('Cannot stop charging stations simulator from worker thread');
     }
     if (this.started === true) {
       if (this.stopping === false) {
@@ -185,7 +188,7 @@ export class Bootstrap extends EventEmitter {
           this.uiServer.buildProtocolRequest(
             generateUUID(),
             ProcedureName.STOP_CHARGING_STATION,
-            Constants.EMPTY_FREEZED_OBJECT,
+            Constants.EMPTY_FROZEN_OBJECT,
           ),
         );
         await Promise.race([
@@ -230,7 +233,7 @@ export class Bootstrap extends EventEmitter {
     if (workerConfiguration?.elementsPerWorker === 'auto') {
       elementsPerWorker =
         this.numberOfChargingStations > availableParallelism()
-          ? Math.round(this.numberOfChargingStations / availableParallelism())
+          ? Math.round(this.numberOfChargingStations / (availableParallelism() * 1.5))
           : 1;
     }
     this.workerImplementation === null &&
@@ -256,13 +259,10 @@ export class Bootstrap extends EventEmitter {
     // logger.debug(
     //   `${this.logPrefix()} ${moduleName}.messageHandler: Worker channel message received: ${JSON.stringify(
     //     msg,
-    //     null,
+    //     undefined,
     //     2,
     //   )}`,
     // );
-    if (isNullOrUndefined(msg?.event)) {
-      return;
-    }
     try {
       switch (msg.event) {
         case ChargingStationWorkerMessageEvents.started:
@@ -284,9 +284,20 @@ export class Bootstrap extends EventEmitter {
             msg.data as Statistics,
           );
           break;
+        case ChargingStationWorkerMessageEvents.startWorkerElementError:
+          logger.error(
+            `${this.logPrefix()} ${moduleName}.messageHandler: Error occured while starting worker element:`,
+            msg.data,
+          );
+          this.emit(ChargingStationWorkerMessageEvents.startWorkerElementError, msg.data);
+          break;
+        case ChargingStationWorkerMessageEvents.startedWorkerElement:
+          break;
         default:
           throw new BaseError(
-            `Unknown event type: '${msg.event}' for data: ${JSON.stringify(msg.data, null, 2)}`,
+            `Unknown charging station worker event: '${
+              msg.event
+            }' received with data: ${JSON.stringify(msg.data, undefined, 2)}`,
           );
       }
     } catch (error) {
@@ -344,13 +355,13 @@ export class Bootstrap extends EventEmitter {
         console.warn(
           chalk.yellow("'stationTemplateUrls' not defined or empty in configuration, exiting"),
         );
-        process.exit(exitCodes.missingChargingStationsConfiguration);
+        exit(exitCodes.missingChargingStationsConfiguration);
       }
       if (this.numberOfChargingStations === 0) {
         console.warn(
           chalk.yellow('No charging station template enabled in configuration, exiting'),
         );
-        process.exit(exitCodes.noChargingStationTemplates);
+        exit(exitCodes.noChargingStationTemplates);
       }
       this.initializedCounters = true;
     }
@@ -378,14 +389,14 @@ export class Bootstrap extends EventEmitter {
   }
 
   private gracefulShutdown = (): void => {
-    console.info(`${chalk.green('Graceful shutdown')}`);
     this.stop()
       .then(() => {
-        process.exit(0);
+        console.info(`${chalk.green('Graceful shutdown')}`);
+        exit(exitCodes.succeeded);
       })
       .catch((error) => {
         console.error(chalk.red('Error while shutdowning charging stations simulator: '), error);
-        process.exit(1);
+        exit(exitCodes.gracefulShutdownError);
       });
   };