From: Jérôme Benoit Date: Mon, 18 May 2026 21:49:47 +0000 (+0200) Subject: refactor(bootstrap): factorize UI server start/stop logic X-Git-Tag: cli@v4.8.0~26 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=24169fc1b3a4c49986cdab3a595cb6818aae0691;p=e-mobility-charging-stations-simulator.git refactor(bootstrap): factorize UI server start/stop logic 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. --- diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index 76422a72..ea014fdb 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -271,16 +271,7 @@ export class Bootstrap extends EventEmitter implements IBootstrap { await this.storage.open() } } - if ( - !this.uiServerStarted && - Configuration.getConfigurationSection( - 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 { await this.stop(StopReason.reload) if ( - this.uiServerStarted && Configuration.getConfigurationSection(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(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 =>