From 97fa84166bc90dfb4c2ba667857d5737cbacef41 Mon Sep 17 00:00:00 2001 From: Sid Sethi Date: Mon, 16 Sep 2019 18:54:46 -0700 Subject: [PATCH] Check healthcheck before version for discprov selection in libs --- libs/src/services/ethContracts/index.js | 47 ++++++++++++++----------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/libs/src/services/ethContracts/index.js b/libs/src/services/ethContracts/index.js index ccdc3452254..85fbc98c071 100644 --- a/libs/src/services/ethContracts/index.js +++ b/libs/src/services/ethContracts/index.js @@ -150,32 +150,39 @@ class EthContracts { try { selectedDiscoveryProvider = await Utils.promiseFight( discoveryProviders.map(async (discprov) => { - const { - data: { service: serviceName, version: serviceVersion } - } = await axios({ url: urlJoin(discprov.endpoint, 'version'), method: 'get' }) + try { + const healthResp = await axios({ url: urlJoin(discprov.endpoint, 'health_check'), method: 'get' }) + if (healthResp.status !== 200) throw new Error(`Discprov healthcheck failed ${discprov.endpoint}`) - // Compare chain service name - if (!this.expectedServiceVersions.hasOwnProperty(serviceName)) { - throw new Error(`Invalid service name: ${serviceName}`) - } + const { + data: { service: serviceName, version: serviceVersion } + } = await axios({ url: urlJoin(discprov.endpoint, 'version'), method: 'get' }) - if (serviceName !== spType) { - throw new Error(`Invalid service type: ${serviceName}. Expected ${spType}`) - } + // Compare chain service name + if (!this.expectedServiceVersions.hasOwnProperty(serviceName)) { + throw new Error(`Invalid service name: ${serviceName}`) + } - if (!semver.valid(serviceVersion)) { - throw new Error(`Invalid semver version found - ${serviceVersion}`) - } + if (serviceName !== spType) { + throw new Error(`Invalid service type: ${serviceName}. Expected ${spType}`) + } - let expectedVersion = this.expectedServiceVersions[serviceName] - if (expectedVersion !== serviceVersion) { - let validSPVersion = this.isValidSPVersion(expectedVersion, serviceVersion) - if (!validSPVersion) { - throw new Error(`Invalid service version: ${serviceName}. Expected ${expectedVersion}, found ${serviceVersion}`) + if (!semver.valid(serviceVersion)) { + throw new Error(`Invalid semver version found - ${serviceVersion}`) } - } - return discprov.endpoint + let expectedVersion = this.expectedServiceVersions[serviceName] + if (expectedVersion !== serviceVersion) { + let validSPVersion = this.isValidSPVersion(expectedVersion, serviceVersion) + if (!validSPVersion) { + throw new Error(`Invalid service version: ${serviceName}. Expected ${expectedVersion}, found ${serviceVersion}`) + } + } + + return discprov.endpoint + } catch (err) { + throw new Error(err) + } }) ) } catch (err) {