diff --git a/lib/ha_handler/drbd/watcher.ex b/lib/ha_handler/drbd/watcher.ex index 85f3361..d6af8cd 100644 --- a/lib/ha_handler/drbd/watcher.ex +++ b/lib/ha_handler/drbd/watcher.ex @@ -28,15 +28,23 @@ defmodule HAHandler.DRBD.Watcher do hostname = Keyword.get(opts, :hostname) password = Keyword.get(opts, :password) - {:ok, pid} = connect(hostname, password) + case connect(hostname, password) do + {:ok, pid} -> + state = %{ + backend: pid, + hostname: hostname, + password: password + } - state = %{ - backend: pid, - hostname: hostname, - password: password - } + {:ok, state} + {:error, err} -> - {:ok, state} + # Wait for 10 seconds so that the supervisor does not loop on dead node + # (and reach the max_restart threshold / stop trying). + Process.sleep(10_000) + + {:error, err} + end end @impl true diff --git a/lib/ha_handler/pgsql/watcher.ex b/lib/ha_handler/pgsql/watcher.ex index ef17bd6..907c2ed 100644 --- a/lib/ha_handler/pgsql/watcher.ex +++ b/lib/ha_handler/pgsql/watcher.ex @@ -12,14 +12,19 @@ defmodule HAHandler.PGSQL.Watcher do # successful. # TODO: set dbconnections backoff and connect hooks # See https://github.com/elixir-ecto/db_connection/blob/master/lib/db_connection.ex#L343 - {:ok, pid} = Postgrex.start_link(opts) + case Postgrex.start_link(opts) do + {:ok, pid} -> + state = %{ + backend: pid, + hostname: Keyword.get(opts, :hostname) + } - state = %{ - backend: pid, - hostname: Keyword.get(opts, :hostname) - } - - {:ok, state} + {:ok, state} + {:error, err} -> + # Will be catched by the supervisor if anything happen. It should not + # be triggered even if a PGSQL node down, since Postgrex has its own + # surpervision tree. + {:error, err} end @impl true