private _workerScript: string;
private _workerData: WorkerData;
private _worker: Worker;
+ private _maxWorkerElements: number;
+ private _numWorkerElements: number;
/**
* Create a new `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();
}
* @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++;
}
/**
reject(new Error(`Worker stopped with exit code ${code}`));
}
});
+ this._numWorkerElements++;
this._worker = worker;
});
}
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()) {