29 lines
701 B
Elixir
29 lines
701 B
Elixir
defmodule HAHandler.PGSQL.Watcher do
|
|
use GenServer
|
|
require Logger
|
|
|
|
def start_link(opts) do
|
|
GenServer.start_link(__MODULE__, opts)
|
|
end
|
|
|
|
@impl true
|
|
def init(opts) do
|
|
# Starts a Postgrex child but does not means the connection was
|
|
# 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)
|
|
|
|
state = %{
|
|
backend: pid,
|
|
hostname: Keyword.get(opts, :hostname)
|
|
}
|
|
|
|
{:ok, state}
|
|
end
|
|
|
|
@impl true
|
|
def handle_call({:execute, query, params}, _from, %{backend: backend} = state) do
|
|
{:reply, Postgrex.query(backend, query, params), state}
|
|
end
|
|
end
|