Skip to content

Commit

Permalink
Add :get_nodes call handler to Sonar
Browse files Browse the repository at this point in the history
This clause exposes the tracked nodes for internal use.
  • Loading branch information
sorentwo committed May 7, 2024
1 parent e7f91f4 commit b6302cd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
12 changes: 8 additions & 4 deletions lib/oban/sonar.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ defmodule Oban.Sonar do
defstruct [
:conf,
:timer,
interval: :timer.seconds(15),
interval: :timer.seconds(5),
nodes: %{},
stale_mult: 2,
stale_mult: 6,
status: :unknown
]

Expand Down Expand Up @@ -45,20 +45,24 @@ defmodule Oban.Sonar do
@impl GenServer
def handle_continue(:start, state) do
:ok = Notifier.listen(state.conf.name, :sonar)
:ok = Notifier.notify(state.conf, :sonar, %{node: state.conf.node, ping: :ping})
:ok = Notifier.notify(state.conf, :sonar, %{node: state.conf.node, ping: true})

{:noreply, schedule_ping(state)}
end

@impl GenServer
def handle_call(:get_nodes, _from, state) do
{:reply, Map.keys(state.nodes), state}
end

def handle_call(:get_status, _from, state) do
{:reply, state.status, state}
end

def handle_call(:prune_nodes, _from, state) do
state = prune_stale_nodes(state)

{:reply, state.nodes, state}
{:reply, Map.keys(state.nodes), state}
end

@impl GenServer
Expand Down
4 changes: 2 additions & 2 deletions test/oban/sonar_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ defmodule Oban.SonarTest do
|> Registry.whereis(Sonar)
|> GenServer.call(:prune_nodes)

assert Map.has_key?(nodes, "web.1")
refute Map.has_key?(nodes, "web.2")
assert "web.1" in nodes
refute "web.2" in nodes
end
end
end

0 comments on commit b6302cd

Please sign in to comment.