refactor: split WorkerConstants class
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 30 Jul 2023 11:44:03 +0000 (13:44 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 30 Jul 2023 11:44:03 +0000 (13:44 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStationWorker.ts
src/utils/Configuration.ts
src/worker/WorkerConstants.ts
src/worker/WorkerFactory.ts
src/worker/WorkerSet.ts
src/worker/index.ts

index db7ae2cb4462ff79f50359dc989b50536a73edd6..ff9dbe6286dab402ec6531ca03d05e4792e538e3 100644 (file)
@@ -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<ChargingStationWorkerData>;
 if (Configuration.workerPoolInUse()) {
   chargingStationWorker = new ThreadWorker<ChargingStationWorkerData>(startChargingStation, {
-    maxInactiveTime: WorkerConstants.POOL_MAX_INACTIVE_TIME,
+    maxInactiveTime: POOL_MAX_INACTIVE_TIME,
   });
 } else {
   chargingStationWorker = new ChargingStationWorker();
index f01deceaf24836c2266020e9e8339991f08f5497..ad8d328df83f1da34d864c59d5431b6b79539dbc 100644 (file)
@@ -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;
index dcbe2ca43caa3faec645c6e693dc04f1cb8b93f2..bbfcdac4b10a37b55adf26ee21d5385517b97ca9 100644 (file)
@@ -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,
+};
index ae27d1dfd56fa5e0fd14d4daba074d35afb32c0c..59fbf3a44fe46409a0d84c5a11ed0abcf7fce7f1 100644 (file)
@@ -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
index e07306784f120a6ccd6b83401b9d9dcc0e3d1fc3..c1fecdcec2f7f6b03adfe412a2bfe18c8d997935 100644 (file)
@@ -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<WorkerData> {
   public readonly emitter!: EventEmitter;
   private readonly workerSet: Set<WorkerSetElement>;
@@ -59,7 +52,7 @@ export class WorkerSet extends WorkerAbstract<WorkerData> {
 
   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<WorkerData> {
       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<WorkerData>) => {
       if (message.event === WorkerMessageEvents.startedWorkerElement) {
         this.emitter?.emit(WorkerSetEvents.elementStarted, this.info);
@@ -136,24 +126,15 @@ export class WorkerSet extends WorkerAbstract<WorkerData> {
         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)!),
     );
index 50848d0900533e9d91cd3fe02b867c66797aad3b..e53c3457fa636e3496ce8ab823b19f41d5965f7c 100644 (file)
@@ -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,