1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-25 00:07:47 +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:
ivaosthu 2019-03-07 21:31:15 +01:00 committed by Ivar Conradi Østhus
parent 106728afe4
commit 227c6c2e7a
2 changed files with 34 additions and 8 deletions

View File

@ -12,13 +12,18 @@ class ClientMetricsStore extends EventEmitter {
this.metricsDb = metricsDb; this.metricsDb = metricsDb;
this.highestIdSeen = 0; this.highestIdSeen = 0;
// Build internal state this._init(pollInterval);
metricsDb }
.getMetricsLastHour()
.then(metrics => this._emitMetrics(metrics)) async _init(pollInterval) {
.then(() => this._startPoller(pollInterval)) try {
.then(() => this.emit('ready')) const metrics = await this.metricsDb.getMetricsLastHour();
.catch(err => logger.error(err)); this._emitMetrics(metrics);
} catch (err) {
logger.error('Error fetching metrics last hour', err);
}
this._startPoller(pollInterval);
this.emit('ready');
} }
_startPoller(pollInterval) { _startPoller(pollInterval) {

View File

@ -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 => { test.cb('should poll for updates', t => {
const clock = lolex.install(); const clock = lolex.install();
@ -55,7 +77,6 @@ test.cb('should poll for updates', t => {
t.true(metrics.length === 4); t.true(metrics.length === 4);
t.true(store.highestIdSeen === 4); t.true(store.highestIdSeen === 4);
store.destroy(); store.destroy();
clock.uninstall();
t.end(); t.end();
}); });
}); });