Skip to content

Commit

Permalink
[Monitoring] Use async/await pattern which should address weird bug (#…
Browse files Browse the repository at this point in the history
…42876)

* Use async/await pattern which should address weird bug

* Fix tests
  • Loading branch information
chrisronline authored Aug 9, 2019
1 parent abe56c2 commit 3f65d5f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/legacy/server/usage/classes/__tests__/collector_set.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describe('CollectorSet', () => {
// Do nothing
}
// This must return an empty object instead of null/undefined
expect(result).to.eql([{}]);
expect(result).to.eql([]);
});
});

Expand Down
28 changes: 14 additions & 14 deletions src/legacy/server/usage/classes/collector_set.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/

import { snakeCase } from 'lodash';
import Promise from 'bluebird';
import { getCollectorLogger } from '../lib';
import { Collector } from './collector';
import { UsageCollector } from './usage_collector';
Expand Down Expand Up @@ -114,25 +113,26 @@ export class CollectorSet {
* Call a bunch of fetch methods and then do them in bulk
* @param {CollectorSet} collectorSet - a set of collectors to fetch. Default to all registered collectors
*/
bulkFetch(callCluster, collectorSet = this) {
async bulkFetch(callCluster, collectorSet = this) {
if (!(collectorSet instanceof CollectorSet)) {
throw new Error(`bulkFetch method given bad collectorSet parameter: ` + typeof collectorSet);
}

const fetchPromises = collectorSet.map(collector => {
const collectorType = collector.type;
this._log.debug(`Fetching data from ${collectorType} collector`);
return Promise.props({
type: collectorType,
result: collector.fetchInternal(callCluster) // use the wrapper for fetch, kicks in error checking
})
.catch(err => {
this._log.warn(err);
this._log.warn(`Unable to fetch data from ${collectorType} collector`);
return {};
const responses = [];
await collectorSet.asyncEach(async collector => {
this._log.debug(`Fetching data from ${collector.type} collector`);
try {
responses.push({
type: collector.type,
result: await collector.fetchInternal(callCluster)
});
}
catch (err) {
this._log.warn(err);
this._log.warn(`Unable to fetch data from ${collector.type} collector`);
}
});
return Promise.all(fetchPromises);
return responses;
}

/*
Expand Down

0 comments on commit 3f65d5f

Please sign in to comment.