|
|
|
@ -6,6 +6,12 @@ defmodule Explorer.Chain.Cache.MinMissingBlockNumber do |
|
|
|
|
use GenServer |
|
|
|
|
|
|
|
|
|
alias Explorer.Chain |
|
|
|
|
alias Explorer.Chain.Cache.BlockNumber |
|
|
|
|
|
|
|
|
|
@default_batch_size 100_000 |
|
|
|
|
@normal_interval 10 |
|
|
|
|
@increased_interval :timer.minutes(20) |
|
|
|
|
@default_last_fetched_number -1 |
|
|
|
|
|
|
|
|
|
@doc """ |
|
|
|
|
Starts a process to periodically update the % of blocks indexed. |
|
|
|
@ -16,37 +22,52 @@ defmodule Explorer.Chain.Cache.MinMissingBlockNumber do |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
@impl true |
|
|
|
|
def init(args) do |
|
|
|
|
Task.start_link(&fetch_min_missing_block/0) |
|
|
|
|
def init(_) do |
|
|
|
|
schedule_next_consolidation(@normal_interval) |
|
|
|
|
{:ok, %{last_fetched_number: @default_last_fetched_number}} |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
schedule_next_consolidation() |
|
|
|
|
def fetch_min_missing_block(last_fetched_number) do |
|
|
|
|
from = last_fetched_number + 1 |
|
|
|
|
to = last_fetched_number + batch_size() |
|
|
|
|
max_block_number = BlockNumber.get_max() - 1 |
|
|
|
|
|
|
|
|
|
{:ok, args} |
|
|
|
|
end |
|
|
|
|
{corrected_to, continue?} = if to >= max_block_number, do: {max_block_number, false}, else: {to, true} |
|
|
|
|
|
|
|
|
|
def fetch_min_missing_block do |
|
|
|
|
result = Chain.fetch_min_missing_block_cache() |
|
|
|
|
result = Chain.fetch_min_missing_block_cache(from, corrected_to) |
|
|
|
|
|
|
|
|
|
unless is_nil(result) do |
|
|
|
|
cond do |
|
|
|
|
not is_nil(result) -> |
|
|
|
|
params = %{ |
|
|
|
|
counter_type: "min_missing_block_number", |
|
|
|
|
value: result |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Chain.upsert_last_fetched_counter(params) |
|
|
|
|
schedule_next_consolidation(@increased_interval) |
|
|
|
|
@default_last_fetched_number |
|
|
|
|
|
|
|
|
|
continue? -> |
|
|
|
|
schedule_next_consolidation(@normal_interval) |
|
|
|
|
corrected_to |
|
|
|
|
|
|
|
|
|
true -> |
|
|
|
|
schedule_next_consolidation(@increased_interval) |
|
|
|
|
@default_last_fetched_number |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
defp schedule_next_consolidation do |
|
|
|
|
Process.send_after(self(), :fetch_min_missing_block, :timer.minutes(20)) |
|
|
|
|
defp schedule_next_consolidation(interval) do |
|
|
|
|
Process.send_after(self(), :fetch_min_missing_block, interval) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
@impl true |
|
|
|
|
def handle_info(:fetch_min_missing_block, state) do |
|
|
|
|
fetch_min_missing_block() |
|
|
|
|
|
|
|
|
|
schedule_next_consolidation() |
|
|
|
|
def handle_info(:fetch_min_missing_block, %{last_fetched_number: last_fetched_number} = state) do |
|
|
|
|
new_last_number = fetch_min_missing_block(last_fetched_number) |
|
|
|
|
{:noreply, %{state | last_fetched_number: new_last_number}} |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
{:noreply, state} |
|
|
|
|
defp batch_size do |
|
|
|
|
Application.get_env(:explorer, __MODULE__)[:batch_size] || @default_batch_size |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|