Skip to content

Commit

Permalink
updating healthy block diff value to be set in env var or qs (#328)
Browse files Browse the repository at this point in the history
* updating healthy block diff value to be set in env var or qs

* is qs value is negative ignore
  • Loading branch information
vicky-g committed Mar 17, 2020
1 parent afad92f commit 8e13c1b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
1 change: 1 addition & 0 deletions discovery-provider/default_config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ blacklist_block_processing_window = 600
peer_refresh_interval = 3000
identity_service_url = https://identityservice.test
user_metadata_service_url = ''
healthy_block_diff = 100

[flask]
debug = true
Expand Down
26 changes: 20 additions & 6 deletions discovery-provider/src/queries/health_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

disc_prov_version = helpers.get_discovery_provider_version()

HEALTHY_BLOCK_DIFF = 100
default_healthy_block_diff = int(shared_config["discprov"]["healthy_block_diff"])

#### INTERNAL FUNCTIONS ####

Expand All @@ -49,7 +49,7 @@ def _get_db_block_state(latest_blocknum, latest_blockhash):
health_results["web"]["blocknumber"] - health_results["db"]["number"]
)
health_results["block_difference"] = block_difference
health_results["maximum_healthy_block_difference"] = HEALTHY_BLOCK_DIFF
health_results["maximum_healthy_block_difference"] = default_healthy_block_diff

return health_results

Expand Down Expand Up @@ -88,6 +88,16 @@ def version():
# has been added (ex. if it's been more than 30 minutes since last block), etc.
@bp.route("/health_check", methods=["GET"])
def health_check():
# type = A callable that is used to cast the value
verbose = request.args.get("verbose", type=str) == 'true'
enforce_block_diff = request.args.get("enforce_block_diff", type=str) == 'true'

# If the value given is not a valid int, will default to None
qs_healthy_block_diff = request.args.get("healthy_block_diff", type=int)
# If healthy block diff is given in url and positive, override config value
healthy_block_diff = qs_healthy_block_diff if qs_healthy_block_diff is not None \
and qs_healthy_block_diff >= 0 else default_healthy_block_diff

latest_block_num = None
latest_block_hash = None

Expand All @@ -106,27 +116,31 @@ def health_check():

health_results = _get_db_block_state(latest_block_num, latest_block_hash)

verbose = request.args.get("verbose", type=str) == 'true'
if verbose:
# DB connections check
health_results["db_connections"] = _get_db_conn_state()

# Return error on unhealthy block diff if requested.
enforce_block_diff = request.args.get("enforce_block_diff", type=str) == 'true'
if enforce_block_diff and health_results["block_difference"] > HEALTHY_BLOCK_DIFF:
if enforce_block_diff and health_results["block_difference"] > healthy_block_diff:
return jsonify(health_results), 500

return jsonify(health_results), 200

# Health check for block diff between DB and chain.
@bp.route("/block_check", methods=["GET"])
def block_check():
# If the value given is not a valid int, will default to None
qs_healthy_block_diff = request.args.get("healthy_block_diff", type=int)
# If healthy block diff is given in url and positive, override config value
healthy_block_diff = qs_healthy_block_diff if qs_healthy_block_diff is not None \
and qs_healthy_block_diff >= 0 else default_healthy_block_diff

latest_block = web3.eth.getBlock("latest", True)
latest_block_num = latest_block.number
latest_block_hash = latest_block.hash.hex()
health_results = _get_db_block_state(latest_block_num, latest_block_hash)

if health_results["block_difference"] > HEALTHY_BLOCK_DIFF:
if health_results["block_difference"] > healthy_block_diff:
return jsonify(health_results), 500

return jsonify(health_results), 200

0 comments on commit 8e13c1b

Please sign in to comment.