Cleanups.
[e-mobility-charging-stations-simulator.git] / src / charging-station / Worker.ts
index d91942021184855f282892b4c9467a6beee9b5cc..84ec9edff55ba258813e498cfbdb45a4daa4ded8 100644 (file)
@@ -1,13 +1,16 @@
+import { Worker, WorkerOptions } from 'worker_threads';
+
 import Configuration from '../utils/Configuration';
+import Constants from '../utils/Constants';
 import Pool from 'worker-threads-pool';
-import { Worker } from 'worker_threads';
 import WorkerData from '../types/WorkerData';
 
 export default class Wrk {
   private _workerScript: string;
   private _workerData: WorkerData;
-  private _pool: Pool;
+  private _index: number;
   private _concurrentWorkers: number;
+  private _worker: Worker;
 
   /**
    * Create a new `Wrk`.
@@ -18,10 +21,11 @@ export default class Wrk {
    */
   constructor(workerScript: string, workerData: WorkerData, numConcurrentWorkers: number) {
     this._workerData = workerData;
+    this._index = workerData.index;
     this._workerScript = workerScript;
     if (Configuration.useWorkerPool()) {
       this._concurrentWorkers = Configuration.getWorkerPoolSize();
-      this._pool = new Pool({ max: Configuration.getWorkerPoolSize() });
+      WorkerPool.concurrentWorkers = this._concurrentWorkers;
     } else {
       this._concurrentWorkers = numConcurrentWorkers;
     }
@@ -40,11 +44,25 @@ export default class Wrk {
    * @return {Promise}
    * @public
    */
-  async start(): Promise<unknown> {
+  async start(): Promise<Worker> {
     if (Configuration.useWorkerPool()) {
-      return this._startWorkerWithPool();
+      await this._startWorkerWithPool();
+    } else {
+      await this._startWorker();
     }
-    return this._startWorker();
+    return this._worker;
+  }
+
+  /**
+   *
+   * @return {void}
+   * @public
+   */
+  addChargingStation(workerData: WorkerData, numConcurrentWorkers: number): void {
+    this._workerData = workerData;
+    this._index = workerData.index;
+    this._concurrentWorkers = numConcurrentWorkers;
+    this._worker.postMessage({ id : Constants.START_CHARGING_STATION, workerData: workerData });
   }
 
   /**
@@ -54,12 +72,13 @@ export default class Wrk {
    */
   private async _startWorkerWithPool() {
     return new Promise((resolve, reject) => {
-      this._pool.acquire(this._workerScript, { workerData: this._workerData }, (err, worker) => {
+      WorkerPool.acquire(this._workerScript, { workerData: this._workerData }, (err, worker) => {
         if (err) {
           return reject(err);
         }
         worker.once('message', resolve);
         worker.once('error', reject);
+        this._worker = worker;
       });
     });
   }
@@ -76,9 +95,28 @@ export default class Wrk {
       worker.on('error', reject);
       worker.on('exit', (code) => {
         if (code !== 0) {
-          reject(new Error(`Worker stopped with exit code ${code}`));
+          reject(new Error(`Worker id ${this._index} stopped with exit code ${code}`));
         }
       });
+      this._worker = worker;
     });
   }
 }
+
+class WorkerPool {
+  public static concurrentWorkers: number;
+  private static _instance: Pool;
+
+  private constructor() { }
+
+  public static getInstance(): Pool {
+    if (!WorkerPool._instance || (WorkerPool._instance?.size === WorkerPool.concurrentWorkers)) {
+      WorkerPool._instance = new Pool({ max: WorkerPool.concurrentWorkers });
+    }
+    return WorkerPool._instance;
+  }
+
+  public static acquire(filename: string, options: WorkerOptions, callback: (error: Error | null, worker: Worker) => void): void {
+    WorkerPool.getInstance().acquire(filename, options, callback);
+  }
+}