Fix worker startup and cleanup some attributes in Wrk class.
authorJérôme Benoit <jerome.benoit@sap.com>
Thu, 21 Jan 2021 12:51:58 +0000 (13:51 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Thu, 21 Jan 2021 12:51:58 +0000 (13:51 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/AutomaticTransactionGenerator.ts
src/charging-station/Worker.ts
src/start.ts

index d6a825ed0db7eb419ad4a8f1c443a02f8b5bfc26..31d4d85284354675676012136f30c2a2a85fd781 100644 (file)
@@ -1,4 +1,4 @@
-import { AuthorizationStatus, StartTransactionResponse, StopTransactionReason, StopTransactionResponse } from '../types/ocpp/1.6/Transaction';
+import { AuthorizationStatus, AuthorizeResponse, StartTransactionResponse, StopTransactionReason, StopTransactionResponse } from '../types/ocpp/1.6/Transaction';
 import { PerformanceObserver, performance } from 'perf_hooks';
 
 import ChargingStation from './ChargingStation';
@@ -89,7 +89,7 @@ export default class AutomaticTransactionGenerator {
       if (start < this._chargingStation.stationInfo.AutomaticTransactionGenerator.probabilityOfStart) {
         skip = 0;
         // Start transaction
-        let startResponse: StartTransactionResponse;
+        let startResponse: StartTransactionResponse | AuthorizeResponse;
         if (this._chargingStation.getEnableStatistics()) {
           const startTransaction = performance.timerify(this.startTransaction);
           this._performanceObserver.observe({ entryTypes: ['function'] });
@@ -127,7 +127,7 @@ export default class AutomaticTransactionGenerator {
   }
 
   // eslint-disable-next-line consistent-this
-  private async startTransaction(connectorId: number, self: AutomaticTransactionGenerator): Promise<StartTransactionResponse> {
+  private async startTransaction(connectorId: number, self: AutomaticTransactionGenerator): Promise<StartTransactionResponse | AuthorizeResponse> {
     if (self._chargingStation.hasAuthorizedTags()) {
       const tagId = self._chargingStation.getRandomTagId();
       if (self._chargingStation.stationInfo.AutomaticTransactionGenerator.requireAuthorize) {
@@ -138,7 +138,7 @@ export default class AutomaticTransactionGenerator {
           // Start transaction
           return await self._chargingStation.sendStartTransaction(connectorId, tagId);
         }
-        return authorizeResponse as StartTransactionResponse;
+        return authorizeResponse;
       }
       logger.info(self._logPrefix(connectorId) + ' start transaction for tagID ' + tagId);
       // Start transaction
index 4137429780aa3fda1f12de4fdd8ab18be9098d48..efa327fd8a0666d301bfcae95d8b332a79158958 100644 (file)
@@ -8,8 +8,6 @@ import WorkerData from '../types/WorkerData';
 export default class Wrk {
   private _workerScript: string;
   private _workerData: WorkerData;
-  private _index: number;
-  private _maxWorkerElements: number;
   private _worker: Worker;
 
   /**
@@ -17,24 +15,13 @@ export default class Wrk {
    *
    * @param {string} workerScript
    * @param {WorkerData} workerData
-   * @param {number} maxWorkerElements
    */
-  constructor(workerScript: string, workerData: WorkerData, maxWorkerElements = 1) {
+  constructor(workerScript: string, workerData: WorkerData) {
     this._workerData = workerData;
-    this._index = workerData.index;
     this._workerScript = workerScript;
     if (Configuration.useWorkerPool()) {
       WorkerPool.maxConcurrentWorkers = Configuration.getWorkerPoolSize();
     }
-    this._maxWorkerElements = maxWorkerElements;
-  }
-
-  /**
-   * @return {number}
-   * @public
-   */
-  public get maxWorkerElements(): number {
-    return this._maxWorkerElements;
   }
 
   /**
@@ -62,7 +49,6 @@ export default class Wrk {
       return;
     }
     this._workerData = workerData;
-    this._index = workerData.index;
     this._worker.postMessage({ id : Constants.START_WORKER_ELEMENT, workerData: workerData });
   }
 
@@ -96,7 +82,7 @@ export default class Wrk {
       worker.on('error', reject);
       worker.on('exit', (code) => {
         if (code !== 0) {
-          reject(new Error(`Worker id ${this._index} stopped with exit code ${code}`));
+          reject(new Error(`Worker stopped with exit code ${code}`));
         }
       });
       this._worker = worker;
index 3b9fc75b287c7c8d9eff1296dd0eb720ddb09fe0..2bcfbae5153cf03b8a1989db915096381c3283f0 100644 (file)
@@ -9,10 +9,12 @@ class Bootstrap {
     try {
       let numStationsTotal = 0;
       let numConcurrentWorkers = 0;
+      const chargingStationsPerWorker = Configuration.getChargingStationsPerWorker();
+      let chargingStationsPerWorkerCounter = 0;
       let worker: Wrk;
       // Start each ChargingStation object in a worker thread
       if (Configuration.getStationTemplateURLs()) {
-        for await (const stationURL of Configuration.getStationTemplateURLs()) {
+        for (const stationURL of Configuration.getStationTemplateURLs()) {
           try {
             const nbStations = stationURL.numberOfStations ? stationURL.numberOfStations : 0;
             numStationsTotal += nbStations;
@@ -25,22 +27,18 @@ class Bootstrap {
                 worker = new Wrk('./dist/charging-station/StationWorker.js', workerData);
                 worker.start().catch(() => { });
                 numConcurrentWorkers = Configuration.getWorkerPoolSize();
-              } else {
-                const chargingStationsPerWorker = Configuration.getChargingStationsPerWorker();
-                let chargingStationsPerWorkerCounter = 0;
-                if (chargingStationsPerWorkerCounter === 0 || chargingStationsPerWorkerCounter === chargingStationsPerWorker) {
-                  // Start new Wrk with one charging station
-                  worker = new Wrk('./dist/charging-station/StationWorker.js', workerData, chargingStationsPerWorker);
-                  worker.start().catch(() => { });
-                  numConcurrentWorkers++;
-                  chargingStationsPerWorkerCounter = 1;
-                  // Start Wrk sequentially to optimize memory at start time
-                  await Utils.sleep(Constants.START_WORKER_DELAY);
-                } else {
-                  // Add charging station to existing Wrk
-                  worker.addWorkerElement(workerData);
-                  chargingStationsPerWorkerCounter++;
-                }
+              } 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.start().catch(() => { });
+                numConcurrentWorkers++;
+                chargingStationsPerWorkerCounter = 1;
+                // Start Wrk sequentially to optimize memory at start time
+                await Utils.sleep(Constants.START_WORKER_DELAY);
+              } else if (!Configuration.useWorkerPool()) {
+                // Add charging station to existing Wrk
+                worker.addWorkerElement(workerData);
+                chargingStationsPerWorkerCounter++;
               }
             }
           } catch (error) {
@@ -63,4 +61,8 @@ class Bootstrap {
   }
 }
 
-Bootstrap.start();
+Bootstrap.start().catch(
+  (error) => {
+    console.error(error);
+  }
+);