X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fstart.ts;h=03bd4b8b03d41cbb3fd7a0787f0e8f082a7770ba;hb=059f35a55515a998352f621e40a1e35bd22b424d;hp=baef77d120b6022ec3ecb762bf7aff8de0a22302;hpb=6af9012e5b9ef2ed6f4fe8a9696b40ac0e8da4d0;p=e-mobility-charging-stations-simulator.git diff --git a/src/start.ts b/src/start.ts index baef77d1..03bd4b8b 100644 --- a/src/start.ts +++ b/src/start.ts @@ -1,44 +1,50 @@ import Configuration from './utils/Configuration'; +import { StationWorkerData } from './types/Worker'; import Utils from './utils/Utils'; -import Wrk from './charging-station/Worker'; -import logger from './utils/Logger'; +import WorkerFactory from './worker/WorkerFactory'; +import Wrk from './worker/Wrk'; class Bootstrap { - static start() { + static async start() { try { - logger.debug('%s Configuration: %j', Utils.logPrefix(), Configuration.getConfig()); let numStationsTotal = 0; - // Start each ChargingStation object in a worker thread + const workerImplementation: Wrk = WorkerFactory.getWorkerImpl('./dist/charging-station/StationWorker.js'); + await workerImplementation.start(); + // Start ChargingStation object in worker thread if (Configuration.getStationTemplateURLs()) { - Configuration.getStationTemplateURLs().forEach((stationURL) => { + for (const stationURL of Configuration.getStationTemplateURLs()) { try { - const nbStation = stationURL.numberOfStation ? stationURL.numberOfStation : 0; - numStationsTotal += nbStation; - for (let index = 1; index <= nbStation; index++) { - const worker = new Wrk('./dist/charging-station/StationWorker.js', { + const nbStations = stationURL.numberOfStations ? stationURL.numberOfStations : 0; + for (let index = 1; index <= nbStations; index++) { + const workerData: StationWorkerData = { index, - templateFile: stationURL.file, - }, numStationsTotal); - worker.start().catch(() => {}); + templateFile: stationURL.file + }; + await workerImplementation.addElement(workerData); + numStationsTotal++; } } catch (error) { // eslint-disable-next-line no-console - console.log('Charging station start with template file ' + stationURL.file + ' error ' + JSON.stringify(error, null, ' ')); + console.error('Charging station start with template file ' + stationURL.file + ' error ', error); } - }); + } } else { console.log('No stationTemplateURLs defined in configuration, exiting'); } if (numStationsTotal === 0) { console.log('No charging station template enabled in configuration, exiting'); } else { - console.log('Charging station simulator started with ' + numStationsTotal.toString() + ' charging station(s)'); + console.log(`Charging station simulator started with ${numStationsTotal.toString()} charging station(s) and ${Utils.workerDynamicPoolInUse() ? `${Configuration.getWorkerPoolMinSize().toString()}/` : ''}${workerImplementation.size}${Utils.workerPoolInUse() ? `/${Configuration.getWorkerPoolMaxSize().toString()}` : ''} worker(s) concurrently running in '${Configuration.getWorkerProcess()}' mode (${workerImplementation.maxElementsPerWorker} charging station(s) per worker)`); } } catch (error) { // eslint-disable-next-line no-console - console.log('Bootstrap start error ' + JSON.stringify(error, null, ' ')); + console.error('Bootstrap start error ', error); } } } -Bootstrap.start(); +Bootstrap.start().catch( + (error) => { + console.error(error); + } +);