Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
Factor out worker related constants
Browse files Browse the repository at this point in the history
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
  • Loading branch information
Jérôme Benoit committed Mar 5, 2022
1 parent 2a5e097 commit 3fa0f0e
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 28 deletions.
6 changes: 4 additions & 2 deletions src/charging-station/ChargingStationWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,25 @@ 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 {
// Add message listener to start charging station from main thread
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,
});
}
Expand Down
14 changes: 8 additions & 6 deletions src/utils/Configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -327,7 +329,7 @@ export default class Configuration {
Configuration.logPrefix(),
'Configuration',
Configuration.configurationFilePath,
error
error as NodeJS.ErrnoException
);
}
if (!Configuration.configurationFileWatcher) {
Expand Down
7 changes: 0 additions & 7 deletions src/utils/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions src/worker/WorkerAbstract.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 */
Expand Down
16 changes: 9 additions & 7 deletions src/worker/WorkerFactory.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
Expand Down

0 comments on commit 3fa0f0e

Please sign in to comment.