From aa7d6d9568ce5ec481a0a230f4ae24c9ee9d44fa Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 20 May 2023 21:59:36 +0200 Subject: [PATCH] refactor(simulator): move configuration related helpers MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/Bootstrap.ts | 5 +-- src/charging-station/ChargingStationUtils.ts | 20 +-------- src/charging-station/ChargingStationWorker.ts | 5 +-- src/utils/Configuration.ts | 44 ++++++++++++------- 4 files changed, 32 insertions(+), 42 deletions(-) diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index c94a9204..33122242 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -6,7 +6,6 @@ import { type Worker, isMainThread } from 'node:worker_threads'; import chalk from 'chalk'; -import { ChargingStationUtils } from './ChargingStationUtils'; import type { AbstractUIServer } from './ui-server/AbstractUIServer'; import { UIServerFactory } from './ui-server/UIServerFactory'; import packageJson from '../../package.json' assert { type: 'json' }; @@ -102,11 +101,11 @@ export class Bootstrap { `Charging stations simulator ${ this.version } started with ${this.numberOfChargingStations.toString()} charging station(s) from ${this.numberOfChargingStationTemplates.toString()} configured charging station template(s) and ${ - ChargingStationUtils.workerDynamicPoolInUse() + Configuration.workerDynamicPoolInUse() ? `${Configuration.getWorker().poolMinSize?.toString()}/` : '' }${this.workerImplementation?.size}${ - ChargingStationUtils.workerPoolInUse() + Configuration.workerPoolInUse() ? `/${Configuration.getWorker().poolMaxSize?.toString()}` : '' } worker(s) concurrently running in '${Configuration.getWorker().processType}' mode${ diff --git a/src/charging-station/ChargingStationUtils.ts b/src/charging-station/ChargingStationUtils.ts index 93e34ec6..e76b3a6e 100644 --- a/src/charging-station/ChargingStationUtils.ts +++ b/src/charging-station/ChargingStationUtils.ts @@ -29,15 +29,7 @@ import { RecurrencyKindType, Voltage, } from '../types'; -import { - ACElectricUtils, - Configuration, - Constants, - DCElectricUtils, - Utils, - logger, -} from '../utils'; -import { WorkerProcessType } from '../worker'; +import { ACElectricUtils, Constants, DCElectricUtils, Utils, logger } from '../utils'; const moduleName = 'ChargingStationUtils'; @@ -351,16 +343,6 @@ export class ChargingStationUtils { } } - public static workerPoolInUse(): boolean { - return [WorkerProcessType.dynamicPool, WorkerProcessType.staticPool].includes( - Configuration.getWorker().processType - ); - } - - public static workerDynamicPoolInUse(): boolean { - return Configuration.getWorker().processType === WorkerProcessType.dynamicPool; - } - public static warnTemplateKeysDeprecation( stationTemplate: ChargingStationTemplate, logPrefix: string, diff --git a/src/charging-station/ChargingStationWorker.ts b/src/charging-station/ChargingStationWorker.ts index c4ff06ea..0ae95a61 100644 --- a/src/charging-station/ChargingStationWorker.ts +++ b/src/charging-station/ChargingStationWorker.ts @@ -5,9 +5,8 @@ import { parentPort, workerData } from 'node:worker_threads'; import { ThreadWorker } from 'poolifier'; import { ChargingStation } from './ChargingStation'; -import { ChargingStationUtils } from './ChargingStationUtils'; import type { ChargingStationWorkerData } from '../types'; -import { Utils } from '../utils'; +import { Configuration, Utils } from '../utils'; import { WorkerConstants, type WorkerMessage, WorkerMessageEvents } from '../worker'; /** @@ -33,7 +32,7 @@ const addMessageListener = (): void => { // Conditionally export ThreadWorker instance for pool usage export let threadWorker: ThreadWorker; -if (ChargingStationUtils.workerPoolInUse()) { +if (Configuration.workerPoolInUse()) { threadWorker = new ThreadWorker(startChargingStation, { maxInactiveTime: WorkerConstants.POOL_MAX_INACTIVE_TIME, async: false, diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index 257f5f65..21426070 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -36,11 +36,11 @@ export class Configuration { // This is intentional } - static setConfigurationChangeCallback(cb: () => Promise): void { + public static setConfigurationChangeCallback(cb: () => Promise): void { Configuration.configurationChangeCallback = cb; } - static getLogStatisticsInterval(): number | undefined { + public static getLogStatisticsInterval(): number | undefined { Configuration.warnDeprecatedConfigurationKey( 'statisticsDisplayInterval', undefined, @@ -52,7 +52,7 @@ export class Configuration { : Constants.DEFAULT_LOG_STATISTICS_INTERVAL; } - static getUIServer(): UIServerConfiguration { + public static getUIServer(): UIServerConfiguration { if (Utils.hasOwnProp(Configuration.getConfig(), 'uiWebSocketServer')) { console.error( chalk`{green ${Configuration.logPrefix()}} {red Deprecated configuration section 'uiWebSocketServer' usage. Use 'uiServer' instead}` @@ -79,7 +79,7 @@ export class Configuration { return uiServerConfiguration; } - static getPerformanceStorage(): StorageConfiguration { + public static getPerformanceStorage(): StorageConfiguration { Configuration.warnDeprecatedConfigurationKey('URI', 'performanceStorage', "Use 'uri' instead"); let storageConfiguration: StorageConfiguration = { enabled: false, @@ -101,7 +101,7 @@ export class Configuration { return storageConfiguration; } - static getAutoReconnectMaxRetries(): number | undefined { + public static getAutoReconnectMaxRetries(): number | undefined { Configuration.warnDeprecatedConfigurationKey( 'autoReconnectTimeout', undefined, @@ -123,7 +123,7 @@ export class Configuration { } } - static getStationTemplateUrls(): StationTemplateUrl[] | undefined { + public static getStationTemplateUrls(): StationTemplateUrl[] | undefined { Configuration.warnDeprecatedConfigurationKey( 'stationTemplateURLs', undefined, @@ -148,7 +148,7 @@ export class Configuration { return Configuration.getConfig()?.stationTemplateUrls; } - static getWorker(): WorkerConfiguration { + public static getWorker(): WorkerConfiguration { Configuration.warnDeprecatedConfigurationKey( 'useWorkerPool', undefined, @@ -222,7 +222,17 @@ export class Configuration { return workerConfiguration; } - static getLogConsole(): boolean | undefined { + public static workerPoolInUse(): boolean { + return [WorkerProcessType.dynamicPool, WorkerProcessType.staticPool].includes( + Configuration.getWorker().processType + ); + } + + public static workerDynamicPoolInUse(): boolean { + return Configuration.getWorker().processType === WorkerProcessType.dynamicPool; + } + + public static getLogConsole(): boolean | undefined { Configuration.warnDeprecatedConfigurationKey( 'consoleLog', undefined, @@ -233,45 +243,45 @@ export class Configuration { : false; } - static getLogFormat(): string | undefined { + public static getLogFormat(): string | undefined { return Utils.hasOwnProp(Configuration.getConfig(), 'logFormat') ? Configuration.getConfig()?.logFormat : 'simple'; } - static getLogRotate(): boolean | undefined { + public static getLogRotate(): boolean | undefined { return Utils.hasOwnProp(Configuration.getConfig(), 'logRotate') ? Configuration.getConfig()?.logRotate : true; } - static getLogMaxFiles(): number | string | false | undefined { + public static getLogMaxFiles(): number | string | false | undefined { return ( Utils.hasOwnProp(Configuration.getConfig(), 'logMaxFiles') && Configuration.getConfig()?.logMaxFiles ); } - static getLogMaxSize(): number | string | false | undefined { + public static getLogMaxSize(): number | string | false | undefined { return ( Utils.hasOwnProp(Configuration.getConfig(), 'logMaxFiles') && Configuration.getConfig()?.logMaxSize ); } - static getLogLevel(): string | undefined { + public static getLogLevel(): string | undefined { return Utils.hasOwnProp(Configuration.getConfig(), 'logLevel') ? Configuration.getConfig()?.logLevel?.toLowerCase() : 'info'; } - static getLogFile(): string | undefined { + public static getLogFile(): string | undefined { return Utils.hasOwnProp(Configuration.getConfig(), 'logFile') ? Configuration.getConfig()?.logFile : 'combined.log'; } - static getLogErrorFile(): string | undefined { + public static getLogErrorFile(): string | undefined { Configuration.warnDeprecatedConfigurationKey( 'errorFile', undefined, @@ -282,7 +292,7 @@ export class Configuration { : 'error.log'; } - static getSupervisionUrls(): string | string[] | undefined { + public static getSupervisionUrls(): string | string[] | undefined { Configuration.warnDeprecatedConfigurationKey( 'supervisionURLs', undefined, @@ -296,7 +306,7 @@ export class Configuration { return Configuration.getConfig()?.supervisionUrls; } - static getSupervisionUrlDistribution(): SupervisionUrlDistribution | undefined { + public static getSupervisionUrlDistribution(): SupervisionUrlDistribution | undefined { Configuration.warnDeprecatedConfigurationKey( 'distributeStationToTenantEqually', undefined, -- 2.34.1