I read that having a GenServer monitor the shutdown of other GenServers is a common pattern.
But it seemed strange to me to have a GenServer to help my Supervisor supervising…
I ended up passing the Supervisor’s pid as a init arg to the Genservers and if I got {:error, :econnrefused}
when trying to connect to the TCP socket, I close the Supervisor with Supervisor.stop\1
@impl true
def handle_info(:connect, state = %{:config => config, :supervisor => supervisor}) do
IO.puts("connecting")
case connect(config) do
{:ok, socket} ->
Process.send_after(self(), :read_data, 1)
new_state = Map.put(state, :socket, socket)
{:noreply, new_state}
{:error, :econnrefused} ->
:ok = Supervisor.stop(supervisor)
{:noreply, state}
end
end
On all other errors I let it crash
Feel free to suggest better patterns to this