Set the defaultMaxListeners EventEmitter value at worker init.
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 17 Apr 2020 19:06:32 +0000 (21:06 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Fri, 17 Apr 2020 19:06:32 +0000 (21:06 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/Worker.js
src/index.js

index 5de059d00c6229153937b3d690f7bb7fa444c410..7e899d40328eea4082568382717745296b879050 100644 (file)
@@ -9,13 +9,32 @@ class Wrk {
    *
    * @param {String} workerScript
    * @param {Object} workerData
+   * @param {Number} numConcurrentWorkers
    */
-  constructor(workerScript, workerData) {
+  constructor(workerScript, workerData, numConcurrentWorkers) {
+    this._workerData = workerData;
+    this._workerScript = workerScript;
+    this._numConcurrentWorkers = numConcurrentWorkers;
     if (Configuration.useWorkerPool()) {
       this._pool = new Pool({max: Configuration.getWorkerPoolSize()});
     }
-    this._workerData = workerData;
-    this._workerScript = workerScript;
+  }
+
+  /**
+   * @param {Number} numConcurrentWorkers
+   * @private
+   */
+  // eslint-disable-next-line class-methods-use-this
+  set _numConcurrentWorkers(numConcurrentWorkers) {
+    if (numConcurrentWorkers > 10) {
+      EventEmitter.defaultMaxListeners = numConcurrentWorkers + 1;
+    }
+    this._concurrentWorkers = numConcurrentWorkers;
+  }
+
+  // eslint-disable-next-line class-methods-use-this
+  get _numConcurrentWorkers() {
+    return this._concurrentWorkers;
   }
 
   /**
@@ -60,9 +79,6 @@ class Wrk {
    */
   start() {
     if (Configuration.useWorkerPool()) {
-      if (Configuration.getWorkerPoolSize() > 10) {
-        EventEmitter.defaultMaxListeners = Configuration.getWorkerPoolSize() + 1;
-      }
       return this._startWorkerWithPool();
     }
     return this._startWorker();
index a1a5c61bf1327a749661dff327ae04a2e6181bb7..71edc5a97b9e004c56083f9db8ff885b67cddfa1 100644 (file)
@@ -10,6 +10,7 @@ class Bootstrap {
       logger.info('%s Configuration: %j', Utils.basicFormatLog(), Configuration.getConfig());
       // Start each ChargingStation object in a worker thread
       if (Configuration.getChargingStationTemplateURLs()) {
+        let numStationsTotal = 0;
         Configuration.getChargingStationTemplateURLs().forEach((stationURL) => {
           try {
             // load file
@@ -17,11 +18,12 @@ class Bootstrap {
             const stationTemplate = JSON.parse(fs.readFileSync(fileDescriptor, 'utf8'));
             fs.closeSync(fileDescriptor);
             const nbStation = (stationURL.numberOfStation ? stationURL.numberOfStation : 0);
+            numStationsTotal += nbStation;
             for (let index = 1; index <= nbStation; index++) {
               const worker = new Wrk('./src/charging-station/StationWorker.js', {
                 index,
                 template: JSON.parse(JSON.stringify(stationTemplate)),
-              });
+              }, numStationsTotal);
               worker.start();
             }
           } catch (error) {
@@ -35,7 +37,7 @@ class Bootstrap {
           const worker = new Wrk('./src/charging-station/StationWorker.js', {
             index,
             template: JSON.parse(JSON.stringify(Configuration.getChargingStationTemplate())),
-          });
+          }, nbStation);
           worker.start();
         }
       }