From: Jérôme Benoit Date: Sat, 5 Mar 2022 19:57:53 +0000 (+0100) Subject: Factor out worker related constants X-Git-Tag: v1.1.52~20 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=3fa0f0edb5e3bb9fff2b343fb4ad7fc6c7c8df34;p=e-mobility-charging-stations-simulator.git Factor out worker related constants Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ChargingStationWorker.ts b/src/charging-station/ChargingStationWorker.ts index 84ce450e..de0a47fe 100644 --- a/src/charging-station/ChargingStationWorker.ts +++ b/src/charging-station/ChargingStationWorker.ts @@ -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(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, }); } diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index 57ee31df..0120fdba 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -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) { diff --git a/src/utils/Constants.ts b/src/utils/Constants.ts index 79e39009..da41eaaf 100644 --- a/src/utils/Constants.ts +++ b/src/utils/Constants.ts @@ -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 diff --git a/src/worker/WorkerAbstract.ts b/src/worker/WorkerAbstract.ts index c3e0ed88..bfb79336 100644 --- a/src/worker/WorkerAbstract.ts +++ b/src/worker/WorkerAbstract.ts @@ -1,6 +1,6 @@ import { WorkerData, WorkerOptions } from '../types/Worker'; -import Constants from '../utils/Constants'; +import WorkerConstants from './WorkerConstants'; export default abstract class WorkerAbstract { protected readonly workerScript: string; @@ -17,11 +17,11 @@ export default abstract class WorkerAbstract { 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 */ diff --git a/src/worker/WorkerFactory.ts b/src/worker/WorkerFactory.ts index 1c4b81c8..b297f42b 100644 --- a/src/worker/WorkerFactory.ts +++ b/src/worker/WorkerFactory.ts @@ -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); workerOptions?.messageHandler && + // eslint-disable-next-line @typescript-eslint/no-misused-promises (workerOptions.poolOptions.messageHandler = workerOptions.messageHandler); let workerImplementation: WorkerAbstract = 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;