-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
2 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
apps/remote_control/lib/lexical/remote_control/manager_node_monitor.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |