fix address sum cache

on the first call address sum cache starts task to
fetch a value from the DB and return nil
pull/2901/head
Ayrat Badykov 5 years ago
parent cd27d9e09a
commit 6f8a738735
No known key found for this signature in database
GPG Key ID: B44668E265E9396F
  1. 2
      apps/block_scout_web/test/block_scout_web/controllers/api/rpc/stats_controller_test.exs
  2. 28
      apps/explorer/lib/explorer/chain/cache/address_sum.ex
  3. 2
      apps/explorer/test/explorer/chain/import/runner/address/token_balances_test.exs

@ -106,6 +106,8 @@ defmodule BlockScoutWeb.API.RPC.StatsControllerTest do
describe "ethsupply" do
test "returns total supply from DB", %{conn: conn} do
insert(:address, fetched_coin_balance: 6)
params = %{
"module" => "stats",
"action" => "ethsupply"

@ -16,33 +16,13 @@ defmodule Explorer.Chain.Cache.AddressSum do
alias Explorer.Chain
defp handle_fallback(:sum) do
# This will get the task PID if one exists and launch a new task if not
# See next `handle_fallback` definition
get_async_task()
result = fetch_from_db()
{:return, nil}
{:update, result}
end
defp handle_fallback(:async_task) do
# If this gets called it means an async task was requested, but none exists
# so a new one needs to be launched
{:ok, task} =
Task.start(fn ->
try do
result = Chain.fetch_sum_coin_total_supply()
set_sum(result)
rescue
e ->
Logger.debug([
"Coudn't update address sum test #{inspect(e)}"
])
end
set_async_task(nil)
end)
{:update, task}
defp fetch_from_db do
Chain.fetch_sum_coin_total_supply()
end
# By setting this as a `callback` an async task will be started each time the

@ -93,7 +93,7 @@ defmodule Explorer.Chain.Import.Runner.Address.TokenBalancesTest do
value_fetched_at: DateTime.utc_now()
}
run_changes(new_changes, options) |> IO.inspect()
run_changes(new_changes, options)
end
end

Loading…
Cancel
Save