refactor(simulator): propagate and handle startup error at callee
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Mon, 6 Feb 2023 10:50:16 +0000 (11:50 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Mon, 6 Feb 2023 10:50:16 +0000 (11:50 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/charging-station/Bootstrap.ts
src/charging-station/ui-server/ui-services/AbstractUIService.ts
src/start.ts

index 5c56c5dd3a84bc54c9a1be3ae640b24b790f078a..55febdbab838a481e9f8e3420c0fcdea24e5e8c8 100644 (file)
@@ -82,51 +82,47 @@ export class Bootstrap {
 
   public async start(): Promise<void> {
     if (isMainThread && this.started === false) {
-      try {
-        this.initializeCounters();
-        this.initializeWorkerImplementation();
-        await this.workerImplementation?.start();
-        await this.storage?.open();
-        this.uiServer?.start();
-        // Start ChargingStation object instance in worker thread
-        for (const stationTemplateUrl of Configuration.getStationTemplateUrls()) {
-          try {
-            const nbStations = stationTemplateUrl.numberOfStations ?? 0;
-            for (let index = 1; index <= nbStations; index++) {
-              await this.startChargingStation(index, stationTemplateUrl);
-            }
-          } catch (error) {
-            console.error(
-              chalk.red(
-                `Error at starting charging station with template file ${stationTemplateUrl.file}: `
-              ),
-              error
-            );
+      this.initializeCounters();
+      this.initializeWorkerImplementation();
+      await this.workerImplementation?.start();
+      await this.storage?.open();
+      this.uiServer?.start();
+      // Start ChargingStation object instance in worker thread
+      for (const stationTemplateUrl of Configuration.getStationTemplateUrls()) {
+        try {
+          const nbStations = stationTemplateUrl.numberOfStations ?? 0;
+          for (let index = 1; index <= nbStations; index++) {
+            await this.startChargingStation(index, stationTemplateUrl);
           }
+        } catch (error) {
+          console.error(
+            chalk.red(
+              `Error at starting charging station with template file ${stationTemplateUrl.file}: `
+            ),
+            error
+          );
         }
-        console.info(
-          chalk.green(
-            `Charging stations simulator ${
-              this.version
-            } started with ${this.numberOfChargingStations.toString()} charging station(s) from ${this.numberOfChargingStationTemplates.toString()} configured charging station template(s) and ${
-              ChargingStationUtils.workerDynamicPoolInUse()
-                ? `${Configuration.getWorker().poolMinSize?.toString()}/`
-                : ''
-            }${this.workerImplementation?.size}${
-              ChargingStationUtils.workerPoolInUse()
-                ? `/${Configuration.getWorker().poolMaxSize?.toString()}`
-                : ''
-            } worker(s) concurrently running in '${Configuration.getWorker().processType}' mode${
-              !Utils.isNullOrUndefined(this.workerImplementation?.maxElementsPerWorker)
-                ? ` (${this.workerImplementation?.maxElementsPerWorker} charging station(s) per worker)`
-                : ''
-            }`
-          )
-        );
-        this.started = true;
-      } catch (error) {
-        console.error(chalk.red('Bootstrap start error: '), error);
       }
+      console.info(
+        chalk.green(
+          `Charging stations simulator ${
+            this.version
+          } started with ${this.numberOfChargingStations.toString()} charging station(s) from ${this.numberOfChargingStationTemplates.toString()} configured charging station template(s) and ${
+            ChargingStationUtils.workerDynamicPoolInUse()
+              ? `${Configuration.getWorker().poolMinSize?.toString()}/`
+              : ''
+          }${this.workerImplementation?.size}${
+            ChargingStationUtils.workerPoolInUse()
+              ? `/${Configuration.getWorker().poolMaxSize?.toString()}`
+              : ''
+          } worker(s) concurrently running in '${Configuration.getWorker().processType}' mode${
+            !Utils.isNullOrUndefined(this.workerImplementation?.maxElementsPerWorker)
+              ? ` (${this.workerImplementation?.maxElementsPerWorker} charging station(s) per worker)`
+              : ''
+          }`
+        )
+      );
+      this.started = true;
     } else {
       console.error(chalk.red('Cannot start an already started charging stations simulator'));
     }
index c00ad92021b5f5c76073d74554b5aa380d6a3da8..f8cebbf03f134645dfc040207c7c9ec97c1af81b 100644 (file)
@@ -184,12 +184,20 @@ export default abstract class AbstractUIService {
   }
 
   private async handleStartSimulator(): Promise<ResponsePayload> {
-    await Bootstrap.getInstance().start();
-    return { status: ResponseStatus.SUCCESS };
+    try {
+      await Bootstrap.getInstance().start();
+      return { status: ResponseStatus.SUCCESS };
+    } catch (error) {
+      return { status: ResponseStatus.FAILURE };
+    }
   }
 
   private async handleStopSimulator(): Promise<ResponsePayload> {
-    await Bootstrap.getInstance().stop();
-    return { status: ResponseStatus.SUCCESS };
+    try {
+      await Bootstrap.getInstance().stop();
+      return { status: ResponseStatus.SUCCESS };
+    } catch (error) {
+      return { status: ResponseStatus.FAILURE };
+    }
   }
 }
index 8eca3a4a11e093983b7ab8f499b2c6406175a2fa..0185903dd0a3b4993fb0559c15134a2271776f35 100644 (file)
@@ -7,5 +7,5 @@ import { Bootstrap } from './internal';
 Bootstrap.getInstance()
   .start()
   .catch((error) => {
-    console.error(chalk.red(error));
+    console.error(chalk.red('Startup error: '), error);
   });