diff --git a/CHANGELOG.md b/CHANGELOG.md index dfe4429156..65bd2d589c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,11 @@ ### Features +[#2294](https://github.com/poanetwork/blockscout/pull/2294) - add healthy block period checking endpoint + ### Fixes - [#2303](https://github.com/poanetwork/blockscout/pull/2303) - fix transaction csv download link +- [#2304](https://github.com/poanetwork/blockscout/pull/2304) - footer grid fix for md resolution - [#2291](https://github.com/poanetwork/blockscout/pull/2291) - dashboard fix for md resolution, transactions load fix, block info row fix, addresses page issue, check mark issue ### Chore 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 new file mode 100644 index 0000000000..957dc797be --- /dev/null +++ b/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/health_controller.ex @@ -0,0 +1,49 @@ +defmodule BlockScoutWeb.API.V1.HealthController do + use BlockScoutWeb, :controller + + alias Explorer.Chain + + def health(conn, _) do + with {:ok, number, timestamp} <- Chain.last_block_status() do + send_resp(conn, :ok, result(number, timestamp)) + else + status -> send_resp(conn, :internal_server_error, error(status)) + end + end + + def result(number, timestamp) do + %{ + "healthy" => true, + "data" => %{ + "latest_block_number" => to_string(number), + "latest_block_inserted_at" => to_string(timestamp) + } + } + |> Jason.encode!() + end + + def error({:error, :no_blocks}) do + %{ + "healthy" => false, + "error_code" => 5002, + "error_title" => "no blocks in db", + "error_description" => "There are no blocks in the DB" + } + |> Jason.encode!() + end + + def error({:error, number, timestamp}) do + %{ + "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", + "data" => %{ + "latest_block_number" => to_string(number), + "latest_block_inserted_at" => to_string(timestamp) + } + } + |> Jason.encode!() + end +end diff --git a/apps/block_scout_web/lib/block_scout_web/router.ex b/apps/block_scout_web/lib/block_scout_web/router.ex index 3a0b76b8ae..d13dc9c43e 100644 --- a/apps/block_scout_web/lib/block_scout_web/router.ex +++ b/apps/block_scout_web/lib/block_scout_web/router.ex @@ -23,6 +23,8 @@ defmodule BlockScoutWeb.Router do get("/supply", SupplyController, :supply) + get("/health", HealthController, :health) + resources("/decompiled_smart_contract", DecompiledSmartContractController, only: [:create]) resources("/verified_smart_contracts", VerifiedSmartContractController, only: [:create]) end diff --git a/apps/block_scout_web/lib/block_scout_web/templates/layout/_footer.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/layout/_footer.html.eex index 7188f283a1..9d84836a5a 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/layout/_footer.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/layout/_footer.html.eex @@ -13,7 +13,7 @@ <% col_size = if Enum.empty?(other_explorers), do: 3, else: 2 %>