Skip to content

Commit

Permalink
Use GenServer.call instead of send
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmanzanera committed Jan 11, 2024
1 parent abeeb61 commit f442439
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
16 changes: 9 additions & 7 deletions lib/archethic_fas/quotes/cache.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,25 @@ defmodule ArchethicFAS.Quotes.Cache do
end
end

def hydrate do
GenServer.call(__MODULE__, :hydrate)
end

def init([]) do
:ets.new(@table, [:named_table, :set, :public])

{:ok, :no_state}
end

def handle_info(:hydrate, state) do
def handle_call(:hydrate, _from, state) do
case Quotes.fetch_latest(Currency.list()) do
{:ok, result} ->
:ets.insert(@table, {:latest, {:ok, result}})
{:reply, {:ok, result}, state}

{:error, reason} ->
Logger.warning("Hydrating failed: #{inspect(reason)}")

:ets.insert(@table, {:latest, {:error, reason}})
{:error, _reason} = e ->
:ets.delete(@table, :latest)
{:reply, e, state}
end

{:noreply, state}
end
end
2 changes: 1 addition & 1 deletion lib/archethic_fas/quotes/scheduler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ defmodule ArchethicFAS.Quotes.Scheduler do
end

def handle_info(:tick, state) do
send(Cache, :hydrate)
Cache.hydrate()

Process.send_after(self(), :tick, 60_000)
{:noreply, state}
Expand Down

0 comments on commit f442439

Please sign in to comment.