Fix workerSet process mode.
authorJérôme Benoit <jerome.benoit@sap.com>
Tue, 26 Jan 2021 18:36:54 +0000 (19:36 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Tue, 26 Jan 2021 18:36:54 +0000 (19:36 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/StationWorker.ts
src/start.ts
src/types/Worker.ts
src/utils/Statistics.ts
src/worker/WorkerFactory.ts

index 1b529190cb10c99f98c0b2ce5b9b17a48fc29a1c..65e2fb8832883662aadb145798da9783f37a1ae2 100644 (file)
@@ -6,6 +6,12 @@ import Constants from '../utils/Constants';
 import { ThreadWorker } from 'poolifier';
 import Utils from '../utils/Utils';
 
+// Conditionally export ThreadWorker instance for pool usage
+export let threadWorker;
+if (Utils.workerPoolInUse()) {
+  threadWorker = new ThreadWorker(startChargingStation, { maxInactiveTime: Constants.WORKER_POOL_MAX_INACTIVE_TIME, async: false });
+}
+
 if (!isMainThread) {
   // Add listener to start charging station from main thread
   addListener();
@@ -26,5 +32,3 @@ function startChargingStation(data: StationWorkerData) {
   const station = new ChargingStation(data.index , data.templateFile);
   station.start();
 }
-
-export default new ThreadWorker(startChargingStation, { maxInactiveTime: Constants.WORKER_POOL_MAX_INACTIVE_TIME, async: false });
index 03bd4b8b03d41cbb3fd7a0787f0e8f082a7770ba..04dd28140811d09d114ce59aa1c87a00a7d467d7 100644 (file)
@@ -8,7 +8,11 @@ class Bootstrap {
   static async start() {
     try {
       let numStationsTotal = 0;
-      const workerImplementation: Wrk = WorkerFactory.getWorkerImpl('./dist/charging-station/StationWorker.js');
+      const workerImplementation: Wrk = WorkerFactory.getWorkerImpl('./dist/charging-station/StationWorker.js', Configuration.getWorkerProcess(), {
+        poolMaxSize: Configuration.getWorkerPoolMaxSize(),
+        poolMinSize: Configuration.getWorkerPoolMinSize(),
+        elementsPerWorker: Configuration.getChargingStationsPerWorker()
+      });
       await workerImplementation.start();
       // Start ChargingStation object in worker thread
       if (Configuration.getStationTemplateURLs()) {
index 6ec9e9997ca1a17f33daa435eb0b34e43fd8bb02..bc2972684c0cb2677f34176ffbd11b119392d423 100644 (file)
@@ -6,6 +6,12 @@ export enum WorkerProcessType {
   STATIC_POOL = 'staticPool'
 }
 
+export interface WorkerOptions {
+  poolMaxSize?: number;
+  poolMinSize?: number;
+  elementsPerWorker?: number;
+}
+
 export interface WorkerData { }
 
 export interface StationWorkerData extends WorkerData {
index 966f1b57a6cc2b61de80485bc32c7cb57abbb5c2..05a2255142b8c89d0b9a9986509d31fdf90a0e5b 100644 (file)
@@ -14,7 +14,7 @@ export default class Statistics {
 
   public constructor(objName: string) {
     this.objId = objName;
-    this.commandsStatistics = { id: this.objId ? this.objId : ' Object id not specified', commandsStatisticsData: {} } as CommandStatistics;
+    this.commandsStatistics = { id: this.objId ? this.objId : ' Object id not specified', commandsStatisticsData: {} };
   }
 
   public addMessage(command: RequestCommand | IncomingRequestCommand, messageType: MessageType): void {
index 2d923daab084bb95b77a783509053bed4b519a7a..24145627c18405c157c3903c8dd773808627795e 100644 (file)
@@ -1,19 +1,35 @@
-import Configuration from '../utils/Configuration';
+import { WorkerOptions, WorkerProcessType } from '../types/Worker';
+
+import Utils from '../utils/Utils';
 import WorkerDynamicPool from './WorkerDynamicPool';
-import { WorkerProcessType } from '../types/Worker';
 import WorkerSet from './WorkerSet';
 import WorkerStaticPool from './WorkerStaticPool';
 import Wrk from './Wrk';
 
 export default class WorkerFactory {
-  public static getWorkerImpl(workerScript: string): Wrk {
-    switch (Configuration.getWorkerProcess()) {
+  public static getWorkerImpl(workerScript: string, workerProcessType: WorkerProcessType, options?: WorkerOptions): Wrk {
+    if (Utils.isUndefined(options)) {
+      options = {} as WorkerOptions;
+    }
+    switch (workerProcessType) {
       case WorkerProcessType.WORKER_SET:
-        return new WorkerSet(workerScript, Configuration.getChargingStationsPerWorker());
+        if (Utils.isUndefined(options.elementsPerWorker)) {
+          options.elementsPerWorker = 1;
+        }
+        return new WorkerSet(workerScript, options.elementsPerWorker);
       case WorkerProcessType.STATIC_POOL:
-        return new WorkerStaticPool(workerScript, Configuration.getWorkerPoolMaxSize());
+        if (Utils.isUndefined(options.poolMaxSize)) {
+          options.elementsPerWorker = 16;
+        }
+        return new WorkerStaticPool(workerScript, options.poolMaxSize);
       case WorkerProcessType.DYNAMIC_POOL:
-        return new WorkerDynamicPool(workerScript, Configuration.getWorkerPoolMinSize(), Configuration.getWorkerPoolMaxSize());
+        if (Utils.isUndefined(options.poolMinSize)) {
+          options.elementsPerWorker = 4;
+        }
+        if (Utils.isUndefined(options.poolMaxSize)) {
+          options.elementsPerWorker = 16;
+        }
+        return new WorkerDynamicPool(workerScript, options.poolMinSize, options.poolMaxSize);
       default:
         return null;
     }