parent
09889e2506
commit
32663bc027
@ -0,0 +1,52 @@ |
||||
defmodule Explorer.Chain.Cache.MinMissingBlockNumber do |
||||
@moduledoc """ |
||||
Caches min missing block number (break in the chain). |
||||
""" |
||||
|
||||
use GenServer |
||||
|
||||
alias Explorer.Chain |
||||
|
||||
@doc """ |
||||
Starts a process to periodically update the % of blocks indexed. |
||||
""" |
||||
@spec start_link(term()) :: GenServer.on_start() |
||||
def start_link(_) do |
||||
GenServer.start_link(__MODULE__, :ok, name: __MODULE__) |
||||
end |
||||
|
||||
@impl true |
||||
def init(args) do |
||||
Task.start_link(&fetch_min_missing_block/0) |
||||
|
||||
schedule_next_consolidation() |
||||
|
||||
{:ok, args} |
||||
end |
||||
|
||||
def fetch_min_missing_block do |
||||
result = Chain.fetch_min_missing_block_cache() |
||||
|
||||
if result > 0 do |
||||
params = %{ |
||||
counter_type: "min_missing_block_number", |
||||
value: result |
||||
} |
||||
|
||||
Chain.upsert_last_fetched_counter(params) |
||||
end |
||||
end |
||||
|
||||
defp schedule_next_consolidation do |
||||
Process.send_after(self(), :fetch_min_missing_block, :timer.minutes(20)) |
||||
end |
||||
|
||||
@impl true |
||||
def handle_info(:fetch_min_missing_block, state) do |
||||
fetch_min_missing_block() |
||||
|
||||
schedule_next_consolidation() |
||||
|
||||
{:noreply, state} |
||||
end |
||||
end |
@ -0,0 +1,12 @@ |
||||
defmodule Explorer.Repo.Migrations.MinMissingBlockNumber do |
||||
use Ecto.Migration |
||||
|
||||
def change do |
||||
insert_initial_genesis_block = """ |
||||
INSERT INTO last_fetched_counters (counter_type, value, inserted_at, updated_at) |
||||
VALUES ('min_missing_block_number', 0, NOW(), NOW()); |
||||
""" |
||||
|
||||
execute(insert_initial_genesis_block) |
||||
end |
||||
end |
Loading…
Reference in new issue