Factor out worker related constants
authorJérôme Benoit <jerome.benoit@sap.com>
Sat, 5 Mar 2022 19:57:53 +0000 (20:57 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Sat, 5 Mar 2022 19:57:53 +0000 (20:57 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStationWorker.ts
src/utils/Configuration.ts
src/utils/Constants.ts
src/worker/WorkerAbstract.ts
src/worker/WorkerFactory.ts

index 84ce450eb2cfa65784aa70da7165aab0e8b43df1..de0a47fe5d5745fd29b477ac072c046024922686 100644 (file)
@@ -8,15 +8,15 @@ import {
 import { parentPort, workerData } from 'worker_threads';
 
 import ChargingStation from './ChargingStation';
-import Constants from '../utils/Constants';
 import { ThreadWorker } from 'poolifier';
 import Utils from '../utils/Utils';
+import WorkerConstants from '../worker/WorkerConstants';
 
 // Conditionally export ThreadWorker instance for pool usage
 export let threadWorker: ThreadWorker;
 if (Utils.workerPoolInUse()) {
   threadWorker = new ThreadWorker<ChargingStationWorkerData>(startChargingStation, {
-    maxInactiveTime: Constants.WORKER_POOL_MAX_INACTIVE_TIME,
+    maxInactiveTime: WorkerConstants.POOL_MAX_INACTIVE_TIME,
     async: false,
   });
 } else {
@@ -24,7 +24,9 @@ if (Utils.workerPoolInUse()) {
   addMessageListener();
   if (!Utils.isUndefined(workerData)) {
     startChargingStation({
+      // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
       index: workerData.index as number,
+      // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
       templateFile: workerData.templateFile as string,
     });
   }
index 57ee31df0c355f0ddc6dd736d35a402ae9e70cb6..0120fdbaadb102855f8d289bd0d6d155b6546911 100644 (file)
@@ -11,6 +11,7 @@ import { HandleErrorParams } from '../types/Error';
 import { ServerOptions } from 'ws';
 import { StorageType } from '../types/Storage';
 import type { WorkerChoiceStrategy } from 'poolifier';
+import WorkerConstants from '../worker/WorkerConstants';
 import { WorkerProcessType } from '../types/Worker';
 import chalk from 'chalk';
 import fs from 'fs';
@@ -170,19 +171,19 @@ export default class Configuration {
   static getWorkerStartDelay(): number {
     return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerStartDelay')
       ? Configuration.getConfig().workerStartDelay
-      : Constants.WORKER_START_DELAY;
+      : WorkerConstants.DEFAULT_WORKER_START_DELAY;
   }
 
   static getElementStartDelay(): number {
     return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'elementStartDelay')
       ? Configuration.getConfig().elementStartDelay
-      : Constants.ELEMENT_START_DELAY;
+      : WorkerConstants.DEFAULT_ELEMENT_START_DELAY;
   }
 
   static getWorkerPoolMinSize(): number {
     return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerPoolMinSize')
       ? Configuration.getConfig().workerPoolMinSize
-      : Constants.DEFAULT_WORKER_POOL_MIN_SIZE;
+      : WorkerConstants.DEFAULT_POOL_MIN_SIZE;
   }
 
   static getWorkerPoolMaxSize(): number {
@@ -193,7 +194,7 @@ export default class Configuration {
     );
     return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerPoolMaxSize')
       ? Configuration.getConfig().workerPoolMaxSize
-      : Constants.DEFAULT_WORKER_POOL_MAX_SIZE;
+      : WorkerConstants.DEFAULT_POOL_MAX_SIZE;
   }
 
   static getWorkerPoolStrategy(): WorkerChoiceStrategy {
@@ -206,7 +207,7 @@ export default class Configuration {
       'chargingStationsPerWorker'
     )
       ? Configuration.getConfig().chargingStationsPerWorker
-      : Constants.DEFAULT_CHARGING_STATIONS_PER_WORKER;
+      : WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER;
   }
 
   static getLogConsole(): boolean {
@@ -299,6 +300,7 @@ export default class Configuration {
     if (
       sectionName &&
       !Configuration.isUndefined(Configuration.getConfig()[sectionName]) &&
+      // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
       !Configuration.isUndefined(Configuration.getConfig()[sectionName][key])
     ) {
       console.error(
@@ -327,7 +329,7 @@ export default class Configuration {
           Configuration.logPrefix(),
           'Configuration',
           Configuration.configurationFilePath,
-          error
+          error as NodeJS.ErrnoException
         );
       }
       if (!Configuration.configurationFileWatcher) {
index 79e39009b851b33233c727530f07838662407898..da41eaaf9188d951c235e1e299ace0ff5d3874e3 100644 (file)
@@ -93,13 +93,6 @@ export default class Constants {
 
   static readonly DEFAULT_IDTAG = '00000000';
 
-  static readonly ELEMENT_START_DELAY = 0;
-  static readonly WORKER_START_DELAY = 500;
-  static readonly WORKER_POOL_MAX_INACTIVE_TIME = 60000;
-  static readonly DEFAULT_WORKER_POOL_MIN_SIZE = 4;
-  static readonly DEFAULT_WORKER_POOL_MAX_SIZE = 16;
-  static readonly DEFAULT_CHARGING_STATIONS_PER_WORKER = 1;
-
   static readonly DEFAULT_CONNECTION_TIMEOUT = 30;
 
   static readonly DEFAULT_HEARTBEAT_INTERVAL = 60000; // Ms
index c3e0ed88e9ae6fa4581b6e99b65bffa20931a546..bfb7933639eb1e3445e6bd71cafeef12807f2b3e 100644 (file)
@@ -1,6 +1,6 @@
 import { WorkerData, WorkerOptions } from '../types/Worker';
 
-import Constants from '../utils/Constants';
+import WorkerConstants from './WorkerConstants';
 
 export default abstract class WorkerAbstract<T extends WorkerData> {
   protected readonly workerScript: string;
@@ -17,11 +17,11 @@ export default abstract class WorkerAbstract<T extends WorkerData> {
   constructor(
     workerScript: string,
     workerOptions: WorkerOptions = {
-      workerStartDelay: Constants.WORKER_START_DELAY,
-      elementStartDelay: Constants.ELEMENT_START_DELAY,
-      poolMinSize: Constants.DEFAULT_WORKER_POOL_MIN_SIZE,
-      poolMaxSize: Constants.DEFAULT_WORKER_POOL_MAX_SIZE,
-      elementsPerWorker: Constants.DEFAULT_CHARGING_STATIONS_PER_WORKER,
+      workerStartDelay: WorkerConstants.DEFAULT_WORKER_START_DELAY,
+      elementStartDelay: WorkerConstants.DEFAULT_ELEMENT_START_DELAY,
+      poolMinSize: WorkerConstants.DEFAULT_POOL_MIN_SIZE,
+      poolMaxSize: WorkerConstants.DEFAULT_POOL_MAX_SIZE,
+      elementsPerWorker: WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER,
       poolOptions: {},
       messageHandler: () => {
         /* This is intentional */
index 1c4b81c8ad6ab5d7c0e35a9b89e46d39b4f19c7e..b297f42b231715643ef14761ca1c45a93295a5cf 100644 (file)
@@ -1,9 +1,9 @@
 import { Worker, isMainThread } from 'worker_threads';
 import { WorkerData, WorkerOptions, WorkerProcessType } from '../types/Worker';
 
-import Constants from '../utils/Constants';
 import { PoolOptions } from 'poolifier';
 import type WorkerAbstract from './WorkerAbstract';
+import WorkerConstants from './WorkerConstants';
 import WorkerDynamicPool from './WorkerDynamicPool';
 import WorkerSet from './WorkerSet';
 import WorkerStaticPool from './WorkerStaticPool';
@@ -23,32 +23,34 @@ export default class WorkerFactory {
     }
     workerOptions = workerOptions ?? ({} as WorkerOptions);
     workerOptions.workerStartDelay =
-      workerOptions?.workerStartDelay ?? Constants.WORKER_START_DELAY;
+      workerOptions?.workerStartDelay ?? WorkerConstants.DEFAULT_WORKER_START_DELAY;
     workerOptions.elementStartDelay =
-      workerOptions?.elementStartDelay ?? Constants.ELEMENT_START_DELAY;
+      workerOptions?.elementStartDelay ?? WorkerConstants.DEFAULT_ELEMENT_START_DELAY;
     workerOptions.poolOptions = workerOptions?.poolOptions ?? ({} as PoolOptions<Worker>);
     workerOptions?.messageHandler &&
+      // eslint-disable-next-line @typescript-eslint/no-misused-promises
       (workerOptions.poolOptions.messageHandler = workerOptions.messageHandler);
     let workerImplementation: WorkerAbstract<T> = null;
     switch (workerProcessType) {
       case WorkerProcessType.WORKER_SET:
         workerOptions.elementsPerWorker =
-          workerOptions?.elementsPerWorker ?? Constants.DEFAULT_CHARGING_STATIONS_PER_WORKER;
+          workerOptions?.elementsPerWorker ?? WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER;
         workerImplementation = new WorkerSet(workerScript, workerOptions);
         break;
       case WorkerProcessType.STATIC_POOL:
         workerOptions.poolMaxSize =
-          workerOptions?.poolMaxSize ?? Constants.DEFAULT_WORKER_POOL_MAX_SIZE;
+          workerOptions?.poolMaxSize ?? WorkerConstants.DEFAULT_POOL_MAX_SIZE;
         workerImplementation = new WorkerStaticPool(workerScript, workerOptions);
         break;
       case WorkerProcessType.DYNAMIC_POOL:
         workerOptions.poolMinSize =
-          workerOptions?.poolMinSize ?? Constants.DEFAULT_WORKER_POOL_MIN_SIZE;
+          workerOptions?.poolMinSize ?? WorkerConstants.DEFAULT_POOL_MIN_SIZE;
         workerOptions.poolMaxSize =
-          workerOptions?.poolMaxSize ?? Constants.DEFAULT_WORKER_POOL_MAX_SIZE;
+          workerOptions?.poolMaxSize ?? WorkerConstants.DEFAULT_POOL_MAX_SIZE;
         workerImplementation = new WorkerDynamicPool(workerScript, workerOptions);
         break;
       default:
+        // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
         throw new Error(`Worker implementation type '${workerProcessType}' not found`);
     }
     return workerImplementation;