mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	fix: Metrics poller should start even if inital fetch fails.
Currently if the intial metrics-fetch fails unleash will not setup a metrics-poller. This fix ensures that the metrics poller will start regardless of whether the inital fetch fails or succeeds. closes #396
This commit is contained in:
		
							parent
							
								
									6dc2ecdb13
								
							
						
					
					
						commit
						756ece1dae
					
				| @ -12,13 +12,18 @@ class ClientMetricsStore extends EventEmitter { | ||||
|         this.metricsDb = metricsDb; | ||||
|         this.highestIdSeen = 0; | ||||
| 
 | ||||
|         // Build internal state
 | ||||
|         metricsDb | ||||
|             .getMetricsLastHour() | ||||
|             .then(metrics => this._emitMetrics(metrics)) | ||||
|             .then(() => this._startPoller(pollInterval)) | ||||
|             .then(() => this.emit('ready')) | ||||
|             .catch(err => logger.error(err)); | ||||
|         this._init(pollInterval); | ||||
|     } | ||||
| 
 | ||||
|     async _init(pollInterval) { | ||||
|         try { | ||||
|             const metrics = await this.metricsDb.getMetricsLastHour(); | ||||
|             this._emitMetrics(metrics); | ||||
|         } catch (err) { | ||||
|             logger.error('Error fetching metrics last hour', err); | ||||
|         } | ||||
|         this._startPoller(pollInterval); | ||||
|         this.emit('ready'); | ||||
|     } | ||||
| 
 | ||||
|     _startPoller(pollInterval) { | ||||
|  | ||||
| @ -37,6 +37,28 @@ test.cb('should call database on startup', t => { | ||||
|     }); | ||||
| }); | ||||
| 
 | ||||
| test.cb('should start poller even if inital database fetch fails', t => { | ||||
|     const clock = lolex.install(); | ||||
|     const mock = getMockDb(); | ||||
|     mock.getMetricsLastHour = () => Promise.reject('oops'); | ||||
| 
 | ||||
|     const store = new ClientMetricStore(mock, 100); | ||||
| 
 | ||||
|     const metrics = []; | ||||
|     store.on('metrics', m => metrics.push(m)); | ||||
| 
 | ||||
|     store.on('ready', () => { | ||||
|         t.true(metrics.length === 0); | ||||
|         clock.tick(300); | ||||
|         process.nextTick(() => { | ||||
|             t.true(metrics.length === 3); | ||||
|             t.true(store.highestIdSeen === 4); | ||||
|             store.destroy(); | ||||
|             t.end(); | ||||
|         }); | ||||
|     }); | ||||
| }); | ||||
| 
 | ||||
| test.cb('should poll for updates', t => { | ||||
|     const clock = lolex.install(); | ||||
| 
 | ||||
| @ -55,7 +77,6 @@ test.cb('should poll for updates', t => { | ||||
|             t.true(metrics.length === 4); | ||||
|             t.true(store.highestIdSeen === 4); | ||||
|             store.destroy(); | ||||
|             clock.uninstall(); | ||||
|             t.end(); | ||||
|         }); | ||||
|     }); | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user