Skip to content

Commit

Permalink
Merge pull request #27 from DeFiCh/bug/healthcheck-api
Browse files Browse the repository at this point in the history
Updated health check api
  • Loading branch information
fullstackninja864 authored Sep 2, 2020
2 parents 568b58d + 96448e4 commit f9e5e57
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions packages/bitcore-node/src/healthCheckCron.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { ChainStateProvider } from './providers/chain-state';
import * as _ from 'lodash';
import logger from './logger';
import { HEATHCHECK_TIME } from './constants/config'

import { HEATHCHECK_TIME } from './constants/config';

class HealthCheck {
public criticalCount: number = 0; //0 shows healthy status of BE
private previous: any = {};
private previous = 0;
private isAlreadyRunning = false; // to make sure only single instance of this is running

private cronJob = async ({ chain, network }) => {
Expand All @@ -16,33 +15,33 @@ class HealthCheck {
chain,
network: network.toLowerCase(),
args: {
limit: 1
}
limit: 1,
},
});

if (!_.isEqual(this.previous, latestBlock)) {
if (latestBlock && this.previous < latestBlock.height) {
this.criticalCount = 0;
this.previous = latestBlock;
this.previous = latestBlock.height;
} else {
this.criticalCount += 1;
}
} catch (err) {
this.criticalCount += 1;
logger.info(err)
logger.info(err);
} finally {
setTimeout(async () => {
await this.cronJob({ chain, network })
await this.cronJob({ chain, network });
}, HEATHCHECK_TIME);
}
}
};

public startJob = (chain, network) => {
if (!(process.env.DISABLE_HEALTH_CRON === 'true')) {
if (!this.isAlreadyRunning) {
this.cronJob({ chain, network })
this.cronJob({ chain, network });
}
}
}
};
}
const HealthCheckObj = new HealthCheck();

Expand Down

0 comments on commit f9e5e57

Please sign in to comment.