Skip to content

Commit

Permalink
Add ensure_all_started callback to pools (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim authored and fishcakez committed Jun 20, 2016
1 parent fd9d659 commit 03ea53f
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 20 deletions.
2 changes: 2 additions & 0 deletions integration_test/connection/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ Code.require_file "../../test/test_support.exs", __DIR__
defmodule TestPool do
use TestConnection, [pool: DBConnection.Connection, pool_size: 1]
end

{:ok, _} = TestPool.ensure_all_started()
2 changes: 2 additions & 0 deletions integration_test/ownership/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ Code.require_file "../../test/test_support.exs", __DIR__
defmodule TestPool do
use TestConnection, [pool: DBConnection.Ownership, pool_size: 1]
end

{:ok, _} = TestPool.ensure_all_started()
2 changes: 2 additions & 0 deletions integration_test/poolboy/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ Code.require_file "../../test/test_support.exs", __DIR__
defmodule TestPool do
use TestConnection, [pool: DBConnection.Poolboy, pool_size: 1]
end

{:ok, _} = TestPool.ensure_all_started()
2 changes: 1 addition & 1 deletion integration_test/sojourn/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ case :erlang.system_info(:otp_release) do
ExUnit.start([capture_log: :true, assert_receive_timeout: 500,
exclude: [:enqueue_disconnected, :queue_timeout_exit]])

{:ok, _} = Application.ensure_all_started(:sbroker)
{:ok, _} = TestPool.ensure_all_started()
end
15 changes: 15 additions & 0 deletions lib/db_connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,21 @@ defmodule DBConnection do
end
end

@doc """
Ensures the given pool applications have been started.
### Options
* `:pool` - The `DBConnection.Pool` module to use, (default:
`DBConnection.Connection`)
"""
@spec ensure_all_started(opts :: Keyword.t, type :: atom) ::
{:ok, [atom]} | {:error, atom}
def ensure_all_started(opts, type \\ :temporary) do
Keyword.get(opts, :pool, DBConnection.Connection).ensure_all_started(opts, type)
end

@doc """
Start and link to a database connection process.
Expand Down
5 changes: 5 additions & 0 deletions lib/db_connection/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ defmodule DBConnection.Connection do

## DBConnection.Pool API

@doc false
def ensure_all_started(_opts, _type) do
{:ok, []}
end

@doc false
def start_link(mod, opts) do
start_link(mod, opts, :connection)
Expand Down
5 changes: 5 additions & 0 deletions lib/db_connection/ownership.ex
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ defmodule DBConnection.Ownership do

## Pool callbacks

@doc false
def ensure_all_started(opts, type) do
Keyword.get(opts, :ownership_pool, DBConnection.Poolboy).ensure_all_started(opts, type)
end

@doc false
def start_link(module, opts) do
Manager.start_link(module, opts)
Expand Down
6 changes: 6 additions & 0 deletions lib/db_connection/pool.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ defmodule DBConnection.Pool do
using `DBConnection`.
"""

@doc """
Ensure all applications necessary to run the pool are started.
"""
@callback ensure_all_started(opts :: Keyword.t, type :: :application.restart_type) ::
{:ok, [atom]} | {:error, atom}

@doc """
Start and link to a pool of `module` connections with options `opts`.
"""
Expand Down
5 changes: 5 additions & 0 deletions lib/db_connection/poolboy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ defmodule DBConnection.Poolboy do

@pool_timeout 5000

@doc false
def ensure_all_started(_opts, type) do
Application.ensure_all_started(:poolboy, type)
end

@doc false
def start_link(mod, opts) do
{pool_opts, worker_opts} = pool_args(mod, opts)
Expand Down
5 changes: 5 additions & 0 deletions lib/db_connection/sojourn.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ defmodule DBConnection.Sojourn do

import Supervisor.Spec

@doc false
def ensure_all_started(_opts, type) do
Application.ensure_all_started(:sbroker, type)
end

@doc false
def start_link(mod, opts) do
apply(:sbroker, :start_link, broker_args(mod, opts))
Expand Down
19 changes: 0 additions & 19 deletions lib/db_connection/sojourn/broker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,25 +67,6 @@ defmodule DBConnection.Sojourn.Broker do

@doc false
def init({broker, mod, opts}) do
apps = :application.which_applications()
unless List.keyfind(apps, :sbroker, 0) do
msg = """
The `:sbroker` application must be started to use #{inspect __MODULE__}.
Add `:sbroker` to the applications list in the project's `mix.exs` file.
Starting `:sbroker` will also start the `:sasl` application. By default
`:sasl` will generate many erlang reports, these can be disabled in the
project's `config/config.exs` file or another config file with:
config: :sasl, :sasl_error_logger, false
Alternatively the `:logger` application can handle these reports with:
config: :logger, handle_sasl_reports: true
"""
raise RuntimeError, msg
end
opts = Keyword.put(opts, :broker_pid, self())
pool = ensure_pool(mod, opts)
reg = lookup_regulator(pool)
Expand Down
4 changes: 4 additions & 0 deletions lib/db_connection/task.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ defmodule DBConnection.Task do
@moduledoc false
@behaviour DBConnection.Pool

def ensure_all_started(_opts, _type) do
{:ok, []}
end

def start_link() do
Task.Supervisor.start_link([name: __MODULE__])
end
Expand Down
4 changes: 4 additions & 0 deletions test/test_support.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ defmodule TestConnection do

defmacro __using__(opts) do
quote do
def ensure_all_started do
DBConnection.ensure_all_started(unquote(opts))
end

def start_link(opts2) do
defaults = [backoff_type: :exp, backoff_min: 200]
TestConnection.start_link(opts2 ++ unquote(opts) ++ defaults)
Expand Down

0 comments on commit 03ea53f

Please # to comment.