Skip to content

Commit

Permalink
Added manager node monitor
Browse files Browse the repository at this point in the history
Occasionally, our script that's designed to clean up after the project
node port doesn't seem to work correctly. Because of this, it makes
sense to monitor the manager node for the project node and to
shut down when the manager node dies.
  • Loading branch information
scohen committed Sep 6, 2024
1 parent dd4bd18 commit 5520a84
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule Lexical.RemoteControl.Application do
children =
if RemoteControl.project_node?() do
[
RemoteControl.ManagerNodeMonitor,
RemoteControl.Api.Proxy,
RemoteControl.Commands.Reindex,
RemoteControl.Module.Loader,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
defmodule Lexical.RemoteControl.ManagerNodeMonitor do
@moduledoc """
A node monitor that monitors the manager node for this project, and shuts down
the system if that node dies.
"""
use GenServer
require Logger

def start_link(_) do
GenServer.start_link(__MODULE__, nil, name: __MODULE__)
end

@impl true
def init(_) do
case fetch_manager_node() do
{:ok, manager_node} ->
Node.monitor(manager_node, true)
{:ok, manager_node}

:error ->
Logger.warning("Could not determine manager node for monitoring.")
:ignore
end
end

@impl true
def handle_info({:nodedown, _}, state) do
System.stop()
{:noreply, state}
end

defp fetch_manager_node do
Enum.find_value(Node.list(), :error, fn node_name ->
string_name = Atom.to_string(node_name)

if String.starts_with?(string_name, "manager-") do
{:ok, node_name}
else
false
end
end)
end
end

0 comments on commit 5520a84

Please sign in to comment.