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 {
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,
});
}
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';
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 {
);
return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerPoolMaxSize')
? Configuration.getConfig().workerPoolMaxSize
- : Constants.DEFAULT_WORKER_POOL_MAX_SIZE;
+ : WorkerConstants.DEFAULT_POOL_MAX_SIZE;
}
static getWorkerPoolStrategy(): WorkerChoiceStrategy {
'chargingStationsPerWorker'
)
? Configuration.getConfig().chargingStationsPerWorker
- : Constants.DEFAULT_CHARGING_STATIONS_PER_WORKER;
+ : WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER;
}
static getLogConsole(): boolean {
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(
Configuration.logPrefix(),
'Configuration',
Configuration.configurationFilePath,
- error
+ error as NodeJS.ErrnoException
);
}
if (!Configuration.configurationFileWatcher) {
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
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;
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 */
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';
}
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;