X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils%2FConfiguration.ts;h=1f370480d4d1b6fc47828792ccbf8271cfb81724;hb=b1f8c0c6252ee6158901c60dfa8bb6333a9e84b7;hp=bf691fa8c1d96ffcb2da9ed44fc6f62ca977e098;hpb=6d877954e2aeac8556ae0e5b849590f4928dc7cf;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index bf691fa8..1f370480 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -1,5 +1,6 @@ import { type FSWatcher, readFileSync, watch } from 'node:fs'; import { dirname, join, resolve } from 'node:path'; +import { env } from 'node:process'; import { fileURLToPath } from 'node:url'; import chalk from 'chalk'; @@ -41,13 +42,21 @@ type ConfigurationSectionType = | WorkerConfiguration | UIServerConfiguration; +// Avoid ESM race condition at class initialization +const configurationLogPrefix = (): string => { + return logPrefix(' Simulator configuration |'); +}; + export class Configuration { + public static configurationChangeCallback: () => Promise; + private static configurationFile = join( dirname(fileURLToPath(import.meta.url)), 'assets', 'config.json', ); + private static configurationFileReloading = false; private static configurationData?: ConfigurationData; private static configurationFileWatcher?: FSWatcher; private static configurationSectionCache = new Map< @@ -60,16 +69,10 @@ export class Configuration { [ConfigurationSection.uiServer, Configuration.buildUIServerSection()], ]); - private static configurationChangeCallback?: () => Promise; - private constructor() { // This is intentional } - public static setConfigurationChangeCallback(cb: () => Promise): void { - Configuration.configurationChangeCallback = cb; - } - public static getConfigurationSection( sectionName: ConfigurationSection, ): T { @@ -108,7 +111,7 @@ export class Configuration { } public static workerPoolInUse(): boolean { - return [WorkerProcessType.dynamicPool, WorkerProcessType.staticPool].includes( + return [WorkerProcessType.dynamicPool, WorkerProcessType.fixedPool].includes( Configuration.getConfigurationSection(ConfigurationSection.worker) .processType!, ); @@ -121,10 +124,6 @@ export class Configuration { ); } - private static logPrefix = (): string => { - return logPrefix(' Simulator configuration |'); - }; - private static isConfigurationSectionCached(sectionName: ConfigurationSection): boolean { return Configuration.configurationSectionCache.has(sectionName); } @@ -175,7 +174,7 @@ export class Configuration { } if (isCFEnvironment() === true) { delete uiServerConfiguration.options?.host; - uiServerConfiguration.options!.port = parseInt(process.env.PORT!); + uiServerConfiguration.options!.port = parseInt(env.PORT!); } return uiServerConfiguration; } @@ -333,7 +332,7 @@ export class Configuration { (stationTemplateUrl: StationTemplateUrl) => { if (!isUndefined(stationTemplateUrl?.['numberOfStation' as keyof StationTemplateUrl])) { console.error( - `${chalk.green(Configuration.logPrefix())} ${chalk.red( + `${chalk.green(configurationLogPrefix())} ${chalk.red( `Deprecated configuration key 'numberOfStation' usage for template file '${stationTemplateUrl.file}' in 'stationTemplateUrls'. Use 'numberOfStations' instead`, )}`, ); @@ -389,17 +388,17 @@ export class Configuration { `Use '${ConfigurationSection.worker}' section to define the worker pool minimum size instead`, ); Configuration.warnDeprecatedConfigurationKey( - 'workerPoolSize;', + 'workerPoolSize', undefined, `Use '${ConfigurationSection.worker}' section to define the worker pool maximum size instead`, ); Configuration.warnDeprecatedConfigurationKey( - 'workerPoolMaxSize;', + 'workerPoolMaxSize', undefined, `Use '${ConfigurationSection.worker}' section to define the worker pool maximum size instead`, ); Configuration.warnDeprecatedConfigurationKey( - 'workerPoolStrategy;', + 'workerPoolStrategy', undefined, `Use '${ConfigurationSection.worker}' section to define the worker pool strategy instead`, ); @@ -408,6 +407,16 @@ export class Configuration { ConfigurationSection.worker, 'Not publicly exposed to end users', ); + if ( + Configuration.getConfigurationData()?.worker?.processType === + ('staticPool' as WorkerProcessType) + ) { + console.error( + `${chalk.green(configurationLogPrefix())} ${chalk.red( + `Deprecated configuration 'staticPool' value usage in worker section 'processType' field. Use '${WorkerProcessType.fixedPool}' value instead`, + )}`, + ); + } // log section Configuration.warnDeprecatedConfigurationKey( 'logEnabled', @@ -468,7 +477,7 @@ export class Configuration { // uiServer section if (hasOwnProp(Configuration.getConfigurationData(), 'uiWebSocketServer')) { console.error( - `${chalk.green(Configuration.logPrefix())} ${chalk.red( + `${chalk.green(configurationLogPrefix())} ${chalk.red( `Deprecated configuration section 'uiWebSocketServer' usage. Use '${ConfigurationSection.uiServer}' instead`, )}`, ); @@ -495,7 +504,7 @@ export class Configuration { ) ) { console.error( - `${chalk.green(Configuration.logPrefix())} ${chalk.red( + `${chalk.green(configurationLogPrefix())} ${chalk.red( `Deprecated configuration key '${key}' usage in section '${sectionName}'${ logMsgToAppend.trim().length > 0 ? `. ${logMsgToAppend}` : '' }`, @@ -505,7 +514,7 @@ export class Configuration { !isUndefined(Configuration.getConfigurationData()?.[key as keyof ConfigurationData]) ) { console.error( - `${chalk.green(Configuration.logPrefix())} ${chalk.red( + `${chalk.green(configurationLogPrefix())} ${chalk.red( `Deprecated configuration key '${key}' usage${ logMsgToAppend.trim().length > 0 ? `. ${logMsgToAppend}` : '' }`, @@ -528,7 +537,7 @@ export class Configuration { Configuration.configurationFile, FileType.Configuration, error as NodeJS.ErrnoException, - Configuration.logPrefix(), + configurationLogPrefix(), ); } } @@ -538,13 +547,30 @@ export class Configuration { private static getConfigurationFileWatcher(): FSWatcher | undefined { try { return watch(Configuration.configurationFile, (event, filename): void => { - if (filename!.trim()!.length > 0 && event === 'change') { + 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`, + )}`, + ); 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; + }); + } else { + Configuration.configurationFileReloading = false; } } }); @@ -553,7 +579,7 @@ export class Configuration { Configuration.configurationFile, FileType.Configuration, error as NodeJS.ErrnoException, - Configuration.logPrefix(), + configurationLogPrefix(), ); } } @@ -568,21 +594,21 @@ export class Configuration { let logMsg: string; switch (error.code) { case 'ENOENT': - logMsg = `${fileType} file ${file} not found:`; + logMsg = `${fileType} file ${file} not found: `; break; case 'EEXIST': - logMsg = `${fileType} file ${file} already exists:`; + logMsg = `${fileType} file ${file} already exists: `; break; case 'EACCES': - logMsg = `${fileType} file ${file} access denied:`; + logMsg = `${fileType} file ${file} access denied: `; break; case 'EPERM': - logMsg = `${fileType} file ${file} permission denied:`; + logMsg = `${fileType} file ${file} permission denied: `; break; default: - logMsg = `${fileType} file ${file} error:`; + logMsg = `${fileType} file ${file} error: `; } - console.error(`${chalk.green(prefix)}${chalk.red(`${logMsg} `)}`, error); + console.error(`${chalk.green(prefix)}${chalk.red(logMsg)}`, error); throw error; }