]> Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/commitdiff
fix: respect elementAddDelay by using sequential startup when configured
authorJérôme Benoit <jerome.benoit@sap.com>
Thu, 26 Mar 2026 16:42:21 +0000 (17:42 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Thu, 26 Mar 2026 16:42:21 +0000 (17:42 +0100)
The perf commit 218fd550 switched to parallel Promise.allSettled for
startup speed, but broke elementAddDelay since all stations launched
simultaneously. Now uses sequential await loop when elementAddDelay > 0
and parallel allSettled when 0 or unset. Removes redundant outer
try/catch — config errors propagate as fatal, station errors are
handled per-station in both paths.

src/charging-station/Bootstrap.ts

index e232f2915565dd2c2d425476f4a729b3dda3d441..80df1fa552e51f54dfbf2f86f8fa4e5bbf0d7869 100644 (file)
@@ -239,15 +239,30 @@ export class Bootstrap extends EventEmitter {
           // Start ChargingStation object instance in worker thread
           // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
           for (const stationTemplateUrl of Configuration.getStationTemplateUrls()!) {
-            try {
-              const nbStations = stationTemplateUrl.numberOfStations
-              const addChargingStationTasks: Promise<ChargingStationInfo | undefined>[] = []
+            const nbStations = stationTemplateUrl.numberOfStations
+            const sequentialAdd =
+              (Configuration.getConfigurationSection<WorkerConfiguration>(
+                ConfigurationSection.worker
+              ).elementAddDelay ?? 0) > 0
+            if (sequentialAdd) {
               for (let index = 1; index <= nbStations; index++) {
-                addChargingStationTasks.push(
-                  this.addChargingStation(index, stationTemplateUrl.file)
-                )
+                try {
+                  await this.addChargingStation(index, stationTemplateUrl.file)
+                } catch (error) {
+                  console.error(
+                    chalk.red(
+                      `Error at starting charging station with template file ${stationTemplateUrl.file}: `
+                    ),
+                    error
+                  )
+                }
               }
-              const results = await Promise.allSettled(addChargingStationTasks)
+            } else {
+              const results = await Promise.allSettled(
+                Array.from({ length: nbStations }, (_, i) =>
+                  this.addChargingStation(i + 1, stationTemplateUrl.file)
+                )
+              )
               for (const result of results) {
                 if (result.status === 'rejected') {
                   console.error(
@@ -258,13 +273,6 @@ export class Bootstrap extends EventEmitter {
                   )
                 }
               }
-            } catch (error) {
-              console.error(
-                chalk.red(
-                  `Error at starting charging station with template file ${stationTemplateUrl.file}: `
-                ),
-                error
-              )
             }
           }
           const workerConfiguration = Configuration.getConfigurationSection<WorkerConfiguration>(