refactor: cleanup default worker options handling
authorJérôme Benoit <jerome.benoit@sap.com>
Tue, 18 Jul 2023 21:41:29 +0000 (23:41 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Tue, 18 Jul 2023 21:41:29 +0000 (23:41 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts
src/charging-station/ui-server/UIHttpServer.ts
src/charging-station/ui-server/UIWebSocketServer.ts
src/worker/WorkerAbstract.ts
src/worker/WorkerDynamicPool.ts
src/worker/WorkerFactory.ts
src/worker/WorkerSet.ts
src/worker/WorkerStaticPool.ts

index bb484be74eaf9e96f0e9c1eee7869d30a32229bd..87ceaca97a0dcece14ce393fd335826018f69c76 100644 (file)
@@ -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`,
       );
index 103a27aed75c163ef7481035ae2eef681ac6b81a..ee12b3f4de996ed3a8f86b2a4db97949e9ebaf4e 100644 (file)
@@ -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!);
                 }
index befb9854fa8a1a0d91e9daeff2a935eaa5634a0d..5651ab5a405f3d98c00f0688ff7f493ab7da8a71 100644 (file)
@@ -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!);
             }
index 41145f803ed448ce60b9a03fa584af5403ae4a92..32319ab2ee87d6faf8af97a2dfcd9c74babaa9fc 100644 (file)
@@ -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<T extends WorkerData> {
    * @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');
     }
index c828c0a9e4ac71d6c791c85afb6490dd827bdccb..5824d8adfa21fc44e3c59d22d9c3fd94461e496e 100644 (file)
@@ -13,7 +13,7 @@ export class WorkerDynamicPool extends WorkerAbstract<WorkerData> {
    * @param workerScript -
    * @param workerOptions -
    */
-  constructor(workerScript: string, workerOptions?: WorkerOptions) {
+  constructor(workerScript: string, workerOptions: WorkerOptions) {
     super(workerScript, workerOptions);
     this.pool = new DynamicThreadPool<WorkerData>(
       this.workerOptions.poolMinSize,
index f64d27810ed04a3e2a7752918f94d0ea3b7ec006..ae27d1dfd56fa5e0fd14d4daba074d35afb32c0c 100644 (file)
@@ -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<T> | 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:
index 61118a0d0a274d17871a71ec8e2ebec3efa2c2a9..ce25e7e3a6e4ec3428072a05fc6e13d90e14c0fd 100644 (file)
@@ -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<WorkerData> {
   public readonly emitter!: EventEmitter;
   private readonly workerSet: Set<WorkerSetElement>;
@@ -25,13 +32,10 @@ export class WorkerSet extends WorkerAbstract<WorkerData> {
    * @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<WorkerSetElement>();
index b1cc718949b4d0ea94d3c338315bf8284cd45e43..acc61b8fad0fadac0fa66a7f7433a9e143db4658 100644 (file)
@@ -13,7 +13,7 @@ export class WorkerStaticPool extends WorkerAbstract<WorkerData> {
    * @param workerScript -
    * @param workerOptions -
    */
-  constructor(workerScript: string, workerOptions?: WorkerOptions) {
+  constructor(workerScript: string, workerOptions: WorkerOptions) {
     super(workerScript, workerOptions);
     this.pool = new FixedThreadPool(
       this.workerOptions.poolMaxSize,