From 769d3b106c5e4744487bc633da4e4ee93f8f1bf4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 30 Jul 2023 13:44:03 +0200 Subject: [PATCH] refactor: split WorkerConstants class MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/ChargingStationWorker.ts | 4 +- src/utils/Configuration.ts | 16 ++++--- src/worker/WorkerConstants.ts | 42 ++++++++++++------- src/worker/WorkerFactory.ts | 11 +---- src/worker/WorkerSet.ts | 31 +++----------- src/worker/index.ts | 8 +++- 6 files changed, 53 insertions(+), 59 deletions(-) diff --git a/src/charging-station/ChargingStationWorker.ts b/src/charging-station/ChargingStationWorker.ts index db7ae2cb..ff9dbe62 100644 --- a/src/charging-station/ChargingStationWorker.ts +++ b/src/charging-station/ChargingStationWorker.ts @@ -8,7 +8,7 @@ import { ThreadWorker } from 'poolifier'; import { ChargingStation } from './ChargingStation'; import type { ChargingStationWorkerData } from '../types'; import { Configuration } from '../utils'; -import { WorkerConstants, type WorkerMessage, WorkerMessageEvents } from '../worker'; +import { POOL_MAX_INACTIVE_TIME, type WorkerMessage, WorkerMessageEvents } from '../worker'; const moduleName = 'ChargingStationWorker'; @@ -63,7 +63,7 @@ class ChargingStationWorker extends AsyncResource { export let chargingStationWorker: ChargingStationWorker | ThreadWorker; if (Configuration.workerPoolInUse()) { chargingStationWorker = new ThreadWorker(startChargingStation, { - maxInactiveTime: WorkerConstants.POOL_MAX_INACTIVE_TIME, + maxInactiveTime: POOL_MAX_INACTIVE_TIME, }); } else { chargingStationWorker = new ChargingStationWorker(); diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index f01decea..ad8d328d 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -20,7 +20,13 @@ import { type UIServerConfiguration, type WorkerConfiguration, } from '../types'; -import { WorkerConstants, WorkerProcessType } from '../worker'; +import { + DEFAULT_ELEMENT_START_DELAY, + DEFAULT_POOL_MAX_SIZE, + DEFAULT_POOL_MIN_SIZE, + DEFAULT_WORKER_START_DELAY, + WorkerProcessType, +} from '../worker'; type ConfigurationSectionType = | LogConfiguration @@ -401,11 +407,11 @@ export class Configuration { ); const defaultWorkerConfiguration: WorkerConfiguration = { processType: WorkerProcessType.workerSet, - startDelay: WorkerConstants.DEFAULT_WORKER_START_DELAY, + startDelay: DEFAULT_WORKER_START_DELAY, elementsPerWorker: 'auto', - elementStartDelay: WorkerConstants.DEFAULT_ELEMENT_START_DELAY, - poolMinSize: WorkerConstants.DEFAULT_POOL_MIN_SIZE, - poolMaxSize: WorkerConstants.DEFAULT_POOL_MAX_SIZE, + elementStartDelay: DEFAULT_ELEMENT_START_DELAY, + poolMinSize: DEFAULT_POOL_MIN_SIZE, + poolMaxSize: DEFAULT_POOL_MAX_SIZE, }; hasOwnProp(Configuration.getConfigurationData(), 'workerPoolStrategy') && delete Configuration.getConfigurationData()?.workerPoolStrategy; diff --git a/src/worker/WorkerConstants.ts b/src/worker/WorkerConstants.ts index dcbe2ca4..bbfcdac4 100644 --- a/src/worker/WorkerConstants.ts +++ b/src/worker/WorkerConstants.ts @@ -1,20 +1,30 @@ -import { availableParallelism } from 'poolifier'; +import { type ThreadPoolOptions, availableParallelism } from 'poolifier'; -export class WorkerConstants { - public static readonly EMPTY_FUNCTION = Object.freeze(() => { - /* This is intentional */ - }); +import type { WorkerOptions } from './WorkerTypes'; - public static readonly DEFAULT_ELEMENT_START_DELAY = 0; - public static readonly DEFAULT_WORKER_START_DELAY = 500; - public static readonly POOL_MAX_INACTIVE_TIME = 60000; - public static readonly DEFAULT_POOL_MIN_SIZE = Math.floor(availableParallelism() / 2); - public static readonly DEFAULT_POOL_MAX_SIZE = Math.round(availableParallelism() * 1.5); - public static readonly DEFAULT_ELEMENTS_PER_WORKER = 1; +export const EMPTY_FUNCTION = Object.freeze(() => { + /* This is intentional */ +}); - public static readonly version = '1.0.1'; +export const workerSetVersion = '1.0.1'; - private constructor() { - // This is intentional - } -} +export const DEFAULT_ELEMENT_START_DELAY = 0; +export const DEFAULT_WORKER_START_DELAY = 500; +export const POOL_MAX_INACTIVE_TIME = 60000; +export const DEFAULT_POOL_MIN_SIZE = Math.floor(availableParallelism() / 2); +export const DEFAULT_POOL_MAX_SIZE = Math.round(availableParallelism() * 1.5); +export const DEFAULT_ELEMENTS_PER_WORKER = 1; + +export const DEFAULT_WORKER_OPTIONS: WorkerOptions = Object.freeze({ + workerStartDelay: DEFAULT_WORKER_START_DELAY, + elementStartDelay: DEFAULT_ELEMENT_START_DELAY, + poolMinSize: DEFAULT_POOL_MIN_SIZE, + poolMaxSize: DEFAULT_POOL_MAX_SIZE, + elementsPerWorker: DEFAULT_ELEMENTS_PER_WORKER, + poolOptions: {}, +}); + +export const DEFAULT_POOL_OPTIONS: ThreadPoolOptions = { + enableEvents: true, + restartWorkerOnError: true, +}; diff --git a/src/worker/WorkerFactory.ts b/src/worker/WorkerFactory.ts index ae27d1df..59fbf3a4 100644 --- a/src/worker/WorkerFactory.ts +++ b/src/worker/WorkerFactory.ts @@ -1,21 +1,12 @@ import { isMainThread } from 'node:worker_threads'; import type { WorkerAbstract } from './WorkerAbstract'; -import { WorkerConstants } from './WorkerConstants'; +import { DEFAULT_WORKER_OPTIONS } from './WorkerConstants'; import { WorkerDynamicPool } from './WorkerDynamicPool'; import { WorkerSet } from './WorkerSet'; import { WorkerStaticPool } from './WorkerStaticPool'; import { type WorkerData, type WorkerOptions, WorkerProcessType } from './WorkerTypes'; -const DEFAULT_WORKER_OPTIONS: WorkerOptions = { - 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: {}, -}; - export class WorkerFactory { private constructor() { // This is intentional diff --git a/src/worker/WorkerSet.ts b/src/worker/WorkerSet.ts index e0730678..c1fecdce 100644 --- a/src/worker/WorkerSet.ts +++ b/src/worker/WorkerSet.ts @@ -3,10 +3,8 @@ import { EventEmitter } from 'node:events'; import { SHARE_ENV, Worker } from 'node:worker_threads'; -import type { ThreadPoolOptions } from 'poolifier'; - import { WorkerAbstract } from './WorkerAbstract'; -import { WorkerConstants } from './WorkerConstants'; +import { DEFAULT_POOL_OPTIONS, EMPTY_FUNCTION, workerSetVersion } from './WorkerConstants'; import { type SetInfo, type WorkerData, @@ -18,11 +16,6 @@ import { } from './WorkerTypes'; import { sleep } from './WorkerUtils'; -const DEFAULT_POOL_OPTIONS: ThreadPoolOptions = { - enableEvents: true, - restartWorkerOnError: true, -}; - export class WorkerSet extends WorkerAbstract { public readonly emitter!: EventEmitter; private readonly workerSet: Set; @@ -59,7 +52,7 @@ export class WorkerSet extends WorkerAbstract { get info(): SetInfo { return { - version: WorkerConstants.version, + version: workerSetVersion, type: 'set', worker: 'thread', size: this.size, @@ -125,10 +118,7 @@ export class WorkerSet extends WorkerAbstract { env: SHARE_ENV, ...this.workerOptions.poolOptions?.workerOptions, }); - worker.on( - 'message', - this.workerOptions.poolOptions?.messageHandler ?? WorkerConstants.EMPTY_FUNCTION, - ); + worker.on('message', this.workerOptions.poolOptions?.messageHandler ?? EMPTY_FUNCTION); worker.on('message', (message: WorkerMessage) => { if (message.event === WorkerMessageEvents.startedWorkerElement) { this.emitter?.emit(WorkerSetEvents.elementStarted, this.info); @@ -136,24 +126,15 @@ export class WorkerSet extends WorkerAbstract { this.emitter?.emit(WorkerSetEvents.elementError, message.data); } }); - worker.on( - 'error', - this.workerOptions.poolOptions?.errorHandler ?? WorkerConstants.EMPTY_FUNCTION, - ); + worker.on('error', this.workerOptions.poolOptions?.errorHandler ?? EMPTY_FUNCTION); worker.on('error', (error) => { this.emitter?.emit(WorkerSetEvents.error, error); if (this.workerOptions.poolOptions?.restartWorkerOnError) { this.addWorkerSetElement(); } }); - worker.on( - 'online', - this.workerOptions.poolOptions?.onlineHandler ?? WorkerConstants.EMPTY_FUNCTION, - ); - worker.on( - 'exit', - this.workerOptions.poolOptions?.exitHandler ?? WorkerConstants.EMPTY_FUNCTION, - ); + worker.on('online', this.workerOptions.poolOptions?.onlineHandler ?? EMPTY_FUNCTION); + worker.on('exit', this.workerOptions.poolOptions?.exitHandler ?? EMPTY_FUNCTION); worker.once('exit', () => this.removeWorkerSetElement(this.getWorkerSetElementByWorker(worker)!), ); diff --git a/src/worker/index.ts b/src/worker/index.ts index 50848d09..e53c3457 100644 --- a/src/worker/index.ts +++ b/src/worker/index.ts @@ -1,5 +1,11 @@ export type { WorkerAbstract } from './WorkerAbstract'; -export { WorkerConstants } from './WorkerConstants'; +export { + DEFAULT_ELEMENT_START_DELAY, + DEFAULT_POOL_MAX_SIZE, + DEFAULT_POOL_MIN_SIZE, + DEFAULT_WORKER_START_DELAY, + POOL_MAX_INACTIVE_TIME, +} from './WorkerConstants'; export { WorkerFactory } from './WorkerFactory'; export { type WorkerData, -- 2.34.1