From ff70d2ede1dd580df1be35ba4837c9a4f04314df Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 21 Jan 2021 18:57:31 +0100 Subject: [PATCH] Add some sanity checks to Wrk class. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/Worker.ts | 14 +++++++++++--- src/start.ts | 6 +++--- 2 files changed, 14 insertions(+), 6 deletions(-) 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()) { -- 2.34.1