diff --git a/healthcheck.js b/healthcheck.js index d963eba..6ada395 100644 --- a/healthcheck.js +++ b/healthcheck.js @@ -6,7 +6,7 @@ const checks = []; //Check if UI is working and is type HTML checks.push(function check_UI() { - return new Promise((resolve, reject) => { + return new Promise(resolve => { fetch(URL).then(res => { if (!res.ok || res.status !== 200) return resolve(`UI fetch failed with status ${res.status}`); @@ -14,13 +14,13 @@ checks.push(function check_UI() { return resolve(`UI fetch: content-type not HTML`); //TODO: check DOM objects for UI resolve(true); - }).catch(error => reject(error)); + }).catch(err => resolve(err)); }) }); //Check if the Sync is finished and 100% checks.push(function check_API_sync() { - return new Promise((resolve, reject) => { + return new Promise(resolve => { fetch(URL + '/api/sync').then(res => { if (!res.ok || res.status !== 200) return resolve(`Sync API fetch failed with status ${res.status}`); @@ -30,14 +30,25 @@ checks.push(function check_API_sync() { if (data.status !== "finished" || data.syncPercentage !== 100) return resolve(`Sync not finished: current percentage ${data.syncPercentage}`); resolve(true); - }).catch(error => resolve(`Sync API: response not JSON`)) - }).catch(error => reject(error)); + }).catch(err => resolve(`Sync API: response not JSON`)) + }).catch(err => resolve(err)); }) }); -//Check if the last synced block is matched with flo core wallet -checks.push(function check_lastBlockTime() { +function getBlockHeight_coreWallet() { return new Promise((resolve, reject) => { + fetch('https://ranchimallflo.duckdns.org/api/v2.0/flocoreHeight').then(res => { + if (!res.ok || res.status !== 200) + return reject(`Get blockchain height failed with status ${res.status}`); + res.json().then(data => resolve(data.blocks)) + .catch(err => reject(`Get blockchain height: response not JSON`)) + }).catch(err => reject(err)) + }) +} + +//Check if the last synced block is matched with flo core wallet +checks.push(function check_lastBlock() { + return new Promise(resolve => { fetch(URL + "/api/blocks?limit=1").then(res => { if (!res.ok || res.status !== 200) return resolve(`Last Block fetch with status ${res.status}`); @@ -45,18 +56,17 @@ checks.push(function check_lastBlockTime() { if (!data.blocks.length) return resolve(`Last Block not found`); let last_block_no = data.blocks[0].height; - fetch('https://ranchimallflo.duckdns.org/api/v2.0/flocoreHeight').then(res2 => { - if (!res2.ok || res2.status !== 200) - return resolve(`Get blockchain height fetch with status ${res.status}`); - res2.json().then(data_2 => { - let blockchain_height = data_2.blocks; - if (blockchain_height != last_block_no) - return resolve(`Last Block#${last_block_no}. Blockchain height=${blockchain_height}`); - resolve(true); - }).catch(error => resolve(`Get blockchain height: response not JSON`)) - }).catch(error => reject(error)) - }).catch(error => resolve(`Last Block API: response not JSON`)) - }).catch(error => reject(error)) + getBlockHeight_coreWallet().then(blockchain_height => { + if (blockchain_height != last_block_no) + return resolve(`Last Block#${last_block_no}. Blockchain height=${blockchain_height}`); + resolve(true); + }).catch(err => { + console.error('Soft-check:', err); + //Unable to get block height from core wallet, ignore case + resolve(true) + }) + }).catch(err => resolve(`Last Block API: response not JSON`)) + }).catch(err => resolve(err)) }) }) Promise.all(checks.map(c => c())).then(results => { @@ -69,8 +79,8 @@ Promise.all(checks.map(c => c())).then(results => { console.debug(reasons); process.exit(1); } -}).catch(error => { +}).catch(err => { console.debug("ERROR"); - console.error(error); + console.error(err); process.exit(1); })