From cf2a5d9b1e3f929ef9805d3fb7e4022cf4f9632d Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 3 Jul 2022 00:29:39 +0200 Subject: [PATCH] Add worker configuration section MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Close #35 Signed-off-by: Jérôme Benoit --- src/assets/config-template.json | 10 +- src/charging-station/Bootstrap.ts | 20 ++-- src/charging-station/ChargingStationUtils.ts | 4 +- src/types/ConfigurationData.ts | 18 +++ src/utils/Configuration.ts | 120 ++++++++++++------- 5 files changed, 112 insertions(+), 60 deletions(-) diff --git a/src/assets/config-template.json b/src/assets/config-template.json index f5595f61..51def291 100644 --- a/src/assets/config-template.json +++ b/src/assets/config-template.json @@ -1,14 +1,16 @@ { "supervisionUrls": ["ws://localhost:8010/OCPP16/5be7fb271014d90008992f06"], "supervisionUrlDistribution": "round-robin", + "worker": { + "processType": "workerSet", + "elementsPerWorker": 1, + "poolMinSize": 4, + "poolMaxSize": 16 + }, "performanceStorage": { "enabled": true, "type": "jsonfile" }, - "chargingStationsPerWorker": 1, - "workerProcess": "workerSet", - "workerPoolMinSize": 4, - "workerPoolMaxSize": 16, "stationTemplateUrls": [ { "file": "siemens.station-template.json", diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index af96523f..ed9f1bef 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -109,13 +109,13 @@ export default class Bootstrap { this.version } started with ${this.numberOfChargingStations.toString()} charging station(s) from ${this.numberOfChargingStationTemplates.toString()} configured charging station template(s) and ${ ChargingStationUtils.workerDynamicPoolInUse() - ? `${Configuration.getWorkerPoolMinSize().toString()}/` + ? `${Configuration.getWorker().poolMinSize.toString()}/` : '' }${this.workerImplementation.size}${ ChargingStationUtils.workerPoolInUse() - ? `/${Configuration.getWorkerPoolMaxSize().toString()}` + ? `/${Configuration.getWorker().poolMaxSize.toString()}` : '' - } worker(s) concurrently running in '${Configuration.getWorkerProcess()}' mode${ + } worker(s) concurrently running in '${Configuration.getWorker().processType}' mode${ this.workerImplementation.maxElementsPerWorker ? ` (${this.workerImplementation.maxElementsPerWorker} charging station(s) per worker)` : '' @@ -154,15 +154,15 @@ export default class Bootstrap { !this.workerImplementation && (this.workerImplementation = WorkerFactory.getWorkerImplementation( this.workerScript, - Configuration.getWorkerProcess(), + Configuration.getWorker().processType, { - workerStartDelay: Configuration.getWorkerStartDelay(), - elementStartDelay: Configuration.getElementStartDelay(), - poolMaxSize: Configuration.getWorkerPoolMaxSize(), - poolMinSize: Configuration.getWorkerPoolMinSize(), - elementsPerWorker: Configuration.getChargingStationsPerWorker(), + workerStartDelay: Configuration.getWorker().startDelay, + elementStartDelay: Configuration.getWorker().elementStartDelay, + poolMaxSize: Configuration.getWorker().poolMaxSize, + poolMinSize: Configuration.getWorker().poolMinSize, + elementsPerWorker: Configuration.getWorker().elementsPerWorker, poolOptions: { - workerChoiceStrategy: Configuration.getWorkerPoolStrategy(), + workerChoiceStrategy: Configuration.getWorker().poolStrategy, }, messageHandler: async (msg: ChargingStationWorkerMessage) => { if (msg.id === ChargingStationWorkerMessageEvents.STARTED) { diff --git a/src/charging-station/ChargingStationUtils.ts b/src/charging-station/ChargingStationUtils.ts index 964c606b..7208fb80 100644 --- a/src/charging-station/ChargingStationUtils.ts +++ b/src/charging-station/ChargingStationUtils.ts @@ -157,12 +157,12 @@ export class ChargingStationUtils { public static workerPoolInUse(): boolean { return [WorkerProcessType.DYNAMIC_POOL, WorkerProcessType.STATIC_POOL].includes( - Configuration.getWorkerProcess() + Configuration.getWorker().processType ); } public static workerDynamicPoolInUse(): boolean { - return Configuration.getWorkerProcess() === WorkerProcessType.DYNAMIC_POOL; + return Configuration.getWorker().processType === WorkerProcessType.DYNAMIC_POOL; } /** diff --git a/src/types/ConfigurationData.ts b/src/types/ConfigurationData.ts index 9aed2f1f..708f411c 100644 --- a/src/types/ConfigurationData.ts +++ b/src/types/ConfigurationData.ts @@ -28,19 +28,37 @@ export interface StorageConfiguration { uri?: string; } +export interface WorkerConfiguration { + processType?: WorkerProcessType; + startDelay?: number; + elementsPerWorker?: number; + elementStartDelay?: number; + poolMinSize?: number; + poolMaxSize?: number; + poolStrategy?: WorkerChoiceStrategy; +} + export default interface ConfigurationData { supervisionUrls?: string | string[]; supervisionUrlDistribution?: SupervisionUrlDistribution; stationTemplateUrls: StationTemplateUrl[]; uiServer?: UIServerConfiguration; performanceStorage?: StorageConfiguration; + worker?: WorkerConfiguration; autoReconnectMaxRetries?: number; + // deprecated workerProcess?: WorkerProcessType; + // deprecated workerStartDelay?: number; + // deprecated elementStartDelay?: number; + // deprecated workerPoolMinSize?: number; + // deprecated workerPoolMaxSize?: number; + // deprecated workerPoolStrategy?: WorkerChoiceStrategy; + // deprecated chargingStationsPerWorker?: number; logStatisticsInterval?: number; logFormat?: string; diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index 716e5309..3fec05a8 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -4,6 +4,7 @@ import ConfigurationData, { StorageConfiguration, SupervisionUrlDistribution, UIServerConfiguration, + WorkerConfiguration, } from '../types/ConfigurationData'; import Constants from './Constants'; @@ -11,7 +12,6 @@ import { EmptyObject } from '../types/EmptyObject'; import { FileType } from '../types/FileType'; import { HandleErrorParams } from '../types/Error'; import { StorageType } from '../types/Storage'; -import type { WorkerChoiceStrategy } from 'poolifier'; import WorkerConstants from '../worker/WorkerConstants'; import { WorkerProcessType } from '../types/Worker'; import chalk from 'chalk'; @@ -161,57 +161,89 @@ export default class Configuration { return Configuration.getConfig().stationTemplateUrls; } - static getWorkerProcess(): WorkerProcessType { + static getWorker(): WorkerConfiguration { Configuration.warnDeprecatedConfigurationKey( 'useWorkerPool', null, - "Use 'workerProcess' to define the type of worker process model to use instead" + "Use 'worker' section to define the type of worker process model instead" + ); + Configuration.warnDeprecatedConfigurationKey( + 'workerProcess', + null, + "Use 'worker' section to define the type of worker process model instead" + ); + Configuration.warnDeprecatedConfigurationKey( + 'workerStartDelay', + null, + "Use 'worker' section to define the worker start delay instead" + ); + Configuration.warnDeprecatedConfigurationKey( + 'chargingStationsPerWorker', + null, + "Use 'worker' section to define the number of element(s) per worker instead" + ); + Configuration.warnDeprecatedConfigurationKey( + 'elementStartDelay', + null, + "Use 'worker' section to define the worker's element start delay instead" + ); + Configuration.warnDeprecatedConfigurationKey( + 'workerPoolMinSize', + null, + "Use 'worker' section to define the worker pool minimum size instead" ); - return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerProcess') - ? Configuration.getConfig().workerProcess - : WorkerProcessType.WORKER_SET; - } - - static getWorkerStartDelay(): number { - return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerStartDelay') - ? Configuration.getConfig().workerStartDelay - : WorkerConstants.DEFAULT_WORKER_START_DELAY; - } - - static getElementStartDelay(): number { - return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'elementStartDelay') - ? Configuration.getConfig().elementStartDelay - : WorkerConstants.DEFAULT_ELEMENT_START_DELAY; - } - - static getWorkerPoolMinSize(): number { - return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerPoolMinSize') - ? Configuration.getConfig().workerPoolMinSize - : WorkerConstants.DEFAULT_POOL_MIN_SIZE; - } - - static getWorkerPoolMaxSize(): number { Configuration.warnDeprecatedConfigurationKey( 'workerPoolSize;', null, - "Use 'workerPoolMaxSize' instead" + "Use 'worker' section to define the worker pool maximum size instead" ); - return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerPoolMaxSize') - ? Configuration.getConfig().workerPoolMaxSize - : WorkerConstants.DEFAULT_POOL_MAX_SIZE; - } - - static getWorkerPoolStrategy(): WorkerChoiceStrategy { - return Configuration.getConfig().workerPoolStrategy; - } - - static getChargingStationsPerWorker(): number { - return Configuration.objectHasOwnProperty( - Configuration.getConfig(), - 'chargingStationsPerWorker' - ) - ? Configuration.getConfig().chargingStationsPerWorker - : WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER; + Configuration.warnDeprecatedConfigurationKey( + 'workerPoolMaxSize;', + null, + "Use 'worker' section to define the worker pool maximum size instead" + ); + Configuration.warnDeprecatedConfigurationKey( + 'workerPoolStrategy;', + null, + "Use 'worker' section to define the worker pool strategy instead" + ); + const workerConfiguration: WorkerConfiguration = { + processType: Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerProcess') + ? Configuration.getConfig().workerProcess + : WorkerProcessType.WORKER_SET, + startDelay: Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerStartDelay') + ? Configuration.getConfig().workerStartDelay + : WorkerConstants.DEFAULT_WORKER_START_DELAY, + elementsPerWorker: Configuration.objectHasOwnProperty( + Configuration.getConfig(), + 'chargingStationsPerWorker' + ) + ? Configuration.getConfig().chargingStationsPerWorker + : WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER, + elementStartDelay: Configuration.objectHasOwnProperty( + Configuration.getConfig(), + 'elementStartDelay' + ) + ? Configuration.getConfig().elementStartDelay + : WorkerConstants.DEFAULT_ELEMENT_START_DELAY, + poolMinSize: Configuration.objectHasOwnProperty( + Configuration.getConfig(), + 'workerPoolMinSize' + ) + ? Configuration.getConfig().workerPoolMinSize + : WorkerConstants.DEFAULT_POOL_MIN_SIZE, + poolMaxSize: Configuration.objectHasOwnProperty( + Configuration.getConfig(), + 'workerPoolMaxSize' + ) + ? Configuration.getConfig().workerPoolMaxSize + : WorkerConstants.DEFAULT_POOL_MAX_SIZE, + poolStrategy: Configuration.getConfig().workerPoolStrategy, + }; + if (Configuration.objectHasOwnProperty(Configuration.getConfig(), 'worker')) { + return { ...workerConfiguration, ...Configuration.getConfig().worker }; + } + return workerConfiguration; } static getLogConsole(): boolean { -- 2.34.1