From 4a3807d16c54137840a60af41877f3b21de12950 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 18 Jul 2023 23:41:29 +0200 Subject: [PATCH] refactor: cleanup default worker options handling MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../ChargingStationWorkerBroadcastChannel.ts | 6 ++--- .../ui-server/UIHttpServer.ts | 2 +- .../ui-server/UIWebSocketServer.ts | 2 +- src/worker/WorkerAbstract.ts | 13 +--------- src/worker/WorkerDynamicPool.ts | 2 +- src/worker/WorkerFactory.ts | 26 +++++++------------ src/worker/WorkerSet.ts | 14 ++++++---- src/worker/WorkerStaticPool.ts | 2 +- 8 files changed, 27 insertions(+), 40 deletions(-) diff --git a/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts b/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts index bb484be7..87ceaca9 100644 --- a/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts +++ b/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts @@ -252,12 +252,12 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne } const [uuid, command, requestPayload] = validatedMessageEvent.data as BroadcastChannelRequest; if ( - !isNullOrUndefined(requestPayload?.hashIds) && - requestPayload?.hashIds?.includes(this.chargingStation.stationInfo.hashId) === false + !isNullOrUndefined(requestPayload.hashIds) && + requestPayload.hashIds?.includes(this.chargingStation.stationInfo.hashId) === false ) { return; } - if (!isNullOrUndefined(requestPayload?.hashId)) { + if (!isNullOrUndefined(requestPayload.hashId)) { logger.error( `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: 'hashId' field usage in PDU is deprecated, use 'hashIds' array instead`, ); diff --git a/src/charging-station/ui-server/UIHttpServer.ts b/src/charging-station/ui-server/UIHttpServer.ts index 103a27ae..ee12b3f4 100644 --- a/src/charging-station/ui-server/UIHttpServer.ts +++ b/src/charging-station/ui-server/UIHttpServer.ts @@ -132,7 +132,7 @@ export class UIHttpServer extends AbstractUIServer { body ?? Constants.EMPTY_FREEZED_OBJECT, ), ) - .then((protocolResponse: ProtocolResponse | undefined) => { + .then((protocolResponse?: ProtocolResponse) => { if (!isNullOrUndefined(protocolResponse)) { this.sendResponse(protocolResponse!); } diff --git a/src/charging-station/ui-server/UIWebSocketServer.ts b/src/charging-station/ui-server/UIWebSocketServer.ts index befb9854..5651ab5a 100644 --- a/src/charging-station/ui-server/UIWebSocketServer.ts +++ b/src/charging-station/ui-server/UIWebSocketServer.ts @@ -60,7 +60,7 @@ export class UIWebSocketServer extends AbstractUIServer { this.uiServices .get(version) ?.requestHandler(request) - .then((protocolResponse: ProtocolResponse | undefined) => { + .then((protocolResponse?: ProtocolResponse) => { if (!isNullOrUndefined(protocolResponse)) { this.sendResponse(protocolResponse!); } diff --git a/src/worker/WorkerAbstract.ts b/src/worker/WorkerAbstract.ts index 41145f80..32319ab2 100644 --- a/src/worker/WorkerAbstract.ts +++ b/src/worker/WorkerAbstract.ts @@ -3,7 +3,6 @@ import { existsSync } from 'node:fs'; import type { PoolEmitter, PoolInfo } from 'poolifier'; -import { WorkerConstants } from './WorkerConstants'; import type { SetInfo, WorkerData, WorkerOptions } from './WorkerTypes'; import { defaultErrorHandler, defaultExitHandler } from './WorkerUtils'; @@ -21,17 +20,7 @@ export abstract class WorkerAbstract { * @param workerScript - * @param workerOptions - */ - constructor( - workerScript: string, - workerOptions: 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: {}, - }, - ) { + constructor(workerScript: string, workerOptions: WorkerOptions) { if (workerScript === null || workerScript === undefined) { throw new Error('Worker script is not defined'); } diff --git a/src/worker/WorkerDynamicPool.ts b/src/worker/WorkerDynamicPool.ts index c828c0a9..5824d8ad 100644 --- a/src/worker/WorkerDynamicPool.ts +++ b/src/worker/WorkerDynamicPool.ts @@ -13,7 +13,7 @@ export class WorkerDynamicPool extends WorkerAbstract { * @param workerScript - * @param workerOptions - */ - constructor(workerScript: string, workerOptions?: WorkerOptions) { + constructor(workerScript: string, workerOptions: WorkerOptions) { super(workerScript, workerOptions); this.pool = new DynamicThreadPool( this.workerOptions.poolMinSize, diff --git a/src/worker/WorkerFactory.ts b/src/worker/WorkerFactory.ts index f64d2781..ae27d1df 100644 --- a/src/worker/WorkerFactory.ts +++ b/src/worker/WorkerFactory.ts @@ -1,7 +1,5 @@ import { isMainThread } from 'node:worker_threads'; -import type { ThreadPoolOptions } from 'poolifier'; - import type { WorkerAbstract } from './WorkerAbstract'; import { WorkerConstants } from './WorkerConstants'; import { WorkerDynamicPool } from './WorkerDynamicPool'; @@ -9,6 +7,15 @@ 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 @@ -22,29 +29,16 @@ export class WorkerFactory { if (!isMainThread) { throw new Error('Cannot get a worker implementation outside the main thread'); } - workerOptions = workerOptions ?? ({} as WorkerOptions); - workerOptions.workerStartDelay = - workerOptions?.workerStartDelay ?? WorkerConstants.DEFAULT_WORKER_START_DELAY; - workerOptions.elementStartDelay = - workerOptions?.elementStartDelay ?? WorkerConstants.DEFAULT_ELEMENT_START_DELAY; - workerOptions.poolOptions = workerOptions?.poolOptions ?? ({} as ThreadPoolOptions); + workerOptions = { ...DEFAULT_WORKER_OPTIONS, ...workerOptions }; let workerImplementation: WorkerAbstract | null = null; switch (workerProcessType) { case WorkerProcessType.workerSet: - workerOptions.elementsPerWorker = - workerOptions?.elementsPerWorker ?? WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER; workerImplementation = new WorkerSet(workerScript, workerOptions); break; case WorkerProcessType.staticPool: - workerOptions.poolMaxSize = - workerOptions?.poolMaxSize ?? WorkerConstants.DEFAULT_POOL_MAX_SIZE; workerImplementation = new WorkerStaticPool(workerScript, workerOptions); break; case WorkerProcessType.dynamicPool: - workerOptions.poolMinSize = - workerOptions?.poolMinSize ?? WorkerConstants.DEFAULT_POOL_MIN_SIZE; - workerOptions.poolMaxSize = - workerOptions?.poolMaxSize ?? WorkerConstants.DEFAULT_POOL_MAX_SIZE; workerImplementation = new WorkerDynamicPool(workerScript, workerOptions); break; default: diff --git a/src/worker/WorkerSet.ts b/src/worker/WorkerSet.ts index 61118a0d..ce25e7e3 100644 --- a/src/worker/WorkerSet.ts +++ b/src/worker/WorkerSet.ts @@ -3,6 +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 { @@ -15,6 +17,11 @@ 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; @@ -25,13 +32,10 @@ export class WorkerSet extends WorkerAbstract { * @param workerScript - * @param workerOptions - */ - constructor(workerScript: string, workerOptions?: WorkerOptions) { + constructor(workerScript: string, workerOptions: WorkerOptions) { super(workerScript, workerOptions); this.workerOptions.poolOptions = { - ...{ - enableEvents: true, - restartWorkerOnError: true, - }, + ...DEFAULT_POOL_OPTIONS, ...this.workerOptions.poolOptions, }; this.workerSet = new Set(); diff --git a/src/worker/WorkerStaticPool.ts b/src/worker/WorkerStaticPool.ts index b1cc7189..acc61b8f 100644 --- a/src/worker/WorkerStaticPool.ts +++ b/src/worker/WorkerStaticPool.ts @@ -13,7 +13,7 @@ export class WorkerStaticPool extends WorkerAbstract { * @param workerScript - * @param workerOptions - */ - constructor(workerScript: string, workerOptions?: WorkerOptions) { + constructor(workerScript: string, workerOptions: WorkerOptions) { super(workerScript, workerOptions); this.pool = new FixedThreadPool( this.workerOptions.poolMaxSize, -- 2.34.1