Extract Indexer.start_monitor

Rename Indexer.BlockFetcher.monitor_task to Indexer.start_monitor, so
that all fetchers can use it.
pull/255/head
Luke Imhoff 6 years ago
parent c98afd5bbb
commit 5d62977973
  1. 12
      apps/explorer/lib/explorer/indexer.ex
  2. 10
      apps/explorer/lib/explorer/indexer/block_fetcher.ex

@ -4,7 +4,7 @@ defmodule Explorer.Indexer do
"""
require Logger
alias Explorer.Chain
alias Explorer.{Chain, Indexer}
@doc """
The maximum `t:Explorer.Chain.Block.t/0` `number` that was indexed
@ -79,6 +79,16 @@ defmodule Explorer.Indexer do
Application.put_env(:explorer, :indexer, Keyword.put(config(), :debug_logs, false))
end
@doc """
Starts a child task of `Indexer.TaskSupervisor` and monitors it, instead of linking to it.
"""
@spec start_monitor((() -> term())) :: {:ok, pid, reference}
def start_monitor(task_function) when is_function(task_function, 0) do
{:ok, pid} = Task.Supervisor.start_child(Indexer.TaskSupervisor, task_function)
ref = Process.monitor(pid)
{:ok, pid, ref}
end
defp debug_logs_enabled? do
Keyword.fetch!(config(), :debug_logs)
end

@ -87,13 +87,13 @@ defmodule Explorer.Indexer.BlockFetcher do
@impl GenServer
def handle_info(:catchup_index, %{} = state) do
{:ok, genesis_task, _ref} = monitor_task(fn -> genesis_task(state) end)
{:ok, genesis_task, _ref} = Indexer.start_monitor(fn -> genesis_task(state) end)
{:noreply, %{state | genesis_task: genesis_task}}
end
def handle_info(:realtime_index, %{} = state) do
{:ok, realtime_task, _ref} = monitor_task(fn -> realtime_task(state) end)
{:ok, realtime_task, _ref} = Indexer.start_monitor(fn -> realtime_task(state) end)
{:noreply, %{state | realtime_task: realtime_task}}
end
@ -315,10 +315,4 @@ defmodule Explorer.Indexer.BlockFetcher do
Process.send_after(self(), :realtime_index, state.realtime_interval)
state
end
defp monitor_task(task_func) do
{:ok, pid} = Task.Supervisor.start_child(Indexer.TaskSupervisor, task_func)
ref = Process.monitor(pid)
{:ok, pid, ref}
end
end

Loading…
Cancel
Save