From: Jérôme Benoit Date: Thu, 21 Jan 2021 17:57:31 +0000 (+0100) Subject: Add some sanity checks to Wrk class. X-Git-Tag: v1.0.1-0~129 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=ff70d2ede1dd580df1be35ba4837c9a4f04314df;p=e-mobility-charging-stations-simulator.git Add some sanity checks to Wrk class. Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/Worker.ts b/src/charging-station/Worker.ts index a4cd80a9..8bb9d107 100644 --- a/src/charging-station/Worker.ts +++ b/src/charging-station/Worker.ts @@ -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; }); } diff --git a/src/start.ts b/src/start.ts index 94af1c54..1baea432 100644 --- a/src/start.ts +++ b/src/start.ts @@ -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()) {