Current section
Files
Jump to
Current section
Files
lib/pgb_connection_reaper.ex
defmodule PgbConnectionReaper do
@moduledoc false
alias PgbConnectionReaper.{Config, Reaper}
@doc """
Starts the PgbConnectionReaper with the given repos and options.
## Parameters
- repos: A list of Ecto repo modules to monitor.
- opts: A keyword list of options:
- :frequency - Interval between disconnections in milliseconds (default: 5 minutes)
- :graceful_window - Time allowed for graceful disconnection in milliseconds (default: 2 minutes)
## Returns
- `{:ok, pid}`: If the Reaper starts successfully.
- `{:error, reason}`: If there's an error starting the Reaper
"""
@spec start_link([module()], keyword()) :: Supervisor.on_start()
def start_link(repos, opts \\ []) when is_list(repos) do
name = Keyword.get(opts, :name, __MODULE__)
config = %Config{
repos: repos,
frequency: Keyword.get(opts, :frequency, :timer.minutes(5)),
graceful_window: Keyword.get(opts, :graceful_window, :timer.minutes(2))
}
Reaper.start_link(config, name: name)
end
@doc """
Returns a child specification for starting the PgbConnectionReaper
under a supervisor.
"""
def child_spec({repos, opts}) when is_list(opts), do: build_child_spec(repos, opts)
def child_spec([repos, opts]) when is_list(opts), do: build_child_spec(repos, opts)
def child_spec([repos]), do: build_child_spec(repos, [])
defp build_child_spec(repos, opts) do
name = Keyword.get(opts, :name, __MODULE__)
%{
id: name,
start: {__MODULE__, :start_link, [repos, opts]},
type: :worker
}
end
end