Add some sanity checks to Wrk class.
authorJérôme Benoit <jerome.benoit@sap.com>
Thu, 21 Jan 2021 17:57:31 +0000 (18:57 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Thu, 21 Jan 2021 17:57:31 +0000 (18:57 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/Worker.ts
src/start.ts

index a4cd80a9a4ed6796408e60b1dfc0743a8dbdbea0..8bb9d1077b934f8d295eed40bdd108ad3cfedb4d 100644 (file)
@@ -9,6 +9,8 @@ export default class Wrk {
   private _workerScript: string;
   private _workerData: WorkerData;
   private _worker: Worker;
+  private _maxWorkerElements: number;
+  private _numWorkerElements: number;
 
   /**
    * Create a new `Wrk`.
@@ -16,9 +18,11 @@ export default class Wrk {
    * @param {string} workerScript
    * @param {WorkerData} workerData
    */
-  constructor(workerScript: string, workerData: WorkerData) {
+  constructor(workerScript: string, workerData: WorkerData, maxWorkerElements = 1) {
     this._workerData = workerData;
     this._workerScript = workerScript;
+    this._maxWorkerElements = maxWorkerElements;
+    this._numWorkerElements = 0;
     if (Configuration.useWorkerPool()) {
       WorkerPool.maxConcurrentWorkers = Configuration.getWorkerPoolMaxSize();
     }
@@ -44,12 +48,15 @@ export default class Wrk {
    * @public
    */
   addWorkerElement(workerData: WorkerData): void {
-    // FIXME: also forbid to add an element if the current number of elements > max number of elements
     if (Configuration.useWorkerPool()) {
-      return;
+      throw Error('Cannot add Wrk element if the worker pool is enabled');
+    }
+    if (this._numWorkerElements >= this._maxWorkerElements) {
+      throw Error('Cannot add Wrk element: max number of elements per worker reached');
     }
     this._workerData = workerData;
     this._worker.postMessage({ id: Constants.START_WORKER_ELEMENT, workerData: workerData });
+    this._numWorkerElements++;
   }
 
   /**
@@ -96,6 +103,7 @@ export default class Wrk {
           reject(new Error(`Worker stopped with exit code ${code}`));
         }
       });
+      this._numWorkerElements++;
       this._worker = worker;
     });
   }
index 94af1c54fb8999ef42e6c5f3e5b1b5e020bd772f..1baea43212e7668446ec068a83d55fe1f8e0920f 100644 (file)
@@ -29,13 +29,13 @@ class Bootstrap {
                 numStationsTotal = numConcurrentWorkers;
                 // Start Wrk sequentially to optimize memory at start time
                 await Utils.sleep(Constants.START_WORKER_DELAY);
-              } else if (!Configuration.useWorkerPool() && (chargingStationsPerWorkerCounter === 0 || chargingStationsPerWorkerCounter === chargingStationsPerWorker)) {
+              } else if (!Configuration.useWorkerPool() && (chargingStationsPerWorkerCounter === 0 || chargingStationsPerWorkerCounter >= chargingStationsPerWorker)) {
                 // Start new Wrk with one charging station
-                worker = new Wrk('./dist/charging-station/StationWorker.js', workerData);
+                worker = new Wrk('./dist/charging-station/StationWorker.js', workerData, chargingStationsPerWorker);
                 worker.start().catch(() => { });
                 numConcurrentWorkers++;
-                numStationsTotal++;
                 chargingStationsPerWorkerCounter = 1;
+                numStationsTotal++;
                 // Start Wrk sequentially to optimize memory at start time
                 await Utils.sleep(Constants.START_WORKER_DELAY);
               } else if (!Configuration.useWorkerPool()) {