fix address sum cache

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

@ -3,6 +3,7 @@
### Features ### Features
### Fixes ### Fixes
- [#2901](https://github.com/poanetwork/blockscout/pull/2901) - fix address sum cache
### Chore ### Chore

@ -7,6 +7,12 @@ defmodule BlockScoutWeb.API.RPC.StatsControllerTest do
alias Explorer.ExchangeRates.Token alias Explorer.ExchangeRates.Token
alias Explorer.ExchangeRates.Source.TestSource alias Explorer.ExchangeRates.Source.TestSource
setup do
Supervisor.terminate_child(Explorer.Supervisor, Explorer.Chain.Cache.AddressSum.child_id())
Supervisor.restart_child(Explorer.Supervisor, Explorer.Chain.Cache.AddressSum.child_id())
:ok
end
describe "tokensupply" do describe "tokensupply" do
test "with missing contract address", %{conn: conn} do test "with missing contract address", %{conn: conn} do
params = %{ params = %{
@ -106,6 +112,8 @@ defmodule BlockScoutWeb.API.RPC.StatsControllerTest do
describe "ethsupply" do describe "ethsupply" do
test "returns total supply from DB", %{conn: conn} do test "returns total supply from DB", %{conn: conn} do
insert(:address, fetched_coin_balance: 6)
params = %{ params = %{
"module" => "stats", "module" => "stats",
"action" => "ethsupply" "action" => "ethsupply"

@ -16,11 +16,13 @@ defmodule Explorer.Chain.Cache.AddressSum do
alias Explorer.Chain alias Explorer.Chain
defp handle_fallback(:sum) do defp handle_fallback(:sum) do
# This will get the task PID if one exists and launch a new task if not result = fetch_from_db()
# See next `handle_fallback` definition
get_async_task()
{:return, nil} if Application.get_env(:explorer, __MODULE__)[:enabled] do
{:update, result}
else
{:return, result}
end
end end
defp handle_fallback(:async_task) do defp handle_fallback(:async_task) do
@ -29,7 +31,7 @@ defmodule Explorer.Chain.Cache.AddressSum do
{:ok, task} = {:ok, task} =
Task.start(fn -> Task.start(fn ->
try do try do
result = Chain.fetch_sum_coin_total_supply() result = fetch_from_db()
set_sum(result) set_sum(result)
rescue rescue
@ -45,6 +47,10 @@ defmodule Explorer.Chain.Cache.AddressSum do
{:update, task} {:update, task}
end end
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 # By setting this as a `callback` an async task will be started each time the
# `sum` expires (unless there is one already running) # `sum` expires (unless there is one already running)
defp async_task_on_deletion({:delete, _, :sum}), do: get_async_task() defp async_task_on_deletion({:delete, _, :sum}), do: get_async_task()

@ -12,7 +12,7 @@ defmodule Explorer.Chain.Cache.AddressSumTest do
test "returns default address sum" do test "returns default address sum" do
result = AddressSum.get_sum() result = AddressSum.get_sum()
assert is_nil(result) assert result == 0
end end
test "updates cache if initial value is zero" do test "updates cache if initial value is zero" do

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

Loading…
Cancel
Save