]> Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/commitdiff
perf: speed up simulator startup
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Thu, 14 Aug 2025 14:14:48 +0000 (16:14 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Thu, 14 Aug 2025 14:14:48 +0000 (16:14 +0200)
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

index b4f3633bc1109ad8345ff79d05006c222265ba7a..1932f34b60cd12a7542ff9ca406c64d87c1e8fd2 100644 (file)
@@ -156,14 +156,15 @@ export class Bootstrap extends EventEmitter {
         templateFile
       ),
     })
-    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-    const templateStatistics = this.templateStatistics.get(buildTemplateName(templateFile))!
-    ++templateStatistics.added
-    templateStatistics.indexes.add(index)
+    const templateStatistics = this.templateStatistics.get(buildTemplateName(templateFile))
+    if (stationInfo != null && templateStatistics != null) {
+      ++templateStatistics.added
+      templateStatistics.indexes.add(index)
+    }
     return stationInfo
   }
 
-  public getLastIndex (templateName: string): number {
+  public getLastContiguousIndex (templateName: string): number {
     // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
     const indexes = [...this.templateStatistics.get(templateName)!.indexes]
       .concat(0)
@@ -236,8 +237,20 @@ export class Bootstrap extends EventEmitter {
         for (const stationTemplateUrl of Configuration.getStationTemplateUrls()!) {
           try {
             const nbStations = stationTemplateUrl.numberOfStations
+            const addChargingStationTasks: Promise<ChargingStationInfo | undefined>[] = []
             for (let index = 1; index <= nbStations; index++) {
-              await this.addChargingStation(index, stationTemplateUrl.file)
+              addChargingStationTasks.push(this.addChargingStation(index, stationTemplateUrl.file))
+            }
+            const results = await Promise.allSettled(addChargingStationTasks)
+            for (const result of results) {
+              if (result.status === 'rejected') {
+                console.error(
+                  chalk.red(
+                    `Error at starting charging station with template file ${stationTemplateUrl.file}: `
+                  ),
+                  result.reason
+                )
+              }
             }
           } catch (error) {
             console.error(
@@ -300,11 +313,7 @@ export class Bootstrap extends EventEmitter {
             Constants.EMPTY_FROZEN_OBJECT
           )
         )
-        try {
-          await this.waitChargingStationsStopped()
-        } catch (error) {
-          console.error(chalk.red('Error while waiting for charging stations to stop: '), error)
-        }
+        await this.waitChargingStationsStopped()
         await this.workerImplementation?.stop()
         this.removeAllListeners()
         this.uiServer.clearCaches()
@@ -324,18 +333,11 @@ export class Bootstrap extends EventEmitter {
     this.stop()
       .then(() => {
         console.info(chalk.green('Graceful shutdown'))
-        this.uiServer.stop()
-        this.uiServerStarted = false
-        this.waitChargingStationsStopped()
-          // eslint-disable-next-line promise/no-nesting
-          .then(() => {
-            return exit(exitCodes.succeeded)
-          })
-          // eslint-disable-next-line promise/no-nesting
-          .catch(() => {
-            exit(exitCodes.gracefulShutdownError)
-          })
-        return undefined
+        if (this.uiServerStarted) {
+          this.uiServer.stop()
+          this.uiServerStarted = false
+        }
+        return exit(exitCodes.succeeded)
       })
       .catch((error: unknown) => {
         console.error(chalk.red('Error while shutdowning charging stations simulator: '), error)
index 0ca837e888353e16e88e5bc5cdb20170eda02234..aec5e15cf03e40a00b9b116a9af1fd7ae916271a 100644 (file)
@@ -229,7 +229,7 @@ export abstract class AbstractUIService {
       let stationInfo: ChargingStationInfo | undefined
       try {
         stationInfo = await Bootstrap.getInstance().addChargingStation(
-          Bootstrap.getInstance().getLastIndex(template) + 1,
+          Bootstrap.getInstance().getLastContiguousIndex(template) + 1,
           `${template}.json`,
           options
         )