Merge pull request #38 from 67P/healthcheck

Extract healthcheck into its own class
This commit is contained in:
fsmanuel 2018-04-21 15:11:01 +00:00 committed by GitHub
commit ee886e6cd0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 9 deletions

View File

@ -1,6 +1,8 @@
const ethers = require('ethers'); const ethers = require('ethers');
const RSVP = require('rsvp'); const RSVP = require('rsvp');
const Healthcheck = require('./utils/healthcheck');
const ABIS = { const ABIS = {
Contributors: require('./abis/Contributors.json'), Contributors: require('./abis/Contributors.json'),
Operator: require('./abis/Operator.json'), Operator: require('./abis/Operator.json'),
@ -49,15 +51,9 @@ class Kredits {
static setup(provider, signer, ipfsConfig = null) { static setup(provider, signer, ipfsConfig = null) {
console.log('Kredits.setup() is deprecated use new Kredits().init() instead'); console.log('Kredits.setup() is deprecated use new Kredits().init() instead');
let ipfs = new IPFS(ipfsConfig); let ipfs = new IPFS(ipfsConfig);
return new Kredits(provider, signer).init().then((kredits) => {
return ipfs._ipfsAPI.id().catch((error) => { kredits.ipfs = ipfs;
throw new Error(`IPFS node not available; config: ${JSON.stringify(ipfsConfig)} - ${error.message}`); return kredits;
}).then(() => {
return new Kredits(provider, signer).init().then((kredits) => {
kredits.ipfs = ipfs;
return kredits;
});
}); });
} }
@ -98,6 +94,10 @@ class Kredits {
return this.contracts[name]; return this.contracts[name];
} }
healthcheck() {
return new Healthcheck(this).check();
}
} }
module.exports = Kredits; module.exports = Kredits;

28
lib/utils/healthcheck.js Normal file
View File

@ -0,0 +1,28 @@
class Healthcheck {
constructor(kredits) {
this.kredits = kredits;
}
check() {
return this.kredits.ipfs._ipfsAPI.id()
.catch((error) => {
throw new Error(`IPFS node not available; config: ${JSON.stringify(this.kredits.ipfs.config)} - ${error.message}`);
})
.then(() => {
let promises = Object.keys(this.kredits.contracts).map((name) => {
let contractWrapper = this.kredits.contracts[name];
return this.kredits.provider.getCode(contractWrapper.contract.address).then((code) => {
// not sure if we always get the same return value if the code is not available
// so checking if it is < 5 long
if (code === '0x00' || code.length < 5) {
throw new Error(`Contract for: ${name} not found at ${contractWrapper.contract.address} on network ${this.kredits.provider.chainId}`);
}
return true;
});
});
return Promise.all(promises);
});
}
}
module.exports = Healthcheck;