Add worker configuration section
[e-mobility-charging-stations-simulator.git] / src / utils / Configuration.ts
index 716e53095b8f7d76a4572751b7d04b9fc428d017..3fec05a882ba2803339307c83a9edbcf625ea9f2 100644 (file)
@@ -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 {