X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fstart.ts;h=947a1abfd15011ae208529f92f7edd1ed41cb98e;hb=418106c832022ba6f100f4bf81315300994bee87;hp=67df5a3f44b1538ebabc3eeb4f79d5c87afaf6a7;hpb=5fdab605ce885d9a9fb21ac47c6d0766bd3673bb;p=e-mobility-charging-stations-simulator.git diff --git a/src/start.ts b/src/start.ts index 67df5a3f..947a1abf 100644 --- a/src/start.ts +++ b/src/start.ts @@ -2,40 +2,55 @@ import Configuration from './utils/Configuration'; import Constants from './utils/Constants'; import Utils from './utils/Utils'; import WorkerData from './types/WorkerData'; -import Wrk from './charging-station/Worker'; +import WorkerGroup from './charging-station/WorkerGroup'; +import WorkerPool from './charging-station/WorkerPool'; class Bootstrap { static async start() { try { let numStationsTotal = 0; let numConcurrentWorkers = 0; - let worker: Wrk; const chargingStationsPerWorker = Configuration.getChargingStationsPerWorker(); - let counter = 0; + let chargingStationsPerWorkerCounter = 0; + let workerImplementation: WorkerGroup | WorkerPool; + if (Configuration.useWorkerPool()) { + workerImplementation = new WorkerPool('./dist/charging-station/StationWorker.js'); + void workerImplementation.start(); + } // Start each ChargingStation object in a worker thread if (Configuration.getStationTemplateURLs()) { - for await (const stationURL of Configuration.getStationTemplateURLs()) { + for (const stationURL of Configuration.getStationTemplateURLs()) { try { const nbStations = stationURL.numberOfStations ? stationURL.numberOfStations : 0; - numStationsTotal += nbStations; for (let index = 1; index <= nbStations; index++) { - const workerData = { + const workerData: WorkerData = { 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); - worker.start().catch(() => { }); - counter = 0; - // Start workers sequentially to optimize memory at start time + }; + if (Configuration.useWorkerPool()) { + void workerImplementation.addElement(workerData); + numConcurrentWorkers = workerImplementation.size; + numStationsTotal = workerImplementation.size; + // Start worker sequentially to optimize memory at start time await Utils.sleep(Constants.START_WORKER_DELAY); } else { - // Add charging station to existing Worker - worker.addChargingStation(workerData, numStationsTotal); + // eslint-disable-next-line no-lonely-if + if (chargingStationsPerWorkerCounter === 0 || chargingStationsPerWorkerCounter >= chargingStationsPerWorker) { + // Start new WorkerGroup with one charging station + workerImplementation = new WorkerGroup('./dist/charging-station/StationWorker.js', workerData, chargingStationsPerWorker); + void workerImplementation.start(); + numConcurrentWorkers++; + chargingStationsPerWorkerCounter = 1; + numStationsTotal++; + // Start worker sequentially to optimize memory at start time + await Utils.sleep(Constants.START_WORKER_DELAY); + } else { + // Add charging station to existing WorkerGroup + void workerImplementation.addElement(workerData); + chargingStationsPerWorkerCounter++; + numStationsTotal++; + } } - counter++; - numConcurrentWorkers = worker.concurrentWorkers; } } catch (error) { // eslint-disable-next-line no-console @@ -47,8 +62,10 @@ class Bootstrap { } if (numStationsTotal === 0) { console.log('No charging station template enabled in configuration, exiting'); + } else if (Configuration.useWorkerPool()) { + console.log('Charging station simulator started with ' + numStationsTotal.toString() + ' charging station(s) and ' + numConcurrentWorkers.toString() + '/' + Configuration.getWorkerPoolMaxSize().toString() + ' worker(s) concurrently running'); } else { - console.log('Charging station simulator started with ' + numStationsTotal.toString() + ' charging station(s) of ' + numConcurrentWorkers.toString() + ' concurrently running'); + console.log('Charging station simulator started with ' + numStationsTotal.toString() + ' charging station(s) and ' + numConcurrentWorkers.toString() + ' worker(s) concurrently running'); } } catch (error) { // eslint-disable-next-line no-console @@ -57,4 +74,8 @@ class Bootstrap { } } -Bootstrap.start(); +Bootstrap.start().catch( + (error) => { + console.error(error); + } +);