Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 13 additions & 18 deletions lib/cluster/strategy/postgres.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,19 @@ defmodule Cluster.Strategy.Postgres do
end

def handle_continue(:connect, state) do
with {:ok, conn} <- Postgrex.start_link(state.meta.opts.()),
{:ok, conn_notif} <- Postgrex.Notifications.start_link(state.meta.opts.()),
{_, _} <- Postgrex.Notifications.listen(conn_notif, state.config[:channel_name]) do
Logger.info(state.topology, "Connected to Postgres database")

meta = %{
state.meta
| conn: conn,
conn_notif: conn_notif,
heartbeat_ref: heartbeat(0)
}

{:noreply, put_in(state.meta, meta)}
else
reason ->
Logger.error(state.topology, "Failed to connect to Postgres: #{inspect(reason)}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when this fails loudly does it take down the whole app with it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would need to check exactly, but in theory, if the failure happen repeatedly, then it can cause whole application to go down.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it take down the whole app with it?

No, Supervisor will try to restart the Postgres strategy process, and after exhausting the retries, it will just leave it unstarted. But that change doesn’t make much sense anyway, because the node will be out of the cluster in case of a Postgres failure. You probably want to forward already assigned values, send a connect message after a timeout, and handle them more gracefully

{:noreply, state}
end
{:ok, conn} = Postgrex.start_link(state.meta.opts.())
{:ok, conn_notif} = Postgrex.Notifications.start_link(state.meta.opts.())
{_, _} = Postgrex.Notifications.listen(conn_notif, state.config[:channel_name])
Logger.info(state.topology, "Connected to Postgres database")

meta = %{
state.meta
| conn: conn,
conn_notif: conn_notif,
heartbeat_ref: heartbeat(0)
}

{:noreply, put_in(state.meta, meta)}
end

def handle_info(:heartbeat, state) do
Expand Down
Loading