Update healthcheck

- Changed all reject(error) to resolve(error), so that other checks will not be interrupted.
- If API for getting block height from core wallet not working, ignore the check
This commit is contained in:
sairajzero 2023-01-05 19:45:24 +05:30
parent 8f3bd54fba
commit 36fe4004d3

View File

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