From 03ea53fbe807c31855da15df6a6739b7eec336b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 20 Jun 2016 15:05:26 +0200 Subject: [PATCH] Add ensure_all_started callback to pools (#46) --- integration_test/connection/test_helper.exs | 2 ++ integration_test/ownership/test_helper.exs | 2 ++ integration_test/poolboy/test_helper.exs | 2 ++ integration_test/sojourn/test_helper.exs | 2 +- lib/db_connection.ex | 15 +++++++++++++++ lib/db_connection/connection.ex | 5 +++++ lib/db_connection/ownership.ex | 5 +++++ lib/db_connection/pool.ex | 6 ++++++ lib/db_connection/poolboy.ex | 5 +++++ lib/db_connection/sojourn.ex | 5 +++++ lib/db_connection/sojourn/broker.ex | 19 ------------------- lib/db_connection/task.ex | 4 ++++ test/test_support.exs | 4 ++++ 13 files changed, 56 insertions(+), 20 deletions(-) diff --git a/integration_test/connection/test_helper.exs b/integration_test/connection/test_helper.exs index f024789..4e0fcd7 100644 --- a/integration_test/connection/test_helper.exs +++ b/integration_test/connection/test_helper.exs @@ -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() diff --git a/integration_test/ownership/test_helper.exs b/integration_test/ownership/test_helper.exs index 7c402dd..27a8926 100644 --- a/integration_test/ownership/test_helper.exs +++ b/integration_test/ownership/test_helper.exs @@ -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() diff --git a/integration_test/poolboy/test_helper.exs b/integration_test/poolboy/test_helper.exs index c38342a..a981196 100644 --- a/integration_test/poolboy/test_helper.exs +++ b/integration_test/poolboy/test_helper.exs @@ -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() diff --git a/integration_test/sojourn/test_helper.exs b/integration_test/sojourn/test_helper.exs index 975bc79..33eb960 100644 --- a/integration_test/sojourn/test_helper.exs +++ b/integration_test/sojourn/test_helper.exs @@ -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 diff --git a/lib/db_connection.ex b/lib/db_connection.ex index 755726c..8b69647 100644 --- a/lib/db_connection.ex +++ b/lib/db_connection.ex @@ -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. diff --git a/lib/db_connection/connection.ex b/lib/db_connection/connection.ex index 459d083..708a2c5 100644 --- a/lib/db_connection/connection.ex +++ b/lib/db_connection/connection.ex @@ -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) diff --git a/lib/db_connection/ownership.ex b/lib/db_connection/ownership.ex index 4cd6df2..a086a5c 100644 --- a/lib/db_connection/ownership.ex +++ b/lib/db_connection/ownership.ex @@ -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) diff --git a/lib/db_connection/pool.ex b/lib/db_connection/pool.ex index 89bd4d5..2c747f5 100644 --- a/lib/db_connection/pool.ex +++ b/lib/db_connection/pool.ex @@ -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`. """ diff --git a/lib/db_connection/poolboy.ex b/lib/db_connection/poolboy.ex index 99698e3..a8af691 100644 --- a/lib/db_connection/poolboy.ex +++ b/lib/db_connection/poolboy.ex @@ -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) diff --git a/lib/db_connection/sojourn.ex b/lib/db_connection/sojourn.ex index 06c8ea0..ce9732e 100644 --- a/lib/db_connection/sojourn.ex +++ b/lib/db_connection/sojourn.ex @@ -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)) diff --git a/lib/db_connection/sojourn/broker.ex b/lib/db_connection/sojourn/broker.ex index 02d7e85..782bd4e 100644 --- a/lib/db_connection/sojourn/broker.ex +++ b/lib/db_connection/sojourn/broker.ex @@ -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) diff --git a/lib/db_connection/task.ex b/lib/db_connection/task.ex index f67545e..1316df1 100644 --- a/lib/db_connection/task.ex +++ b/lib/db_connection/task.ex @@ -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 diff --git a/test/test_support.exs b/test/test_support.exs index 2b33e81..2285489 100644 --- a/test/test_support.exs +++ b/test/test_support.exs @@ -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)