]> Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/commitdiff
refactor(bootstrap): factorize UI server start/stop logic
authorJérôme Benoit <jerome.benoit@sap.com>
Mon, 18 May 2026 21:49:47 +0000 (23:49 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Mon, 18 May 2026 21:49:47 +0000 (23:49 +0200)
Extract idempotent helpers and remove duplication across the four
UI server lifecycle call sites:

- public startUIServer() now contains the actual logic instead of
  delegating to a private wrapper; start() calls it directly.
- new private stopUIServer() helper replaces the two inline stop
  blocks in gracefulShutdown() and restart().
- restart() guards syncUIServerTemplates() on uiServerStarted to
  avoid redundant template sync (twice when re-enabling, wasted
  call when the UI server is disabled or stopped). startUIServer()
  performs the sync itself when it actually starts the server.

No public API change. Behavior preserved.

src/charging-station/Bootstrap.ts

index 76422a7216dfd5ae77b445e61c0fcf06cdc534a3..ea014fdb52af162d5b229785f0179291cc9d3fe0 100644 (file)
@@ -271,16 +271,7 @@ export class Bootstrap extends EventEmitter implements IBootstrap {
               await this.storage.open()
             }
           }
-          if (
-            !this.uiServerStarted &&
-            Configuration.getConfigurationSection<UIServerConfiguration>(
-              ConfigurationSection.uiServer
-            ).enabled === true
-          ) {
-            this.syncUIServerTemplates()
-            this.uiServer.start()
-            this.uiServerStarted = true
-          }
+          this.startUIServer()
           // Start ChargingStation object instance in worker thread
           for (const stationTemplateUrl of Configuration.getStationTemplateUrls() ?? []) {
             const nbStations = stationTemplateUrl.numberOfStations
@@ -420,10 +411,7 @@ export class Bootstrap extends EventEmitter implements IBootstrap {
     this.stop(StopReason.shutdown)
       .then(() => {
         logger.info(`${this.logPrefix()} ${moduleName}.gracefulShutdown: Graceful shutdown`)
-        if (this.uiServerStarted) {
-          this.uiServer.stop()
-          this.uiServerStarted = false
-        }
+        this.stopUIServer()
         return exit(exitCodes.succeeded)
       })
       .catch((error: unknown) => {
@@ -632,15 +620,15 @@ export class Bootstrap extends EventEmitter implements IBootstrap {
   private async restart (): Promise<void> {
     await this.stop(StopReason.reload)
     if (
-      this.uiServerStarted &&
       Configuration.getConfigurationSection<UIServerConfiguration>(ConfigurationSection.uiServer)
         .enabled !== true
     ) {
-      this.uiServer.stop()
-      this.uiServerStarted = false
+      this.stopUIServer()
     }
     this.prepareTemplateStatistics()
-    this.syncUIServerTemplates()
+    if (this.uiServerStarted) {
+      this.syncUIServerTemplates()
+    }
     // TODO: compare worker configuration hash to skip unnecessary re-initialization
     this.initializeWorkerImplementation(
       Configuration.getConfigurationSection<WorkerConfiguration>(ConfigurationSection.worker)
@@ -648,6 +636,14 @@ export class Bootstrap extends EventEmitter implements IBootstrap {
     await this.start()
   }
 
+  private stopUIServer (): void {
+    if (!this.uiServerStarted) {
+      return
+    }
+    this.uiServer.stop()
+    this.uiServerStarted = false
+  }
+
   private syncUIServerTemplates (): void {
     this.uiServer.setChargingStationTemplates(
       Configuration.getStationTemplateUrls()?.map(stationTemplateUrl =>