From: Jérôme Benoit Date: Tue, 7 Nov 2023 22:20:34 +0000 (+0100) Subject: fix: avoid concurrent configuration change callback execution X-Git-Tag: v1.2.24~36 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=1b4ccee3d824d7ad5c3e52b81d821be7793866f2;p=e-mobility-charging-stations-simulator.git fix: avoid concurrent configuration change callback execution Signed-off-by: Jérôme Benoit --- diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index 48b4897f..4b10778b 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -56,6 +56,7 @@ export class Configuration { 'config.json', ); + private static configurationFileReloading = false; private static configurationData?: ConfigurationData; private static configurationFileWatcher?: FSWatcher; private static configurationSectionCache = new Map< @@ -546,8 +547,14 @@ export class Configuration { private static getConfigurationFileWatcher(): FSWatcher | undefined { try { return watch(Configuration.configurationFile, (event, filename): void => { - if (filename!.trim()!.length > 0 && event === 'change') { - console.warn( + if ( + !Configuration.configurationFileReloading && + filename!.trim()!.length > 0 && + event === 'change' + ) { + Configuration.configurationFileReloading = true; + const consoleWarnOnce = once(console.warn, this); + consoleWarnOnce( `${chalk.green(configurationLogPrefix())} ${chalk.yellow( `${FileType.Configuration} ${this.configurationFile} file have changed, reload`, )}`, @@ -555,9 +562,13 @@ export class Configuration { delete Configuration.configurationData; Configuration.configurationSectionCache.clear(); if (!isUndefined(Configuration.configurationChangeCallback)) { - Configuration.configurationChangeCallback().catch((error) => { - throw typeof error === 'string' ? new Error(error) : error; - }); + Configuration.configurationChangeCallback() + .catch((error) => { + throw typeof error === 'string' ? new Error(error) : error; + }) + .finally(() => { + Configuration.configurationFileReloading = false; + }); } } });