From 532010cda1b21f54cdb8e37e5cb67eae69b5022f Mon Sep 17 00:00:00 2001 From: Viktor Baranov Date: Fri, 12 Aug 2022 13:32:13 +0300 Subject: [PATCH] Enhance health API endpoint: better parsing HEALTHY_BLOCKS_PERIOD and use it in the response --- CHANGELOG.md | 1 + .../controllers/api/v1/health_controller.ex | 11 ++++++++++- config/runtime.exs | 8 +++++++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3661a65211..680f31daf3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### Features ### Fixes +- [#5904](https://github.com/blockscout/blockscout/pull/5904) - Enhance health API endpoint: better parsing HEALTHY_BLOCKS_PERIOD and use it in the response - [#5809](https://github.com/blockscout/blockscout/pull/5809) - Fix 404 on `/metadata` page - [#5786](https://github.com/blockscout/blockscout/pull/5786) - Replace `current_path` with `Controller.current_full_path` in two controllers - [#5807](https://github.com/blockscout/blockscout/pull/5807) - Update Makefile migrate command due to release build diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/health_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/health_controller.ex index 578c21f77a..52352d55d4 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/health_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/health_controller.ex @@ -3,6 +3,7 @@ defmodule BlockScoutWeb.API.V1.HealthController do alias BlockScoutWeb.API.APILogger alias Explorer.Chain + alias Timex.Duration def health(conn, _) do APILogger.log(conn) @@ -39,12 +40,20 @@ defmodule BlockScoutWeb.API.V1.HealthController do end def error({:error, number, timestamp}) do + healthy_blocks_period = Application.get_env(:explorer, :healthy_blocks_period) + + healthy_blocks_period_formatted = + healthy_blocks_period + |> Duration.from_milliseconds() + |> Duration.to_minutes() + |> trunc() + %{ "healthy" => false, "error_code" => 5001, "error_title" => "blocks fetching is stuck", "error_description" => - "There are no new blocks in the DB for the last 5 mins. Check the healthiness of Ethereum archive node or the Blockscout DB instance", + "There are no new blocks in the DB for the last #{healthy_blocks_period_formatted} mins. Check the healthiness of Ethereum archive node or the Blockscout DB instance", "data" => %{ "latest_block_number" => to_string(number), "latest_block_inserted_at" => to_string(timestamp) diff --git a/config/runtime.exs b/config/runtime.exs index 6bdd5fa7a8..b47099a5b2 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -158,6 +158,12 @@ config :ethereum_jsonrpc, EthereumJSONRPC.Geth, debug_trace_transaction_timeout: disable_indexer = System.get_env("DISABLE_INDEXER") disable_webapp = System.get_env("DISABLE_WEBAPP") +healthy_blocks_period = + System.get_env("HEALTHY_BLOCKS_PERIOD", "5") + |> Integer.parse() + |> elem(0) + |> :timer.minutes() + config :explorer, coin: System.get_env("COIN") || "POA", coin_name: System.get_env("COIN_NAME") || System.get_env("COIN") || "POA", @@ -166,7 +172,7 @@ config :explorer, "homestead,tangerineWhistle,spuriousDragon,byzantium,constantinople,petersburg,istanbul,berlin,london,default", include_uncles_in_average_block_time: if(System.get_env("UNCLES_IN_AVERAGE_BLOCK_TIME") == "true", do: true, else: false), - healthy_blocks_period: System.get_env("HEALTHY_BLOCKS_PERIOD") || :timer.minutes(5), + healthy_blocks_period: healthy_blocks_period, realtime_events_sender: if(disable_webapp != "true", do: Explorer.Chain.Events.SimpleSender,