Commit | Line | Data |
---|---|---|
6af9012e | 1 | import Configuration from './utils/Configuration'; |
5fdab605 | 2 | import Constants from './utils/Constants'; |
3d2ff9e4 | 3 | import Utils from './utils/Utils'; |
3d2ff9e4 | 4 | import WorkerData from './types/WorkerData'; |
418106c8 JB |
5 | import WorkerGroup from './charging-station/WorkerGroup'; |
6 | import WorkerPool from './charging-station/WorkerPool'; | |
7dde0b73 JB |
7 | |
8 | class Bootstrap { | |
3d2ff9e4 | 9 | static async start() { |
7dde0b73 | 10 | try { |
6ecb15e4 | 11 | let numStationsTotal = 0; |
ad3de6c4 | 12 | let numConcurrentWorkers = 0; |
4faad557 JB |
13 | const chargingStationsPerWorker = Configuration.getChargingStationsPerWorker(); |
14 | let chargingStationsPerWorkerCounter = 0; | |
418106c8 JB |
15 | let workerImplementation: WorkerGroup | WorkerPool; |
16 | if (Configuration.useWorkerPool()) { | |
17 | workerImplementation = new WorkerPool('./dist/charging-station/StationWorker.js'); | |
18 | void workerImplementation.start(); | |
19 | } | |
7dde0b73 | 20 | // Start each ChargingStation object in a worker thread |
2e6f5966 | 21 | if (Configuration.getStationTemplateURLs()) { |
4faad557 | 22 | for (const stationURL of Configuration.getStationTemplateURLs()) { |
7dde0b73 | 23 | try { |
e118beaa | 24 | const nbStations = stationURL.numberOfStations ? stationURL.numberOfStations : 0; |
e118beaa | 25 | for (let index = 1; index <= nbStations; index++) { |
90225052 | 26 | const workerData: WorkerData = { |
7dde0b73 | 27 | index, |
3d2ff9e4 | 28 | templateFile: stationURL.file |
90225052 | 29 | }; |
97ed5cec | 30 | if (Configuration.useWorkerPool()) { |
418106c8 JB |
31 | void workerImplementation.addElement(workerData); |
32 | numConcurrentWorkers = workerImplementation.size; | |
418106c8 | 33 | // Start worker sequentially to optimize memory at start time |
6baef03b | 34 | await Utils.sleep(Constants.START_WORKER_DELAY); |
418106c8 JB |
35 | } else { |
36 | // eslint-disable-next-line no-lonely-if | |
37 | if (chargingStationsPerWorkerCounter === 0 || chargingStationsPerWorkerCounter >= chargingStationsPerWorker) { | |
38 | // Start new WorkerGroup with one charging station | |
39 | workerImplementation = new WorkerGroup('./dist/charging-station/StationWorker.js', workerData, chargingStationsPerWorker); | |
40 | void workerImplementation.start(); | |
41 | numConcurrentWorkers++; | |
42 | chargingStationsPerWorkerCounter = 1; | |
418106c8 JB |
43 | // Start worker sequentially to optimize memory at start time |
44 | await Utils.sleep(Constants.START_WORKER_DELAY); | |
45 | } else { | |
46 | // Add charging station to existing WorkerGroup | |
47 | void workerImplementation.addElement(workerData); | |
48 | chargingStationsPerWorkerCounter++; | |
418106c8 | 49 | } |
3d2ff9e4 | 50 | } |
d392bb75 | 51 | numStationsTotal++; |
7dde0b73 JB |
52 | } |
53 | } catch (error) { | |
54 | // eslint-disable-next-line no-console | |
2e6f5966 | 55 | console.log('Charging station start with template file ' + stationURL.file + ' error ' + JSON.stringify(error, null, ' ')); |
7dde0b73 | 56 | } |
3d2ff9e4 | 57 | } |
7dde0b73 | 58 | } else { |
d3a7883e | 59 | console.log('No stationTemplateURLs defined in configuration, exiting'); |
7dde0b73 | 60 | } |
6ecb15e4 JB |
61 | if (numStationsTotal === 0) { |
62 | console.log('No charging station template enabled in configuration, exiting'); | |
4fa59b8a JB |
63 | } else if (Configuration.useWorkerPool()) { |
64 | console.log('Charging station simulator started with ' + numStationsTotal.toString() + ' charging station(s) and ' + numConcurrentWorkers.toString() + '/' + Configuration.getWorkerPoolMaxSize().toString() + ' worker(s) concurrently running'); | |
d4a73fb7 | 65 | } else { |
4fa59b8a | 66 | console.log('Charging station simulator started with ' + numStationsTotal.toString() + ' charging station(s) and ' + numConcurrentWorkers.toString() + ' worker(s) concurrently running'); |
6ecb15e4 | 67 | } |
7dde0b73 JB |
68 | } catch (error) { |
69 | // eslint-disable-next-line no-console | |
70 | console.log('Bootstrap start error ' + JSON.stringify(error, null, ' ')); | |
71 | } | |
72 | } | |
73 | } | |
74 | ||
4faad557 JB |
75 | Bootstrap.start().catch( |
76 | (error) => { | |
77 | console.error(error); | |
78 | } | |
79 | ); |