import Constants from '../utils/Constants';
if (!isMainThread) {
- const station = new ChargingStation(workerData.index as number, workerData.templateFile as string);
- station.start();
+ startChargingStation({ index: workerData.index as number, templateFile: workerData.templateFile as string });
// Listener: start new charging station from main thread
addListener();
function addListener() {
parentPort.setMaxListeners(Constants.MAX_LISTENERS);
parentPort.on('message', (e) => {
- if (e.id === Constants.START_CHARGING_STATION) {
+ if (e.id === Constants.START_WORKER_ELEMENT) {
startChargingStation(e.workerData);
}
});
private _workerScript: string;
private _workerData: WorkerData;
private _index: number;
- private _concurrentWorkers: number;
+ private _maxWorkerElements: number;
private _worker: Worker;
/**
*
* @param {string} workerScript
* @param {WorkerData} workerData
- * @param {number} numConcurrentWorkers
+ * @param {number} maxWorkerElements
*/
- constructor(workerScript: string, workerData: WorkerData, numConcurrentWorkers: number) {
+ constructor(workerScript: string, workerData: WorkerData, maxWorkerElements = 1) {
this._workerData = workerData;
this._index = workerData.index;
this._workerScript = workerScript;
if (Configuration.useWorkerPool()) {
- this._concurrentWorkers = Configuration.getWorkerPoolSize();
- WorkerPool.concurrentWorkers = this._concurrentWorkers;
- } else {
- this._concurrentWorkers = numConcurrentWorkers;
+ WorkerPool.maxConcurrentWorkers = Configuration.getWorkerPoolSize();
}
+ this._maxWorkerElements = maxWorkerElements;
}
/**
* @return {number}
* @public
*/
- public get concurrentWorkers(): number {
- return this._concurrentWorkers;
+ public get maxWorkerElements(): number {
+ return this._maxWorkerElements;
}
/**
* @return {void}
* @public
*/
- addChargingStation(workerData: WorkerData, numConcurrentWorkers: number): void {
+ 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;
+ }
this._workerData = workerData;
this._index = workerData.index;
- this._concurrentWorkers = numConcurrentWorkers;
- this._worker.postMessage({ id : Constants.START_CHARGING_STATION, workerData: workerData });
+ this._worker.postMessage({ id : Constants.START_WORKER_ELEMENT, workerData: workerData });
}
/**
}
class WorkerPool {
- public static concurrentWorkers: number;
+ public static maxConcurrentWorkers: 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 });
+ if (!WorkerPool._instance) {
+ WorkerPool._instance = new Pool({ max: WorkerPool.maxConcurrentWorkers });
}
return WorkerPool._instance;
}
let numStationsTotal = 0;
let numConcurrentWorkers = 0;
let worker: Wrk;
- const chargingStationsPerWorker = Configuration.getChargingStationsPerWorker();
- let counter = 0;
// Start each ChargingStation object in a worker thread
if (Configuration.getStationTemplateURLs()) {
for await (const stationURL of Configuration.getStationTemplateURLs()) {
index,
templateFile: stationURL.file
} as WorkerData;
- if (counter === 0 || counter === chargingStationsPerWorker) {
- // Start new worker with one charging station
- worker = new Wrk('./dist/charging-station/StationWorker.js', workerData, numStationsTotal);
+ if (Configuration.useWorkerPool()) {
+ worker = new Wrk('./dist/charging-station/StationWorker.js', workerData);
worker.start().catch(() => { });
- counter = 0;
- // Start workers sequentially to optimize memory at start time
- await Utils.sleep(Constants.START_WORKER_DELAY);
+ numConcurrentWorkers = Configuration.getWorkerPoolSize();
} else {
- // Add charging station to existing Worker
- worker.addChargingStation(workerData, numStationsTotal);
+ const chargingStationsPerWorker = Configuration.getChargingStationsPerWorker();
+ let chargingStationsPerWorkerCounter = 0;
+ if (chargingStationsPerWorkerCounter === 0 || chargingStationsPerWorkerCounter === chargingStationsPerWorker) {
+ // Start new Wrk with one charging station
+ worker = new Wrk('./dist/charging-station/StationWorker.js', workerData, chargingStationsPerWorker);
+ worker.start().catch(() => { });
+ numConcurrentWorkers++;
+ chargingStationsPerWorkerCounter = 1;
+ // Start Wrk sequentially to optimize memory at start time
+ await Utils.sleep(Constants.START_WORKER_DELAY);
+ } else {
+ // Add charging station to existing Wrk
+ worker.addWorkerElement(workerData);
+ chargingStationsPerWorkerCounter++;
+ }
}
- counter++;
- numConcurrentWorkers = worker.concurrentWorkers;
}
} catch (error) {
// eslint-disable-next-line no-console
+// FIXME: make it more generic
export default interface WorkerData {
index: number;
templateFile: string;
static readonly MAX_LISTENERS = 1000;
static readonly START_WORKER_DELAY = 500;
- static readonly START_CHARGING_STATION = 'startChargingStation';
+ static readonly START_WORKER_ELEMENT = 'startWorkerElement';
}